gdb Brief Primer on

advertisement

Brief Primer on

gdb

Getting started . Assume our C program is named test.c

. The program is shown below.

#include<stdio.h> int main( )

{

int a = 2;

int b = 1000;

char x = '$' ;

char phrase[4] = "Fun" ;

printf( "Yes");

printf("No");

}

To run the debugger on the compiled version of test.c

, always start by entering: gcc –g test.c gdb –q ./a.out set dis intel list

If your source code is more than 10 lines, you may have to hit enter again, to list the next 10 lines. We see this: midshipman@EC310-VM:~ $ gcc -g test.c midshipman@EC310-VM:~ $ gdb -q ./a.out

Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".

(gdb) set dis intel

(gdb) list

1 #include<stdio.h>

2 int main( )

3 {

4 int a = 2;

5 int b = 1000;

6

7 char x = '$' ;

8

9 char phrase[4] = "Fun" ;

10

(gdb)

11 printf( "Yes");

12

13 printf("No");

14 }

(gdb)

1

The line numbers shown on the left can be very useful for setting breakpoints. For example, if I wanted to run the program but have it pause right between the two final printf statements, I would enter break 12 run

(You have already seen us set a breakpoint at main by entering break main … this is the same idea.)

Looking at Memory Based on our program, we should have the following items stored in memory:

2

1000

'$'

"Fun"

The strings "Yes" and "No" are also in memory somewhere, but we'll concentrate just on the integers 2 and

1000, the character '$' and the string "Fun" .

If I were to look into memory, I would see this (where all values are hexadecimal):

Looking at this section of memory, it may not be obvious where items are stored. Here is where the integers 2 and 1000, the character '$' and the string "Fun" are placed:

2

The string "Fun"

Consulting the handy-dandy ASCII table, we see:

Character

F u n

ASCII hexadecimal value

46

75

6e

So, sure enough, there it is at memory location bffff808 . You should also note that the NULL terminator appears as the character immediately following the 'n' in "Fun" .

Looking at the memory on the bottom of the previous page, try to guess what will be displayed by each of the following commands. (The answers immediately follow.)

(a) x/xb 0xbffff808

(b) x/xh 0xbffff808

(c) x/xw 0xbffff808

(d) x/xs 0xbffff808

Answers:

(a) 46 (displays a byte)

(b) 7546 (displays two bytes)

(c) 006e7546 (displays four bytes)

(d) "Fun" (displays as a string)

For (b) and (c), note the annoying little-endian.

We can specify the number of units we wish to have printed out by placing a number after the slash. For example, looking at the memory on the bottom of the previous page, try to guess what will be displayed by each of the following commands. (The answers immediately follow.)

(a) x/xb 0xbffff808

(b) x/2b 0xbffff808

(c) x/3b 0xbffff808

(d) x/4b 0xbffff808

(e) x/2h 0xbffff808

Answers:

(a) 46 (displays a byte)

(b) 0x46 0x75 (displays two bytes)

(c) 0x46 0x75 0x6e (displays three bytes)

(d) 0x46 0x75 0x6e 0x00 (displays four bytes)

(e) 0x7546 0x006e (notice that each half-word is presented in annoying little-endian)

3

If we suspect that characters are being stored, we can ask that the display be presented as characters by specifying the c format. Looking at the memory on the bottom of the previous page, try to guess what will be displayed by each of the following commands. (The answers immediately follow.)

(a) x/c 0xbffff808

(b) x/2c 0xbffff808

(c) x/3c 0xbffff808

(d) x/4c 0xbffff808

Answers:

(a) 70 'F'

(b) 70 'F' 117 'u'

(c) 70 'F' 117 'u' 110 'n'

(d) 70 'F' 117 'u' 110 'n' 0 '\0

Suppose we thought an integer was stored at address 0xbffff808 . We could check this be entering x/dw 0xbffff808

If we do this, we see:

0xbffff808: 7238982

Can you guess where on Earth this value 7238982 comes from?

Answer:

We saw earlier that entering: x/xw gave us 006e7546 . If we convert the hexadecimal value 006e7546 to a decimal integer, we find its value is 7238982.

The character $

Looking at the bottom of page 2, we see the $ character is stored at location bffff80f .

Looking at the memory on the bottom of page 2, try to guess what will be displayed by each of the following commands. (The answers immediately follow.)

(a) x/xb 0xbffff80f

(b) x/c 0xbffff80f

(c) x/db 0xbffff80f

(d) x/s 0xbffff80f

Answers:

(a) 0x 24

(b) 36 '$'

(c) 36

(d) $ � \003

Note that 0x24 equals 36

10

, and that that the last item is gibberish because a string is not stored in this location.

4

The integer 1000

So, first, we should convert the decimal value of 1000 to hexadecimal. If we do this, we find it is equal to

0x3e8 .

With reference to the bottom of page 2, answer the following questions.

(a) Why is 1000 stored in four bytes if it only needs two bytes?

(b) Presuming this value does take four bytes, and thus is equal to 0x000003e8 , why is it not stored with the leading two zeros at the "top" memory locations?

Answers:

(a) All integers are stored in four bytes, even if fewer are needed.

(b) Little endian, little endian, little endian.

Looking at the memory on the bottom of page 2, try to guess what will be displayed by each of the following commands. (The answers immediately follow.) x/xb 0xbffff810 x/xh 0xbffff810 x/d 0xbffff810 x/2c 0xbffff810

Answers:

(a) 0xe8

(b) 0x03e8

(c) 1000

(d) gibberish

So… do you think you get it? To see, try this:

Your friend types x/d 0xbffff813 and sees that the result is 512 . Explain!

Assistant Professor Patrick Vincent

Help us improve these notes! Send comments, corrections and clarifications to vincent@usna.edu

5

Download