print array contents to a file using gdb - c++

I am debugging a while loop using conditional breakpoints in gdb. There are multiple large arrays that are getting created in while loop. I would like to print them in a file while debugging so that I can compare using diff later.
I am able to visualize content at the console using the following command :
(gdb) p *&ff[0]#10
where ff is my array. Kindly tell how I can redirect them to text file.

You can use:
(gdb) set logging file large_array.txt
(gdb) set logging on
By default the logging file name is gdb.txt
You can find more details at: https://sourceware.org/gdb/onlinedocs/gdb/Logging-Output.html
There is also one WA gdb --args a.out arg1 ... |& tee gdb_out.txt

You set logging by using
(gdb) set logging on
after this, all command output will be output in a file called "gdb.txt". You can find the array content in the file.

Related

how to make gdb output only to the file

I'm not very into unix/linux, I'm using rhat linux with sh, tcsh shells.
What I'm trying to do is to debug lifecycle of the object of the class by breaking on it's default, copy c-tors, on d-tor and on operator=; move operations are not defined neither by compiler or me. I break on these functions and printf some lines and also print backtrace
br /project/src/some_file.c:408
commands
silent
printf "<%p> D E F A U L T c-tor bt:\n", this
bt
cont
end
The thing is there're a lot objects of this class, so there is a lot of output, and nothing helps me to disable output to the terminal, so I want see gdb output only in the file, not in the shell. Is it possible to achieve using sh or tcsh? - I can't really impact on the environment and use some other debugger or shell. The reason I want to disable any output from gdb and process being debugged to the shell is because I believe it slows down gdb and execution of the debugged process, which breaks behavior of debugged application.
Using gdb 8.1. I tried logging options of gdb, redirecting output by
run > somefile
and I tried to run gdb like this
gdb -p 1000 -x breakpoint.txt | tee somefile.txt
Thanks many times!
this link has various option for logging
http://sourceware.org/gdb/onlinedocs/gdb/Logging-Output.html
simple one is
set logging file file
Change the name of the current logfile. The default logfile is gdb.txt.
then
set logging on
Enable logging.

How to debug a C++ program that takes input from a script in gdb

Background info: The C++ program(LAMMPS - an open source) takes in a input script that has all the commands to be executed. The executable is named "lmp_fedora", input script named "in.hit". The program's run command "./lmp_fedora < in.hit"
My Problem: I am trying to debug one of the .cpp files in LAMMPS.
My Attempts: 1. I tried "gdb lmp_fedora < in.hit", but it failed. 2. Also tried to find the pid of the running program using ps aux, but wasn't sure which id it was.
My Question: How do you debug a input script(that has commands linked to c++ project) using gdb?
You use the gdb run command:
$ gdb lmp_fedora
(gdb) run <in.hit
From the help:
(gdb) help run
Start debugged program. You may specify arguments to give it.
Args may include "*", or "[...]"; they are expanded using "sh".
Input and output redirection with ">", "<", or ">>" are also allowed.
With no arguments, uses arguments last specified (with "run" or "set args").
To cancel previous arguments and run with no arguments,
use "set args" without arguments.
When you say gdb foo < bar that means bar is input to gdb, not to foo.
I think what you want to use is the gdb command set args.

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.

GDB logging file location

Hi I am new to GDB. I used the "set logging on" command. It creates a default file "gdb.txt" to store the output. However, I am unable to locate the file and see the data. Can you please tell me where the file is located?
In the current working directory. Try pwd command at the (gdb) prompt.
the gdb.txt file will not be created in pwd always.
It has to be explicitly specified by executing command 'set logging on' in gdb terminal.

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.