How do I run a program with commandline arguments using GDB within a Bash script?

后端 未结 8 2025
半阙折子戏
半阙折子戏 2020-11-27 23:52

When running a program on GDB, usually, the arguments for the program are given at the run command. Is there a way to run the program using GDB and as well as g

相关标签:
8条回答
  • 2020-11-28 00:31

    Another way to do this, which I personally find slightly more convenient and intuitive (without having to remember the --args parameter), is to compile normally, and use r arg1 arg2 arg3 directly from within gdb, like so:

    $ gcc -g *.c *.h
    $ gdb ./a.out
    (gdb) r arg1 arg2 arg3
    
    0 讨论(0)
  • 2020-11-28 00:32

    If the --args parameter is not working on your machine (i.e. on Solaris 8), you may start gdb like

    gdb -ex "set args <arg 1> <arg 2> ... <arg n>"
    

    And you can combine this with inputting a file to stdin and "running immediatelly":

    gdb -ex "set args <arg 1> <arg 2> ... <arg n> < <input file>" -ex "r"
    
    0 讨论(0)
  • 2020-11-28 00:36

    You could create a file with context:

    run arg1 arg2 arg3 etc
    
    program input
    

    And call gdb like

    gdb prog < file
    
    0 讨论(0)
  • 2020-11-28 00:36

    In addition to the answer of Hugo Ideler. When using arguments having themself prefix like -- or -, I was not sure to conflict with gdb one.

    It seems gdb takes all after args option as arguments for the program.

    At first I wanted to be sure, I ran gdb with quotes around your args, it is removed at launch.

    This works too, but optional:

    gdb --args executablename "--arg1" "--arg2" "--arg3"
    

    This doesn't work :

    gdb --args executablename "--arg1" "--arg2" "--arg3" -tui
    

    In that case, -tui is used as my program parameter not as gdb one.

    0 讨论(0)
  • 2020-11-28 00:43
    gdb -ex=r --args myprogram arg1 arg2
    

    -ex=r is short for -ex=run and tells gdb to run your program immediately, rather than wait for you to type "run" at the prompt. Then --args says that everything that follows is the command and arguments, just as you'd normally type them at the commandline prompt.

    0 讨论(0)
  • 2020-11-28 00:46

    gdb has --init-command <somefile> where somefile has a list of gdb commands to run, I use this to have //GDB comments in my code, then `

    echo "file ./a.out" > run
    grep -nrIH "//GDB"|
        sed "s/\(^[^:]\+:[^:]\+\):.*$/\1/g" |
        awk '{print "b" " " $1}'|
        grep -v $(echo $0|sed "s/.*\///g") >> run
    gdb --init-command ./run -ex=r
    

    as a script, which puts the command to load the debug symbols, and then generates a list of break commands to put a break point for each //GDB comment, and starts it running

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