In order to run executable file with gdb with initializations, specify a initialization file '.gdbinit',
which takes up the normal initializations.
Simple .gdbinit file:
$ cat .gdbinit
1 file a.out
2 set args
3 b a.c:451
4 b a.c:455
Now gdb can be run without any filenames or parameters as,
$gdb or
$gdb -tui (Terminal User Interface)
This runs the program within gdb by setting breakpoints and arguments (none here) as specified
in .gdbinit. Inputs can also be initialized, from other files too, using .gdbinit.
Create a file named “input”, using an editor indeed, which contains the initial value inputs. Pass it
as an argumment to .gdbinit as
set args < input (line 2 of previous example)
Commands can also be initialized as:
b r.c:451
p *t
.
.
run
GDB thus initialises inputs, breakpoints and also commands that are used frequently using the
.gdbinit. .gdbinit can be used to such an extent that even macros, functions can be used.
Consider this “input” and .gdbinit files for some program and hope for some logical errors in source
code. I am `cat`ting them as,
$cat input
1 268
25
3B
4 18000
Let this be an input that initializes ID, semester, section and fee.
$cat .gdbinit
1 file semester.out
2 set args < input
3 b semester.c:135
4 b semester.c:534
5 b semester.c:73 if (fee == 125000)
.
.
13 p *student
I hope its self explanatory except line 5, which breaks only if the condition is satisfied.
SJCE/CS&E/VikramTV 1
$gdb or
$gdb -tui
gdb starts executing semester.out. It provides inputs from file input, creates breakpoints and starts
running. The required instruction to debug, in a huge code, can be reached in no time with proper
initializations with .gdbinit.
There is no manual page for .gdbinit. You may need to refer external sources for further help.
Defining macros or funcitons or loopings in .gdbinit are same as with shell programming.
Using GDB under GNU Emacs:
Emacs is an environment. It can be used to write source codes, run it and also debug within it and if
you are bored, you can even play some simple games within it. It uses buffers to handle tasks.
Emacs uses Escape, Control (C) and Meta (M – Meta key or Edit key or Alt key) as the basic tools.
Every shortcut is prefixed with either C or M or their combination. Emacs works using buffers.
To start using Emacs, ofcourse Emacs should be installed, type:
$emacs filename (starts the Emacs window)
Press C-p and write or edit the source code.
Save it using C-x C-s combination or the dropdown menus can be used.
M-! takes to shell.
Compile source code under a suitable directory using
gcc -g filename
Press
Emacs can now be used to debug with gdb. If there is a .gdbinit file, Emacs starts debugging with
initializations, else the executable should be provided. Debugging commands using gdb within
Emacs are the same.
To quit using Emacs, press C-x C-c.
