how to make gdb output only to the file - c++

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.

Related

VSCode C++ debug capture console output

I am running VSCode in Ubuntu to debug a C++ program. Debugging a console app with GDB is working fine except I really want to capture the console log output to a file. I cannot see a way or option to do this. Is there any option to capture this console log output?
Since there does not seem to be a native feature to save the output of a VSCode terminal, maybe you can use in said terminal a command allowing you to save that session.
See for instance "Gdb print to file instead of stdout"
gdb core.3599 -ex bt -ex quit |& tee backtrace.log
As mentioned, the output is written to backtrace.log and also on the screen.
As the OP Andy Tomlin mentions in the comments, this is not compatible with a debugger session.
We solved the problem by just handling it inside the app and redirecting cout internally to a file.

How to only output warnings when launching a program under gdb?

I have a program which i am trying to debug. When the program is started under gdb, there are several warnings being outout, but then those warnings are hidden under the output of the program.
How can one ensure that only warnings are shown and remain avalable to be read?
If you're asking how to suppress output from your program, then the answer is to do it exactly as you'd do it in the shell. gdb actually uses the shell to start the inferior when you type run, so some kinds of redirection work.
In this case, to suppress all output, you could do something like:
(gdb) run > /dev/null 2>&1
I'm assuming here that the warnings are things gdb is printing that you want to see. Unfortunately gdb doesn't remember what it has printed, so there's no way to ask it after the fact.

GDB and NS2: how to stop program at some Function call

I am using gdb to debug NS-2 which is a simulator for network protocols. It takes an .tcl file as input and interpret it. [I think it is an interpreter.]
Some of the code is written in tcl (events and creation of network components) and some in C++ (especially Packet Formats, Agents etc.).
I have created an Agent in C++ and i want to stop it at some function call so that i can see the stack trace and find which other classes have been called before it.
This is what i have done:
There was some error in one of my MyAgent::function and it was giving Segmentation Fault and gdb was stopping there automatically. I could then see the stack trace. I rectified the error.
Now when i run
gdb ./ns
b MyAgent::function()
/*
When i press TAB after writing "b MyA" it gives me all functions
of my class :). when i press enter after above command --
it asks me "Breakpoint on future shared library load" and i say Yes.
I hope this is ok ??
*/
r myfiles/myWireless.tcl
Now it runs and do not stop anywhere. :(
I am sure that this function is being called, because when that Segmentation fault was occuring, it was stopping at that function.
Thanks
You can add a breakpoint in that function:
(gdb) break MyAgent::function()
You must make sure to compile with whatever options are necessary to get debug symbols. On GCC, use the -g or -ggdb options.
You need the -args option to specify the tcl script that will be executed.
Run gdb like this:
gdb -args ./ns path/to/tcl/script.tcl
To enable debug flag to c++ code, if have not done it already, re-configure your ns2 instalation with:
./configure --enable-debug ;# plus any other flags you use for configuring
make clean
make -j 3 ;# -j for faster compiling
make install ;# optional
You can also use the --with-tcldebug=..., for debugging tcl code (You need to install tcldebug first for this option)

Input redirection in gdb (MinGW)

I'm trying to get gdb to run programs with input redirection to stdin. For example, without gdb I would run a program like this:
prog < input.txt
Now in gdb, the usual way to do this is run < input.txt. However, it doesn't work for me and when doing this nothing gets redirected into stdin.
I'm using Windows with MinGW. What could be the problem?
As far back as the late '90s, broken command line redirection was a known and assumed limitation. My suspicion is that it remains that way, since the mingw32 port of gdb still gleefully passes on verbatim all run arguments (including redirects) to the debugee.
Several possible workarounds:
if you have the option to alter the command line interface, then implement bbadour's suggestion
otherwise, if you can easily suspend the process before the point you want to debug at, invoke the debugee (with redirection) from a shell and attach to it while it is already running
otherwise, if you have symbols for the debugee (gcc -g) or you know the address of main() (gcc -Wl,-Map,mapfile) and can set a breakpoint there, proceed in the following manner (tested with mingw gdb 6.8.0):
# gdb debugee.exe
(gdb) b main
(gdb) run non-redirect-arguments-if-any
(gdb) p dup2(open("/tmp/input.txt", 0), 0)
(gdb) c
I ran into the same issue here, and I just got into the habit of adding a command-line argument to allow grabbing input from a file.
e.g. Parsing a "-i ifile" argument using argc and argv to get input from ifile instead of stdin and parsing a "-o ofile" to write output to ofile instead of stdout.
Then I just use those arguments instead of redirects.
The tools that come with MinGW often are not the latest versions and often have features omitted. ::shrug::
Input redirection is supported starting with GDB 8.0. From the NEWS file:
Native debugging on MS-Windows supports command-line redirection
Command-line arguments used for starting programs on MS-Windows
can now include redirection symbols supported by native Windows
shells, such as '<', '>', '>>', '2>&1', etc. This affects GDB
commands such as "run", "start", and "set args", as well as the
corresponding MI features.

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.