Invoke gdb to automatically pass arguments to the program being debugged

前端 未结 8 1795

I\'d like to write a script that (under certain conditions) will execute gdb and automatically run some program X with some set of arguments Y. Once the program has finishe

相关标签:
8条回答
  • 2020-12-23 00:22

    If you want to run some commands through GDB and then have it exit or run to completion, just do

    echo commands | gdb X
    

    If you want to leave it at the command prompt after running those commands, you can do

    (echo commands; cat) | gdb X
    

    This results in echoing the commands to GDB, and then you type into the cat process, which copies its stdin to stdout, which is piped into GDB.

    0 讨论(0)
  • 2020-12-23 00:24

    After trying all of the answers here,

    1. The echo/cat hack, while clever, breaks quite a few important features of gdb. Most notably, all user prompts are answered automatically (so you don't get a chance to confirm potentially dangerous operations), and Ctrl+C (to stop a process that you are debugging) ends up killing cat, so you can't actually talk to gdb after that.
    2. The -x option is supposed to work, but I couldn't get it to work with my version of gdb, and it requires a temporary file.

    However, it turns out you can just use -ex, like this:

    gdb -ex "target remote localhost:1234"
    

    You can also specify -ex multiple times to run multiple commands!

    0 讨论(0)
  • 2020-12-23 00:28

    The easiest way to do this given a program X and list of parameters a b c:

    X a b c
    

    Is to use gdb's --args option, as follows:

    gdb --args X a b c
    

    gdb --help has this to say about --args:

    --args Arguments after executable-file are passed to inferior

    Which means that the first argument after --args is the executable to debug, and all the arguments after that are passed as is to that executable.

    0 讨论(0)
  • 2020-12-23 00:29

    there is option -x, e.g.

    gdb -x gdb_commands exe_file
    

    where gdb_commands can be for example (in the case of android emulator):

    target remote :5039
    
    0 讨论(0)
  • 2020-12-23 00:30
    gdb target -e "my-automation-commands"
    

    my-automation-commands containing anything you would normally want to run,

    break 0x123
    set args "foo" bar 2
    r
    

    Not strictly a temp file, if you have a few standard init scripts ;)

    0 讨论(0)
  • 2020-12-23 00:37

    With bash you can create a script that give user like input to any executable you're executing:

    #!/bin/sh
    gdb X <<GDB_INPUT
    pwd
    run X a b c
    quit
    GDB_INPUT
    
    0 讨论(0)
提交回复
热议问题