Multiple commands in gdb separated by some sort of delimiter ';'?

前端 未结 6 1409
故里飘歌
故里飘歌 2020-12-12 12:53

I am trying to execute two commands at once in gdb:

finish; next

I tried using the \';\' to separate the commands but gdb did not let me do

6条回答
  •  温柔的废话
    2020-12-12 13:05

    Certainly it is possible. Given, for example, C code

    int a = 3;
    double b = 4.4;
    short c = 555;
    

    , say we want to ask GDB what is the type of each of those variables.  The following sequence of GDB commands will allow us to enter 3 whatis requests all on a single line:

    1. set prompt #gdb#
      • Any prompt whose first non-whitespace character is # will work: it just so happens that # starts a comment in GDB command scripts.
    2. set logging overwrite on
      • By default, GDB appends to a log file; choosing to instead overwrite will let us easily deploy this trick again later, with different commands.
    3. set logging redirect on
      • Meaning, save output of typed commands to log file only: do not also show it at the terminal. (Not absolutely required for our purposes, but keeps the clutter down.)
    4. set logging on
      • This causes GDB to start actually logging; by default, the log file is called gdb.txt.
    5. printf "\nwhatis a\nwhatis b\nwhatis c\n"
      • Our 3 whatis requests, entered on a single line as promised!  Separating the commands, before the first, and after the last is \n.
    6. set logging off
      • Done writing to gdb.txt; that file now contains a valid GDB command script:
       #gdb#
       whatis a
       whatis b
       whatis c
       #gdb#
    
    1. source gdb.txt
      • GDB now executes commands in the script which it just generated, producing the expected results:
    type = int
    type = double
    type = short
    

    Notes

    • Should you wish to deploy this trick again in the same GDB session, just perform steps 4-7.
    • Generating a command script with shell would be less cumbersome, and may well be possible; the above method, however, is platform-agnostic.

提交回复
热议问题