There is a shell script for automated calls to the trace function using gdb:
http://blog.superadditive.com/2007/12/01/call-graphs-using-the-gnu-project-debugger/
Since the link is now dead, I am posting a copy of the text here (author - 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 loves Unix for a tool that will make calls, which 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 this was that I came up with a trivial shell / awk script that achieved almost the same goal. Since this little program was useful for someone other than me, I thought Id would polish it a bit and post it here.
The purpose of these tools was to print a graph of calls from this program. It works by controlling GDB and automatically setting breakpoints with every function call. Of course, the executable should contain debugging symbols (that is, it must be compiled with the gccs -g option). Has some sample output (format: caller 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 create files suitable for processing with graphviz .
Program (archive size 2110 bytes, md5 54b583e637d19be0502ccadfa6174756):
http://web.archive.org/web/20110902010824/http://superadditive.com/software/callgraph.tar.gz
It resets all functions from the program and generates a gdb batch file with breakpoints for each function. At each breakpoint, " backtrace 2 " and " continue " are executed (via commands ).
This script is rather slow in a large project (~ thousands of functions), so I added a filter to the list of functions (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 removes it from the archives. Use xxd -r to restore
0000000: 1f8b 0800 825e 1d49 0003 ed58 7b73 db46 .....^.I...X{sF 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 Ou|8<z.....V?<D 0000080: 31d4 beba 27bf 428b 44b8 31c0 939f 66a3 1...'.BD1...f. 0000090: f8b7 d67d 49fe 17a5 bda7 d511 0fab c954 ...}I..........T 00000a0: 51f6 a015 cd1f 623e 990a 287a 2568 d46a Q.....b>..(z%hj 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 ....Jv`....... 0000170: 63fa 6514 1cc2 cc17 a380 2753 0d7c 4ef0 ce......'S.|N. 0000180: a385 c0b5 0931 3d16 921e 4653 8d62 4830 .....1=...FS.bH0 0000190: bb84 c159 9246 bcf6 9060 2866 b434 a7d4 ...YF..`(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 iq.........F.. 0000210: 306d b8b2 7adf 996d a30d aa6e e3bb aac1 0m..z..m...n.... 0000220: 1bd3 b9e8 f51d c015 96de 75de 42ef 0cf4 ..........uB.. 0000230: ee5b f8a7 d96d 53bc c6f7 5796 61db d0b3 .[...mS...Wa.. 0000240: c0bc bcea 9846 5b03 b3db eaf4 db66 f71c .....F[......f.. 0000250: 4e51 b3db 73a0 635e 9a0e c23a 3d69 3203 NQ..sc^...:=i2. 0000260: 330d d43b 4390 4bc3 6a5d 2043 3f35 3ba6 3..;CKj] 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 ...!.zce.o... 00002e0: 428b 7a17 41f4 9663 f6ba 949d 56af eb58 BzA.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.