Clemson University -- CPSC 231 -- Fall 2009 gdb commands (section 2.7; also, command list on p. 89) q -- quit r -- run b -- set breakpoint c -- continue from breakpoint x -- examine (needs /) x/i $pc p -- print register contents l -- list ten source lines n or ni -- single step execution s or si -- single step execution, follows calls disassemble display -- done at each prompt commands -- done at specific breakpoint pressing return repeats last command .gdbinit -- file contains start-up commands example program .global main main: save %sp,-96,%sp mov 9, %l0 sub %l0, 1, %o0 sub %l0, 7, %o1 call .mul nop sub %l0, 0xb, %o1 call .div nop mov %o0, %l1 mov 1, %g1 ta 0 compile using "gcc -gstabs" (or "gcc -gstabs -static" if you want to follow calls to the linked routines) % gdb a.out GNU gdb 6.1.1 Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "sparc-sun-solaris2.9"...No symbol table is loaded. Use the "file" command. (gdb) l <- list ten lines 1 .global main 2 main: 3 save %sp,-96,%sp 4 mov 9, %l0 5 sub %l0, 1, %o0 6 sub %l0, 7, %o1 7 call .mul 8 nop 9 sub %l0, 0xb, %o1 10 call .div (gdb) <- list next ten lines 11 nop (or until program end) 12 mov %o0, %l1 13 mov 1, %g1 14 ta 0 (gdb) disassemble No frame selected. (gdb) b main <- set breakpoint at "main" Breakpoint 1 at 0x10260: file gtest.s, line 3. (gdb) r Starting program: /users/mark/231/a.out Breakpoint 1, main () at gtest.s:4 <- has executed save, now 4 mov 9, %l0 ready to execute mov Current language: auto; currently asm (gdb) disassemble Dump of assembler code for function main: 0x0001025c : save %sp, -96, %sp 0x00010260 : mov 9, %l0 0x00010264 : sub %l0, 1, %o0 0x00010268 : sub %l0, 7, %o1 0x0001026c : call 0x105cc <.mul> 0x00010270 : nop 0x00010274 : sub %l0, 0xb, %o1 0x00010278 : call 0x105f0 <.div> 0x0001027c : nop 0x00010280 : mov %o0, %l1 0x00010284 : mov 1, %g1 0x00010288 : ta 0 End of assembler dump. (gdb) n <- next assembly language 5 sub %l0, 1, %o0 source line (gdb) 6 sub %l0, 7, %o1 (gdb) 7 call .mul (gdb) 8 nop (gdb) 9 sub %l0, 0xb, %o1 (gdb) 10 call .div (gdb) 11 nop (gdb) 12 mov %o0, %l1 (gdb) 13 mov 1, %g1 (gdb) 14 ta 0 (gdb) Program exited with code 0370. (gdb) display $o1 <- set up display list to (gdb) display $o0 be done at each prompt (gdb) display $l1 (will be done in reverse (gdb) display $l0 order of entering) (gdb) r Starting program: /users/mark/231/a.out Breakpoint 1, main () at gtest.s:4 <- ready to execute mov; 4 mov 9, %l0 current values of source 10: $l0 = 352936 registers listed -- note 9: $l1 = 359300 that %l0 doesn't yet have 8: $o0 = 359212 the value 9 7: $o1 = 0 (gdb) n 5 sub %l0, 1, %o0 <- ready to execute sub; 10: $l0 = 9 %l0 now has the value 9 9: $l1 = 359300 8: $o0 = 359212 7: $o1 = 0 (gdb) 6 sub %l0, 7, %o1 10: $l0 = 9 9: $l1 = 359300 8: $o0 = 8 7: $o1 = 0 (gdb) 7 call .mul <- ready to call .mul with 10: $l0 = 9 parameters %o0 = 8 and 9: $l1 = 359300 %o1 = 2 8: $o0 = 8 7: $o1 = 2 (gdb) 8 nop <- nop in delay slot 10: $l0 = 9 9: $l1 = 359300 8: $o0 = 8 7: $o1 = 2 (gdb) 9 sub %l0, 0xb, %o1 <- .mul returns with return 10: $l0 = 9 value of 16 in %o0 9: $l1 = 359300 8: $o0 = 16 7: $o1 = 0 (gdb) 10 call .div <- ready to call .div with 10: $l0 = 9 parameters %o0 = 16 and 9: $l1 = 359300 %o1 = -2 8: $o0 = 16 7: $o1 = -2 (gdb) 11 nop 10: $l0 = 9 9: $l1 = 359300 8: $o0 = 16 7: $o1 = -2 (gdb) 12 mov %o0, %l1 <- .div returns with return 10: $l0 = 9 value of -8 in %o0 9: $l1 = 359300 8: $o0 = -8 7: $o1 = -2 (gdb) 13 mov 1, %g1 <- code moves -8 into %l1 10: $l0 = 9 as last step and then is 9: $l1 = -8 ready to trap back to shell 8: $o0 = -8 7: $o1 = -2 (gdb) 14 ta 0 10: $l0 = 9 9: $l1 = -8 8: $o0 = -8 7: $o1 = -2 (gdb) Program exited with code 0370. (gdb) quit program from pp. 72 C version #define A2 1 #define A1 7 #define A0 11 main(){ int x,y; x = 0; do{ y = ((x - A2) * (x - A1)) / ( x - A0); x++; }while (x < 11); } assembly language version, with m4 macros (bottom p. 72 and top p. 73) /* This programs computes the expression: y = (x - 1) * (x - 7) / (x - 11) The polynomial coefficients are: */ define(a2, 1) define(a1, 7) define(a0, 11) /* Variables x and y are stored in %l0 and %l1 */ define(x_r, l0) !`%l0 is x_r' define(y_r, l1) !`%l1 is y_r' .global main main: save %sp,-96,%sp clr %x_r !initialize x to zero .global loop loop: !the do loop sub %x_r, a2, %o0 !(x - a2) into %o0 sub %x_r, a1, %o1 !(x - a1) into %o1 call .mul !.mul does %o0 = %o0 * %o1 nop sub %x_r, a0, %o1 !(x - a0) into %o1, the divisor call .div !.div does %o0 = %o0 / %o1 nop mov %o0, %y_r !store it in y, add %x_r, 1, %x_r !x++ subcc %x_r, 11, %g0 !set condition codes bl loop nop mov 1, %g1 !trap dispatch ta 0 !trap to system after processing by m4 (see p. 74) /* This programs computes the expression: y = (x - 1) * (x - 7) / (x - 11) The polynomial coefficients are: */ /* Variables x and y are stored in %l0 and %l1 */ !%l0 is x_r !%l1 is y_r .global main main: save %sp,-96,%sp clr %l0 !initialize x to zero .global loop loop: !the do loop sub %l0, 1, %o0 !(x - 1) into %o0 sub %l0, 7, %o1 !(x - 7) into %o1 call .mul !.mul does %o0 = %o0 * %o1 nop sub %l0, 11, %o1 !(x - 11) into %o1, the divisor call .div !.div does %o0 = %o0 / %o1 nop mov %o0, %l1 !store it in y, add %l0, 1, %l0 !x++ subcc %l0, 11, %g0 !set condition codes bl loop nop mov 1, %g1 !trap dispatch ta 0 !trap to system examine memory contents using gdb "x/16x main" 0x10a98
: 0x9de3bfa0 0xa0100000 0x90242001 0x92242007 0x10aa8 : 0x40004302 0x01000000 0x9224200b 0x40004302 0x10ab8 : 0x01000000 0xa2100008 0xa0042001 0x80a4200b 0x10ac8 : 0x06bffff6 0x01000000 0x82102001 0x91d02000 "x/16i main" 0x10a98
: save %sp, -96, %sp 0x10a9c : mov %g0, %l0 0x10aa0 : sub %l0, 1, %o0 0x10aa4 : sub %l0, 7, %o1 0x10aa8 : call 0x216b0 <.mul> 0x10aac : nop 0x10ab0 : sub %l0, 0xb, %o1 0x10ab4 : call 0x216bc <.div> 0x10ab8 : nop 0x10abc : mov %o0, %l1 0x10ac0 : inc %l0 0x10ac4 : cmp %l0, 0xb 0x10ac8 : bl 0x10aa0 0x10acc : nop 0x10ad0 : mov 1, %g1 0x10ad4 : ta 0