in software development, testing needs to go as automated as possible and as fast as possible 😀

hostnamectl;
Operating System: Debian GNU/Linux 12 (bookworm)  
          Kernel: Linux 6.1.0-13-amd64
    Architecture: x86-64

# tested with
gcc --version
gcc (Debian 12.2.0-14) 12.2.0

PROGNAME="string_pointer_const.c";time gcc -g ${PROGNAME} -o ${PROGNAME}.bin; ./${PROGNAME}.bin

# -g means: compile with debug symbols = so gdb can debug the binary
# --help does not seem to give all possible arguments (-g is missing)

# start the debuggin in vim:
:packageadd termdebug
:Termdebug

# hotkeys:
# switch from gdb to vim - Ctrl+W then L
# switch from vim to gdb - [ESC] then type :gdb
# in gdb window: clear screen - Ctrl+C then Ctrl+L
# in gdb window: load binary with debug symbols for debugging
file ./${PROGNAME}.bin

# in gdb window: start the debugging process
start
# output content of variable string1
print string1

# "step" (also steps into functions)
s

# with repeated "s" gdb will also step into glibc library functions
# look further below how to download glibc
109 in ../sysdeps/x86_64/multiarch/strchr-avx2.S 
# "next" step (over)
n

# "step out"
finish

# quit gdb
exit

alternatively: script compile.sh

vim /scripts/compile.sh
#!/bin/bash
echo "=== gcc compile.sh $1 to $1.bin ==="
gcc --version|head -n1; # what gcc version is used

# name of program (without .c ending)
FILENAME=$1;

# status info given before compile
STATUS="compiling $FILENAME to $FILENAME.bin"

# enables compilation with gdb debugging symbols
DEBUG=""
if [[ $2 = "debug" ]]
then
DEBUG=-ggdb
STATUS=$STATUS" with debug symbols."
else
STATUS=$STATUS" without debug symbols."
fi
# -g Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging
# information.
#
# On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information
# makes debugging work better in GDB but probably makes other debuggers crash or refuse to read the program. If you want to control for
# certain whether to generate the extra information, use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).
#
# -ggdb
# Produce debugging information for use by GDB. This means to use the most expressive format available (DWARF, stabs, or the native
# format if neither of those are supported), including GDB extensions if at all possible.
# src: https://linux.die.net/man/1/gcc

if [[ -z $1 ]];
then 
echo "no parameter passed. please pass filename (with .c ending) to script to compile."
exit 22; # src: https://www.cyberciti.biz/faq/linux-bash-exit-status-set-exit-statusin-bash/ code 22 "Invalid argument"
fi

echo "..."$STATUS
gcc $DEBUG $FILENAME -o $FILENAME.bin;
# -v outputs massive amounts of info on how the compiler was compiled
# gcc -v $DEBUG $FILENAME.c -o $FILENAME.c.bin;

echo "...running $FILENAME.bin"
echo ""

./$FILENAME.bin

exit 0; # Success

then in order to compile ./example.c call it like:

/scripts/compile.sh example.c debug

sample output:

=== gcc compile.sh string_compare to string_compare.bin ===
gcc (Debian 12.2.0-14) 12.2.0
...compiling string_compare.c to string_compare.c.bin with debug symbols.
...running string_compare.c.bin

aaa < aab
aaa < abb abb > aab
aaa == aaa

gcc –help

gcc --help
Usage: gcc [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase.
  --help                   Display this information.
  --target-help            Display target specific command line options (including assembler and linker options).
  --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...].
                           Display specific types of command line options.
  (Use '-v --help' to display command line options of sub-processes).
  --version                Display compiler version information.
  -dumpspecs               Display all of the built in spec strings.
  -dumpversion             Display the version of the compiler.
  -dumpmachine             Display the compiler's target processor.
  -foffload=      Specify offloading targets.
  -print-search-dirs       Display the directories in the compiler's search path.
  -print-libgcc-file-name  Display the name of the compiler's companion library.
  -print-file-name=   Display the full path to library .
  -print-prog-name=  Display the full path to compiler component .
  -print-multiarch         Display the target's normalized GNU triplet, used as
                           a component in the library path.
  -print-multi-directory   Display the root directory for versions of libgcc.
  -print-multi-lib         Display the mapping between command line options and
                           multiple library search directories.
  -print-multi-os-directory Display the relative path to OS libraries.
  -print-sysroot           Display the target libraries directory.
  -print-sysroot-headers-suffix Display the sysroot suffix used to find headers.
  -Wa,            Pass comma-separated on to the assembler.
  -Wp,            Pass comma-separated on to the preprocessor.
  -Wl,            Pass comma-separated on to the linker.
  -Xassembler         Pass  on to the assembler.
  -Xpreprocessor      Pass  on to the preprocessor.
  -Xlinker            Pass  on to the linker.
  -save-temps              Do not delete intermediate files.
  -save-temps=        Do not delete intermediate files.
  -no-canonical-prefixes   Do not canonicalize paths when building relative
                           prefixes to other gcc components.
  -pipe                    Use pipes rather than intermediate files.
  -time                    Time the execution of each subprocess.
  -specs=            Override built-in specs with the contents of .
  -std=          Assume that the input sources are for .
  --sysroot=    Use  as the root directory for headers
                           and libraries.
  -B            Add  to the compiler's search paths.
  -v                       Display the programs invoked by the compiler.
  -###                     Like -v but options quoted and commands not executed.
  -E                       Preprocess only; do not compile, assemble or link.
  -S                       Compile only; do not assemble or link.
  -c                       Compile and assemble, but do not link.
  -o                 Place the output into .
  -pie                     Create a dynamically linked position independent
                           executable.
  -shared                  Create a shared library.
  -x             Specify the language of the following input files.
                           Permissible languages include: c c++ assembler none
                           'none' means revert to the default behavior of
                           guessing the language based on the file's extension.

Options starting with -g, -f, -m, -O, -W, or --param are automatically
 passed on to the various sub-processes invoked by gcc.  In order to pass
 other options on to these processes the -W options must be used.

For bug reporting instructions, please see:
<file:///usr/share/doc/gcc-12/README.Bugs>.

more on debugging c

https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html

libraries: glibc:

The GNU C Library project provides the core libraries for the GNU system and GNU/Linux systems, as well as many other systems that use Linux as the kernel. These libraries provide critical APIs including ISO C11, POSIX.1-2008, BSD, OS-specific APIs and more. These APIs include such foundational facilities as open, read, write, malloc, printf, getaddrinfo, dlopen, pthread_create, crypt, login, exit and more.

The GNU C Library is designed to be a backwards compatible, portable, and high performance ISO C library. It aims to follow all relevant standards including ISO C11, POSIX.1-2008, and IEEE 754-2008.

The project was started circa 1988 and is more than 30 years old. You can see the complete project release history on the wiki.

Despite the project’s age there is still a lot to do so please Get Started and Get Involved!

https://www.gnu.org/software/libc/

howto download:

https://ftp.gnu.org/gnu/glibc/

Checkout the latest glibc in development:

git clone https://sourceware.org/git/glibc.git
cd glibc
git checkout master

manuals:

https://gcc.gnu.org/onlinedocs/

https://sourceware.org/gdb/current/onlinedocs/

single page html version https://sourceware.org/gdb/current/onlinedocs/gdb

pdf version: https://sourceware.org/gdb/current/onlinedocs/gdb.pdf <- “Debugging with gdb The gnu Source-Level Debugger Tenth Edition, for gdb version 15.0.50.20231203-git” (Richard Stallman, Roland Pesch, Stan Shebs, et al.)

books:

imho an excellent book https://openbook.rheinwerk-verlag.de/c_von_a_bis_z/

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin