DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
adb: absolute debugger

An example: tracing multiple functions

The following example illustrates how to execute a program under adb control. In particular, it shows how to set breakpoints, start the program, and examine registers and memory. The program to be examined has the following source statements:

      int	fcnt,gcnt,hcnt;
      h(x,y)
      {
      	int hi; register int hr;
      	hi = x+1;
      	hr = x-y+1;
      	hcnt++ ;
      	hj:
      	f(hr,hi);
      }
   

g(p,q) { int gi; register int gr; gi = q-p; gr = q-p+1; gcnt++ ; gj: h(gr,gi); }

f(a,b) { int fi; register int fr; fi = a+2*b; fr = a+b; fcnt++ ; fj: g(fr,fi); }

main() { f(1,1); }

The program is compiled and stored in a file named sample. To start the session, type:
   adb sample
This starts adb and opens the corresponding program file. There is no core image file.

The first step is to set breakpoints at the beginning of each function. Use the :br command. For example, to set a breakpoint at the start of function ``f'', type:

   f:br
Use similar commands for the ``g'' and ``h'' functions. Once the breakpoints are created, display their locations by typing:
   $b
This command lists the address, optional count, and optional command associated with each breakpoint. In this case, the command displays:
   breakpoints
   count	bkpt		command
   1	h
   1	g
   1	f
The next step is to display the first five instructions in the ``f'' function. Type:
   f,5?ia
This command displays five instructions, each proceeded by its symbolic address:
   f:	push	ebp
   f+0x1:	mov	ebp,esp
   f+0x3:	sub	esp,0x8
   f+0x9:	push	ebx
   f+0xa:	push	edi
   f+0xb:		
Display five instructions in the ``g'' function without their addresses by typing:
   g,5?i
In this case, the display is:
   g:	push	ebp
   	mov	ebp,esp
   	sub	esp,0x8
   	push 	ebx
   	push	edi
To begin program execution, type:
   :r
then adb displays the following message and begins to execute:
   sample: running
As soon as adb encounters the first breakpoint (at the beginning of the ``f'' function), it stops execution and displays the following message:
   breakpoint	f:	push	ebp
Since execution to this point caused no errors, you can remove the first breakpoint by typing:
   f:dl
To continue the program, type:
   :co
adb displays the following message and begins program execution at the next instruction:
   sample: running
Execution continues until the next breakpoint, where adb displays the following message:
   breakpoint	g:	push	ebp
To trace the path of execution, type:
   $c
The commands show that only three functions are active: ``f'', ``main'', and ``_start'':
   f (0x1, 0x1)				from main+0x15
   main(0x1, 0x187ef20, 0x187ef28)	from _start+0x39
The values 0x187ef20, 0x187ef28, and 0x39 will vary.

Although the breakpoint has been set at the start of function ``g'', it will not be listed in the backtrace until its first few instructions have been executed. To execute these instructions, type:

   ,5:s
The adb program responds with a message indicating it has single-stepped the first five instructions. Now you can list the backtrace again. Type:
   $c
This time, the list shows four active functions:
   g (0x2,0x3)				from f+0x2c
   f (0x1,0x1)				from main+0x15	
   main (0x1, 0x187ef20, 0x187ef28)	from _start+0x39
To display the contents of the integer variable fcnt, type:
   fcnt/D
This command displays the value of fcnt found in memory. The number should be 1. To continue execution of the program and skip the first 10 breakpoints, type:
   ,10:co
adb starts the program; then it displays the running message again. It does not stop the program until it encounters exactly ten breakpoints. It displays the following message:
   breakpoint	g:	push	ebp
To show that these breakpoints have been skipped, display the backtrace again by typing:

$c

The system displays:

   f (0x2,0x11)			from h+0x29
   h (0x10,0xf)			from g+0x2b
   g (0x11,0x20)			from f+0x2c
   f (0x2,0xf)			from h+0x29
   h (0xe,0xd)			from g+0x2b
   g (0xf,0x1c)			from f+0x2c
   f (0x2,0xd)			from h+0x29
   h (0xc,0xb)			from g+0x2b
   g (0xd,0x18)			from f+0x2c
   f (0x2,0xb)			from h+0x29
   h (0xa,0x9)			from g+0x2b
   g (0xb,0x14)			from f+0x2c
   f (0x2,0x9)			from h+0x29
   h (0x8,0x7)			from g+0x2b
   g (0x9,0x10)			from f+0x2c
   f (0x2,0x7)			from h+0x29
   h (0x6,0x5)			from g+0x2b
   g (0x7,0xc)			from f+0x2c
   f (0x2,0x5)			from h+0x29
   h (0x4,0x3)			from g+0x2b
   g (0x5,0x8)			from f+0x2c
   f (0x2,0x3)			from h+0x29
   h (0x2,0x1)			from g+0x2b
   g (0x2,0x3)			from f+0x2c
   f (0x1,0x1)			from main+0x15
   main (0x1,0x187ef20,0x187ef28)	from _start+0x39
Exit adb by typing:
   $q

Next topic: Using the adb memory maps
Previous topic: Displaying external variables

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003