Gdb print to file instead of stdout

后端 未结 8 1994
醉话见心
醉话见心 2020-11-29 17:15

I am running gdb and want to examine one of those unfortunate god objects. It takes many pages (and I have a 24\" monitor turned sideways!) to see the whole thing. For eas

相关标签:
8条回答
  • 2020-11-29 17:40

    A simple method to log gdb to a file while still seeing the output (which eases writing commands) is to use tee:

    gdb command |& tee gdb.log
    
    0 讨论(0)
  • 2020-11-29 17:40

    Although there are many good answers here, I still have to post the only thing that worked for me:

    [niko@my-laptop]# gdb MyBinary 2>&1 log.txt
    

    This was the only way to get gdb and binary output into the same log.txt file, while also seeing it on the console.

    EDIT:

    Caution: Output seems to be partially not synced among the gdb output and the binary output. Can someone confirm? You might want to check whether your telnet/ssh client has a function to log the output that you see in your console.

    0 讨论(0)
  • 2020-11-29 17:43

    You've had multiple answers here. They are corrects. I just want to add a command that will help you to collect all the output at once. This really helpful when your collecting a huge backtrace. Before doing any logging configuration, do this:

    (gdb)set height 0
    

    I found it on this article: https://askaralikhan.blogspot.com/2016/05/gdb-all-threads-bt-to-file.html?showComment=1584614942454#c4584028195226351332

    0 讨论(0)
  • 2020-11-29 17:48

    From https://sourceware.org/gdb/onlinedocs/gdb/Logging-Output.html:

    You may want to save the output of gdb commands to a file. There are several commands to control gdb's logging.

    set logging on
    

    Enable logging.

    set logging off
    

    Disable logging.

    set logging file file
    

    Change the name of the current logfile. The default logfile is gdb.txt.

    set logging overwrite [on|off]
    

    By default, gdb will append to the logfile. Set overwrite if you want set logging on to overwrite the logfile instead.

    set logging redirect [on|off]
    

    By default, gdb output will go to both the terminal and the logfile. Set redirect if you want output to go only to the log file.

    show logging
    

    Show the current values of the logging settings.

    0 讨论(0)
  • 2020-11-29 17:50

    I've found that you can redirect the output from gdb to a file via the run command:

    (gdb) run > outfile
    
    0 讨论(0)
  • 2020-11-29 17:54

    You need to enable logging.

    (gdb) set logging on
    

    You can tell it which file to use.

    (gdb) set logging file my_god_object.log
    

    And you can examine current logging configuration.

    (gdb) show logging
    
    0 讨论(0)
提交回复
热议问题