I have been trying to find a way for some time to automate the progress in GDB of tracing the control flow of a program.
Even just a simple way of automating the n
command so you can see in what order routines are called.
I realise that you can issues n x
where x is the number of times GDB steps through, but the trouble with that is that it shows the command but not the address of the routine! But if you press n
manually in GDB (then press return to issue the previous command) it shows the address.
I have tried the following in GDB:
(after setting a breakpoint at say 0x0123456)
b *0x0123456
GDB says <breakpoint 1 at 0x123456>
. I type:
commands 1
n 1000
c
end
but it doesn't loop as expected, and it doesn't show the address location :-(.
Any help would be appreciated! Surely it must be simple to automatically log the order routines are called??
This is easy, actually. I'll give you the bare bones, and you can modify to suit.
(gdb) define nstep
> set $foo = $arg0
> while ($foo--)
> step
> end
> end
(gdb) nstep 100
I've done this many times. Hope this helps!
How about using strace for system calls and ltrace for library calls to see traces instead of using gdb? strace for system calls and ltrace for library calls work with any application, even if you don't have the source code.
If you have access to the source code, then you could also instrument your own code to do traces. Gcc has a mode that allows you to do that. Here's an example.
Then you would only have to analyze the log files.
If you want to use plain GDB, then you can use Breakpoint Command Lists
You can give any breakpoint (or watchpoint or catchpoint) a series of commands to execute when your program stops due to that breakpoint. For example, you might want to print the values of certain expressions, or enable other breakpoints.
And in particular:
for example, here is how you could use breakpoint commands to print the value of x at entry to foo whenever x is positive.
break foo if x>0
commands
silent
printf "x is %d\n",x
cont
end
There is a shell script for automatized tracing function calls with gdb:
http://blog.superadditive.com/2007/12/01/call-graphs-using-the-gnu-project-debugger/
Because the link is dead now, I'm posting copy of text here (author is Juan M. Bello Rivas - jmbr): http://web.archive.org/web/20090317091725/http://blog.superadditive.com/2007/12/01/call-graphs-using-the-gnu-project-debugger/
Call graphs using the GNU Project Debugger
Chris Ball asked everyone who likes Unix for a tool which would be to function calls what strace and ltrace are for system and library calls respectively.
I wanted to write this myself for a long time but what prevented me from doing so was the fact that I had come up with a trivial shell/awk script that achieved almost the same goal. Since that little program was useful for someone other than me, I thought I’d polish it slightly and post it here.
This tool’s original intent was to print a call graph from a given program. It works by taking control of GDB and automatically setting breakpoints at each function call. Of course, the executable must include debugging symbols (that is, it must have been compiled with gcc’s -g option). Here’s some sample output (the format is: caller callee arguments):
xmltree_parse xmltree_itor_next (self=0x80571e8)
xmltree_itor_next xmlnode_stack_push (stack=0x80571f0, n=0x8056780)
xmltree_parse h_requires (n=0x8056808, u=0xbff9d504)
h_requires xmlnode_get_attr (self=0x8056808, name=0x80520ea "plugin")
h_requires probe_get_name (self=0x8056af0)
h_requires htab_lookup (self=0x8058100, key=0x8056868, len=3, create=0, value=0x0)
You can use the included
cg2dot.awk
script to produce files suitable for processing withgraphviz
.
The program (archive size 2110bytes, md5 54b583e637d19be0502ccadfa6174756):
http://web.archive.org/web/20110902010824/http://superadditive.com/software/callgraph.tar.gz
It dumps all functions from program and generate a gdb command file with breakpoints on each function. At each breakpoint, "backtrace 2
" and "continue
" are executed (via commands
).
This script is rather slow on big project (~ thousands of functions), so I added a filter on function list (via egrep). It was very easy. I use this script almost every month.
PS: There is a copy of callgraph.tar.gz for the case when archive.org will delete it from archives. Use xxd -r
to restore
0000000: 1f8b 0800 825e 1d49 0003 ed58 7b73 db46 .....^.I...X{s.F
0000010: 0ecf bfe2 a740 6835 926c ea69 2799 8b62 .....@h5.l.i'..b
0000020: 5f69 89b6 7927 4b3e 926a 9ab3 5497 2257 _i..y'K>.j..T."W
0000030: d2d6 14a9 922b 3b4e 93fb ec07 2ca9 97d3 .....+;N....,...
0000040: 34d3 995c 3bbd 1126 1391 c0e2 87c7 0258 4..\;..&.......X
0000050: ae3d 3708 26b1 3b9f 56bd e5d3 93af 4d35 .=7.&.;.V.....M5
0000060: a417 4747 f45b 7ff9 bcb6 f92b 45cf 1b47 ..GG.[.....+E..G
0000070: 4fea 757c 383c 7af1 fca8 fea4 563f 3c44 O.u|8<z.....V?<D
0000080: 31d4 beba 27bf 428b 44b8 31c0 939f 66a3 1...'.B.D.1...f.
0000090: f8b7 d67d 49fe 17a5 bda7 d511 0fab c954 ...}I..........T
00000a0: 51f6 a015 cd1f 623e 990a 287a 2568 d46a Q.....b>..(z%h.j
00000b0: 4765 fcef 25fc 63e1 8670 5981 5316 0411 Ge..%.c..pY.S...
00000c0: 58fc ce4d e035 25e4 db64 3167 b1eb fb5c X..M.5%..d1g...\
00000d0: f03b 56f1 a2d9 89b2 8740 572c 9ef1 24e1 .;V......@W,..$.
00000e0: 5108 3c81 298b d9e8 01b0 b842 c17c 0dc6 Q.<.)......B.|..
00000f0: 3163 108d c19b baf1 8469 2022 70c3 0740 1c.......i "p..@
0000100: a424 0a51 3b1a 0997 873c 9c80 0b1e ba44 .$.Q;....<.....D
0000110: 6bc5 1481 9268 2cee dd98 e172 1fdc 2489 k....h,....r..$.
0000120: 3cee 2222 f891 b798 b150 b882 2c8e 79c0 <."".....P..,.y.
0000130: 1284 298a 2903 d5ce 74d4 9234 e433 3700 ..).)...t..4.37.
0000140: 1e02 c996 22b8 e762 1a2d 04c4 2c11 31f7 ...."..b.-..,.1.
0000150: 0845 4300 1e7a c1c2 273f 960b 023e e399 .EC..z..'?...>..
0000160: 1502 90b9 4a08 7691 601c e4ab 06b3 c8e7 ....J.v.`.......
0000170: 63fa 6514 1cc2 cc17 a380 2753 0d7c 4ef0 c.e.......'S.|N.
0000180: a385 c0b5 0931 3d16 921e 4653 8d62 4830 .....1=...FS.bH0
0000190: bb84 c159 9246 bcf6 9060 2866 b434 a7d4 ...Y.F...`(f.4..
00001a0: 8a2c 59d2 f6fd 349a 6dc7 83a9 1a2f e210 .,Y...4.m..../..
00001b0: 8d32 a9e3 4798 3ac2 40ab 3f31 4f10 8f14 .2..G.:.@.?1O...
00001c0: c611 eee7 3d05 e845 21ed 2122 be92 1be8 ....=..E!.!"....
00001d0: a0d4 1d45 774c 0695 d644 1809 f438 f583 ...EwL...D...8..
00001e0: 7663 bede e44c 944c 7186 c088 ad72 87e6 vc...L.Lq....r..
00001f0: 31d7 ee46 5c31 f980 2d17 0a8e 1b31 8f62 1..F\1..-....1.b
0000200: 69f4 71bc 95d4 890b 03ec de99 f346 b70c i.q..........F..
0000210: 306d b8b2 7adf 996d a30d aa6e e3bb aac1 0m..z..m...n....
0000220: 1bd3 b9e8 f51d c015 96de 75de 42ef 0cf4 ..........u.B...
0000230: ee5b f8a7 d96d 53bc c6f7 5796 61db d0b3 .[...mS...W.a...
0000240: c0bc bcea 9846 5b03 b3db eaf4 db66 f71c .....F[......f..
0000250: 4e51 b3db 73a0 635e 9a0e c23a 3d69 3203 NQ..s.c^...:=i2.
0000260: 330d d43b 4390 4bc3 6a5d 2043 3f35 3ba6 3..;C.K.j] C?5;.
0000270: f356 8333 d3e9 12ea 19c2 ea70 a55b 8ed9 .V.3.......p.[..
0000280: ea77 740b aefa d655 cf36 d085 36ea 757b .wt....U.6..6.u{
0000290: 5db3 7b66 a125 e3d2 e83a 15b4 8c3c 30be ].{f.%...:...<0.
00002a0: c317 b02f f44e 479a d3fb 1883 257d 6cf5 .../.NG.....%}l.
00002b0: aede 5ae6 f985 0317 bd4e dbb0 6c44 3935 ..Z......N..lD95
00002c0: d03f fdb4 63a4 e630 b856 4737 2f35 68eb .?..c..0.VG7/5h.
00002d0: 97fa b921 f57a 8863 c965 9987 6f2e 0cc9 ...!.z.c.e..o...
00002e0: 428b 7a17 41f4 9663 f6ba 949d 56af eb58 B.z.A..c....V..X
00002f0: f8aa 61b4 96b3 527e 63da 8606 ba65 da94 ..a...R~c....e..
0000300: 9833 ab87 0628 b1a8 41b9 230c d4ec 1a29 .3...(..A.#....)
0000310: 0ea5 7d7b 7770 11bd f76d 63ed 4fdb d03b ..}{wp...mc.O..;
0000320: 8866 9317 c840 88e5 f28a a2cc e368 7213 .f...@.......hr.
0000330: ba33 76ac ae0e 1e55 51f8 18ae 21bf 07e5 .3v....UQ...!...
0000340: 4040 1d86 4daa 8a50 0124 e64d 2350 fb89 @@..M..P.$.M#P..
0000350: 3b61 af20 bf52 c72d 365a 7d47 26e8 5ab7 ;a. .R.-6Z}G&.Z.
0000360: ceed 4aa5 3254 571a 1baa c63b 7736 0fb6 ..J.2TW....;w6..
0000370: 95ff 2387 9fc0 b62f 1333 7667 d813 1134 ..#..../.3vg...4
0000380: 0e33 8477 d869 7565 cc69 38da 6ec8 c503 .3.w.iue.i8.n...
0000390: 0e2d e6dd 2615 e5cc ec18 c7f9 7ae6 f253 .-..&.......z..S
00003a0: 28bf 833c f17e c5e9 b5c1 57d0 0fdd 51c0 (..<.~....W...Q.
00003b0: a801 c71c 1b89 bd63 de42 4856 41aa 173e .......c.BHVA..>
00003c0: b1dc d1bb e7c7 aa0a 137f 04e5 32bb 7383 ............2.s.
00003d0: 32ce d919 b6e1 f1cf 0b5c 955a 6d9c 3cab 2........\.Zm.<.
00003e0: c340 ea7e c049 cbe6 5036 a050 0ca3 018e .@.~.I..P6.P....
00003f0: bcd1 6232 c12e 1f40 f230 1b45 4132 c028 ..b2...@.0.EA2.(
0000400: 17a1 ff01 db76 800d 3ad8 7083 44f1 cc15 .....v..:.p.D...
0000410: a542 0a79 0255 9fdd 55c3 4510 649b f377 .B.y.U..U.E.d..w
0000420: 28b3 9fa1 f669 9ce5 703b d496 1b16 7050 (....i..p;....pP
0000430: c53c 1440 7b0c 7293 097f 192b 8e0a cfc5 .<.@{.r....+....
0000440: d109 5c14 121a 2238 f0d5 8dc4 e1e6 b8f1 ..\..."8........
0000450: c366 9230 f239 8e77 5f4e e475 64cb c02a .f.0.9.w_N.ud..*
0000460: 9bd9 6bca f425 533e 1672 ff98 80c5 1c04 ..k..%S>.r......
0000470: 9be1 d421 5879 4e54 146a 07e3 58fd 7176 ...!XyNT.j..X.qv
0000480: 4b22 288b 8d02 a97c bfa2 1f55 f8f0 4122 K"(....|...U..A"
0000490: 2be7 8673 d6ef b6ec dfa5 2430 7828 c433 +..s......$0x(.3
00004a0: 288f 711b 3153 d2ae 8a0f 4b38 b580 c56c (.q.1S....K8...l
00004b0: 3ad9 4af9 7f59 729a f09b 6acd 2cde 025c :.J..Yr...j.,..\
00004c0: f4af b0d9 1cf8 57df 74c0 31ac 4b8a db71 ......W.t.1.K..q
00004d0: 6f29 6fa1 88a3 80e6 ec79 fb54 cef0 c71b o)o......y.T....
00004e0: 5351 3c57 e07e af80 e1f5 6b03 0760 8279 SQ<W.~....k..`.y
00004f0: 9b32 7902 d414 1e8e b172 17a1 3c23 1385 .2y......r..<#..
0000500: e44a 5a98 2357 7853 fc5d d6e6 1a66 59a0 .JZ.#WxS.]...fY.
0000510: ab42 c20a 75ef 6fa1 a02c 8160 c2c4 0dbd .B..u.o..,.`....
0000520: c8fc 15f1 942c 29bf e04e 26f3 800b 7ad5 .....,)..N&...z.
0000530: 60ee c642 03b5 a896 9a28 0858 08c7 9998 `..B.....(.X....
0000540: 24d7 f5e1 6a09 7c66 0932 d68b f669 11ae $...j.|f.2...i..
0000550: 8a99 c083 1256 0b9a ca47 4539 35ce 7164 .....V...GE95.qd
0000560: 9103 2212 784a 1d43 8d10 d384 a994 8f7b ..".xJ.C.......{
0000570: ee63 f9d5 d447 ec65 9a1e f3ef 583c 8a12 .c...G.e....X<..
0000580: fae6 19ab d242 f5da 2dbf d7cb ffbe 1966 .....B..-......f
0000590: 0fb5 f2df 6e86 fb83 6255 9a1d 93eb db39 ....n...bU.....9
00005a0: c9d7 4a2b d071 511d c5cc bd85 6f92 4188 ..J+.qQ.....o.A.
00005b0: a7e0 3894 b283 03e9 ae34 6074 db29 1236 ..8......4`t.).6
00005c0: 5b91 235a bd09 1c5e 1fa7 11e1 f3c1 4149 [.#Z...^......AI
00005d0: 2e58 f999 ed5b 8278 bc29 05d5 fda5 4cee .X...[.x.)....L.
00005e0: 3a7e ab25 586b fbd5 4dad 91eb dd62 a5e2 :~.%Xk..M....b..
00005f0: d9df 509b db70 78c2 870b b6cd 65a1 2f19 ..P..px.....e./.
0000600: 1f95 7582 e245 a8a2 cb05 2a3d 59dc 9fad ..u..E....*=Y...
0000610: 2829 4586 100f c7eb 622a 97c9 b3ac caf2 ()E.....b*......
0000620: df7e b9d0 a8e4 1956 d956 8d61 d418 2456 .~.....V.V.a..$V
0000630: 8696 964f ca26 de75 6348 295e 5756 aa8f ...O.&.ucH)^WV..
0000640: 391d 8758 779b 6594 4ae4 066c 59c4 f272 9..Xw.e.J..lY..r
0000650: 6709 1909 1f19 4509 31d7 1691 210d 4a81 g.....E.1...!.J.
0000660: 0ad8 9199 3f8b 1116 0716 886c 0229 47e3 ....?......l.)G.
0000670: 8f8b 58fa b355 c13c 8999 17c5 feb2 8857 ..X..U.<.......W
0000680: ceab eaea 35a6 d77d b36b 3aa6 ded9 4f8b ....5..}.k:...O.
0000690: 577a 9c2d a382 fde1 942a 6e1e d196 5d63 Wz.-.....*n...]c
00006a0: b50e 0fb4 ea63 0375 e9cd cac2 46ae 97d5 .....c.u....F...
00006b0: bb44 dd48 4abe a665 2aa5 ccd2 5efd 1a06 .D.HJ..e*...^...
00006c0: 6278 90e1 8fb1 8033 1b25 594b 2b9f f347 bx.....3.%YK+..G
00006d0: 9946 fed3 a5f0 ec19 5e62 d295 4fb7 c22b .F......^b..O..+
00006e0: 6d15 3ef6 d237 09c8 7fb2 9752 95a5 4b5a m.>..7.....R..KZ
00006f0: e672 a9b9 b64c a1ad 5cd8 ca13 d535 16b2 .r...L..\....5..
0000700: f267 dfe9 7e0f 79eb fbff a4e1 47a2 820d .g..~.y.....G...
0000710: f3b5 6d7c e1fe 5f3b acbd fce4 fe7f f472 ..m|.._;.......r
0000720: 77ff ff23 28bb ff4f 684e 96c7 0a4e a71c w..#(..OhN...N..
0000730: f327 2cb9 ced7 35c8 3786 695b af4e 945c .',...5.7.i[.N.\
0000740: 36b4 7d9e 7ecc 9dc3 2f58 f839 79cc e09c 6.}.~.../X.9y...
0000750: 46a8 f4ca 2731 a8d1 72b9 74ae 2d85 9a14 F...'1..r.t.-...
0000760: 6960 f74f 6de3 0afb 2a97 5bf6 e140 0cb0 i`.Om...*.[..@..
0000770: 1707 2a94 4f20 7d6a ca96 2405 79c0 cb87 ..*.O }j..$.y...
0000780: 060d bddc c795 271f e580 fab3 d3f8 97a5 ......'.........
0000790: 75ff d32d a8e2 fd2f 6c7c a9ff 8f1a f547 u..-.../l|.....G
00007a0: fddf 7851 abef faff 8fa0 bdec 0f35 f03a ..xQ.........5.:
00007b0: 113e 8f2a d313 658b 15f0 11f1 9484 feec .>.*..e.........
00007c0: e5c1 5dc4 7dba 2217 a9f7 e8bb 6593 3f72 ..].}.".....e.?r
00007d0: e3cf f0df aff8 7883 10ca cce5 a1e4 e097 ......x.........
00007e0: 9ba7 c9bf 03c2 3e3e df5d 0fe9 fb28 4706 ......>>.]...(G.
00007f0: 1a87 b43a 4757 9b22 5d82 6eec 7eab 65d8 ...:GW."].n.~.e.
0000800: 76fa 9520 7195 b523 a445 e6c3 b558 59bb v.. q..#.E...XY.
0000810: 934a df3f 92be df90 a69f 50bb 49b2 a31d .J.?......P.I...
0000820: ed68 473b dad1 8e76 b4a3 1ded 6847 3bda .hG;...v....hG;.
0000830: d18e fe0f e9bf e4d9 0494 0028 0000 ...........(..
Fenris, is THE tool your looking for, not gdb. Fenris homepage
Authored by the legondary Michal Zalewski, is exactally what your looking for.
Here is my attempt of something similar, using the gdb python API - c - GDB auto stepping - automatic printout of lines, while free running?
I have created a script to do this specifically, it automates gdb to send 's' and then read gdb's output, continuously, and then output the result to a text file and to terminal.
The script is in C, and it is posted on my blog.
http://www.codeground.net/coding/gdb-step-into-all-lines-to-get-full-application-flow/
I came to this question while searching for that script, and couldn't find something exactly like it, so hope this helps someone else.
wget http://www.codeground.net/downloads/gdbwalkthrough.c
gcc gdbwalkthrough.c -o gdbwalkthrough
./gdbwalkthrough <application full path> [application arguments]
This script can be modified easily to step only through a set of lines, or to step only a predefined number of times, just replace
while(1)
writeDebugger("s",NULL);
with this
int x=1000;
while(x--)
writeDebugger("s",NULL);
来源:https://stackoverflow.com/questions/764382/how-to-print-the-next-n-executed-lines-automatically-in-gdb