How to read and execute GDB commands from a file? - gdb

I run GDB on object file (e.g exeFile) and I want to examine it according to several commands . How can I execute these commands according to lines in a file (instead input these each GDN running) ?
For example -
I want to set break in -
break *0x8048e19
break *0x8048e32
break *0x8048e6f
break *0x8048e90
so I want to save them in a file and then tell the GDB execute them from this file.

write the commands in a file and execute gdb with -x switch
gdb -x command_file_name
or run the gdb source command while gdb is running if you dont want to specify a command file from command line
>source [-s] [-v] command_file_name

Related

Is it possible to pass parameters to GDB as the command line?

I am running someone's code and the code crashes on a specific dataset, but the crash message as-is is not informative. I cannot call GDB and then wait (for GDB) to pass r to it (things are running on a cluster). Is there a way to pass r to GDB in the runtime by default, something like the following?
gdb r ./run
You can load your program in GDB, like:
$ gdb your_program_name
and then attach to process which you want to debug
attach pid
You may use a commands file. Write all the commands you want to execute sequentially in the file. And use that file to pass the command to GDB with input redirection:
echo "r" > cmds
gdb r --args ./run -arg1 -arg2 -arg3 < cmds

Command line option to run program under gdb after it loads?

Whe loading a program with gdb, how do you have gdb automatically start the program and run it without waiting?
Use the -ex command line option and provide the run command.
gdb -ex=r --args executable --with-options-for-the-executable

How to use gdb to reverse engineer an ELF which runs another program?

I am a beginner and got some trouble in RE.
I have an ELF 'bomb' and an unknown file 'model.abc'.
The correct way to run bomb is:
bomb model.abc
Now I want to use gdb to see the value of some addresses when running it. Can any one help me?
First start gdb from a shell prompt:
$ gdb bomb
Then run your program from the (gdb) prompt with the command line you want:
(gdb) run model.abc
You need to launch your program this way because gdb doesn't allow you to specify command line arguments for your program on the gdb command line.
Another, more convenient way of debugging a program with arguments:
gdb --args program <arguments>
If you don't have symbols, you'll have to start from the entry point. To figure our where it is, use:
(gdb) info file
Symbols from "/.../tesprog".
Local exec file:
`/.../tesprog', file type elf32-i386.
Entry point: 0x804abc0
Then you can set breakpoint on it before running:
break *0x804abc0
Note that the entry will be most often the library startup code (ctr0.s), it might take a while to get to the actual code written by the programmer.

Is there a way to let the gdb repeat the same instrcutions on every start again?

I am currently debugging a program with gdb.
I have to start gdb over and over again and do the same steps:
set a breakpoint,
run,
print a variable,
quit
Is there a way to let gdb do that automatically for me? Probably a script that could be attached as a parameter?
Thanks in advance!
You can do it either by -x file option or by -ex command option. From Gdb manual:
-command file
-x file
Execute commands from file file. The contents of this file is evaluated exactly as the source command would. See Command files.
-eval-command command
-ex command
Execute a single gdb command.
This option may be used multiple times to call multiple commands. It may also be interleaved with `-command' as required.
gdb -ex 'target sim' -ex 'load' \
-x setbreakpoints -ex 'run' a.out
The interwebs differ on whether the name of the file is .gdbrc or .gdbinit, but GDB will read this file from your home directory on start-up, and it can give any GDB command (including setting breakpoints).
Also check out http://www.andrews.edu/~seidel/gdb.help

How to dump the entire GDB session to a file, including commands I type and their output?

In bash, I can use the script command, which dumps everything that shows on the shell to a file, including:
commands typed
PS1 line
stdout and stderr of commands
What is the equivalent in gdb?
I tried to run shell script from inside GDB, but after I hit return, I was in the shell and lost the shell prompt and could not run command any more. Moreover I could not use ctrl+c or ctrl+\ to exit. I needed to force kill the /bin/login tty2 to exit.
If you want to log GDB's output, you can use the GDB logging output commands, eg.
set logging file mylog.txt
set logging on
If you want to redirect your program's output to a file, you can use a redirect, eg.
run myprog > mylog.txt
see the chapter on program IO in the GDB manual for more information
Create a text file, i.e. gdbCommands.txt, with the following commands
set logging on my_log_file\nbt 10\nq
bt 10, indicates the number of lines (function calls) we need from the backtrace, in our example is 10 lines.
Execute gdb using the following command, assuming a core dump file core.2345
gdb -x gdbCommands.txt myApp core.2345
Open my_log_file and inspect backtrace!
howto-redirect-gdb-backtrace-output-to-a-text-file
I have logging enabled using:
set trace-commands on
set pagination off
set logging file $log
and show logging reports (to both to terminal and file):
+show logging
Currently logging to mylog.
Logs will be appended to the log file.
Output will be logged and displayed
If I print the value of a variable that also gets logged (to both to terminal and file):
+p myvar
$2 = 0
But if I do command like where or “info b” all I get logged to the file is:
+where
+info b
Anyone know why or how to fix it?
Have a look at the GDB documentation. Search for "Canned Sequences of Commands". There is a way to save GDB commands in a file and run them with the source command and you can use some GDB commands in these scripts to print information available to GDB (like echo, output and printf).
If you want that output in a file, use set logging file FILE.