GDB examples

0.  Compile prog.c with gdb support: 

gcc -g prog.c -o prog

1.  Start gdb in emacs:

M-x gdb <return> prog

2.  Set a break point:

b <position>

    Here <position> can be a line number or a function name.

3.  Run prog with arguments:

r prog arg1 arg2 …

    It will stop at the break point. To set another break point,
    repeat 2 or alternatively, switch to the source code buffer, move
    cursor to the line you want to set a break point, and then press
    C-x <space>. After this you may run command &quot;c&quot; to proceed to the
    next break point.

    To step forward just one line of code, use command &quot;n&quot;; more than
    one lines (5 lines for example): &quot;n 5&quot;

4.  Check variable values:

p <expr>

    Here <expr> is a valid C expression.  Usually, gdb will choose the
    right format for you automatically.  To print out with specific
    format, you should use:

p /<format> <expr>

    Here <format> can be:
    d:   int type
    u:   unsigned int type
    c:   (single) character type
    f:   floating point type

x /<length><format> pointer

    Here pointer is a pointer to a variable. Notably we can use this
    to print out a string:

    x /s string

ptype <expr>

   Shows the type of <expr>

Example:

int a = 51;
double x[3] = {1.1, 2.2, 3.3};

p a will print out 51;
p x[1]  will print out 3.3;
x &amp;a is roughly equivalent to p a;
x /3f x prints all the elements of x.

, , ,

  1. 发表评论

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: