Johnny [Life & Code]

Semper Exploro!

GDB Scripting : A short article for a internal magazine

with 11 comments

I wrote a small article for a internal magazine and few of my friends wanted me to post it to this blog.

This is for people who are new to GDB and still exploring itz features. So if you’ve used GDB for more than few weeks please ignore and skip :)

GDB – Scripting

A good majority of novice programmers tend to use printf functions to trace function calls and to printout the debug data. This forces you to change the code and compile again and again. To eliminate these superfluous tasks from your day-to-day work, use GDB. The GDB has facilities for scripting and helps in saving plenty of your time.

Tracing Function Calls

If you want to know whether a function is called or not, create a break point and write a simple script.

<code>

#Set the breakpoint

(gdb) b mapi_sync

Breakpoint 1 at 0x7fffd75f36e2: file camel-mapi-folder.c, line 741

#Tell GDB what to do when the breakpoint is reached

(gdb) commands

Type commands for when breakpoint 1 is hit, one per line.

End with a line saying just “end”.

> continue

> end

(gdb)

</code>

continue – Come out of break and continue

end – terminate command list

Run the program now. The GDB prints the function name when the breakpoint is hit and automatically continues running the program.

Breakpoint 1, mapi_sync (folder=0xc9c1a0, expunge=0, ex=0xf3a0c0) at camel-mapi-folder.c:741

741             CamelMapiStore *mapi_store = CAMEL_MAPI_STORE (folder->parent_store);

Using the GDB Scripts for Analyzing the Data

Suppose that you have a singly-linked list that has strings in it. At some point, you might want to know the contents of the list. To do this, use the GDB scripting instead of adding the debug statements in your code.

<code>

#Example for gslist traversal.

define p_gslist_str

set $list = ($arg0)

while ((GSList *)$list->next != 0)

p (char *)(GSList *)$list->data

set $list = (GSList *)$list->next

end

end

document p_gslist_str

p_gslist_str <list>: Dumps the strings in a GSList

end

</code>

Add the above snippet into a file and load it into the GDB as follows:

<code>

(gdb) source /home/jjohnny/scripts/gdb/gslist.gdb

</code>

Now, anywhere you want to take a look in the GSList, simply break and

<code>

(gdb) p_gslist_str server_uid_list

$17 = 0x7fffd81101b0 “7666BC1E000000015870BD1E00000001″

$18 = 0x7fffd810e330 “7666BC1E000000015970BD1E00000001″

$19 = 0x7fffd810cbe0 “7666BC1E000000015C70BD1E00000001″

</code>

Simple scripts thus can save you a lot of time from adding or removing the debugging statements from your code. Now go ahead and create a suite of scripts to aid the library you are writing.

More cool developer tricks later. Have fun !

— End —

Thanks to Radhika for editing the article.

Btw when is Archer branch  (Python scripting) getting into GDB ? I’ve been using it a bit .

GDB

- Scripting

A good majority of novice programmers tend to use printf functions to trace function calls and to printout the debug data. This forces you to change the code and compile again and again. To eliminate these superfluous tasks from your day-to-day work, use GDB, the GNU Project Debugger. The GDB has facilities for scripting and helps in saving plenty of your time.
Tracing Function Calls
If you want to know whether a function is called or not, create a break point and write a simple script.
<code>
#Set the breakpoint
(gdb) b mapi_sync
Breakpoint 1 at 0x7fffd75f36e2: file camel-mapi-folder.c, line 741
#Tell GDB what to do when the breakpoint is reached
(gdb) commands
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just “end”.
> continue
> end
(gdb)
</code>
continue – Come out of break and continue
end – terminate command list
Run the program now. The GDB prints the function name when the breakpoint is hit and automatically continues running the program.
Breakpoint 1, mapi_sync (folder=0xc9c1a0, expunge=0, ex=0xf3a0c0) at camel-mapi-folder.c:741
741             CamelMapiStore *mapi_store = CAMEL_MAPI_STORE (folder->parent_store);
Using the GDB Scripts for Analyzing the Data
Suppose that you have a singly-linked list that has strings in it. At some point, you might want to know the contents of the list. To do this, use the GDB scripting instead of adding the debug statements in your code to print out the data.
</code>
#Example for gslist traversal.
define p_gslist_str
set $list = ($arg0)
while ((GSList *)$list->next != 0)
p (char *)(GSList *)$list->data
set $list = (GSList *)$list->next
end
end
document p_gslist_str
p_gslist_str <list>: Dumps the strings in a GSList
end
</code>
Add the above snippet into a file and load it into the GDB as follows:
<code>
(gdb) source /home/jjohnny/scripts/gdb/gslist.gdb
</code>
Now, anywhere you want to take a look in the GSList, simply break
<code>
(gdb) p_gslist_str server_uid_list
$17 = 0x7fffd81101b0 “7666BC1E000000015870BD1E00000001″
$18 = 0x7fffd810e330 “7666BC1E000000015970BD1E00000001″
$19 = 0x7fffd810cbe0 “7666BC1E000000015C70BD1E00000001″
</code>
Simple scripts thus can save you a lot of time from adding or removing the debugging statements from your code.
Now go ahead and create a suite of scripts to aid the library you are writing.
More cool developer tricks later. Have fun !
About these ads

Written by Johnny

July 7, 2009 at 8:56 am

11 Responses

Subscribe to comments with RSS.

  1. The python scripting engine _is_ part of gdb HEAD, and will be part of gdb-7.0.

    Maik

    July 7, 2009 at 9:54 am

  2. I want more of these! Thanks!

    avilella

    July 7, 2009 at 12:27 pm

  3. I use this technique all the time, but it has limitations.

    It only works well for breakpoints that are rarely hit, or if the code takes little time to execute. If the code you’re trying to monitor is in a hot spot, it could run as much as 1000x slower, which can be a problem if it already takes time to reach the problem area of the code.

    Joe Buck

    July 7, 2009 at 6:50 pm

  4. Simple scripts thus can save you a lot of time from adding or removing the debugging statements from your code.

    As I understand, GDB itself is to avoid adding or removing the debugging statements to code. Scripting is to avoid repeated typing of typing lengthy/multiple commands again and again. For example, it is possible to traverse the list in the GDB prompt, but scripting makes it easier, if you would be doing it often. Just like bash scripts and bash prompt.

    Nice technical article. I would like to read more like these. :)

    Nikanth

    July 8, 2009 at 4:11 am

    • Nice article! This would definitely save time for debugging certain issues, but still printf’s are my favorite for debugging threading issues :)

      I have learn’t so much things from searching blogs, especially while programming share-point. Unfortunately I could not put much there due to some reasons. Looking forward to see some technical articles on mapi side too :) We may not know, small things which appear to us may turn out to be a great deal for someone ;)

      chenthill

      July 8, 2009 at 4:49 am

  5. […] Johnny Jacob: GDB Scripting : A short article for a internal magazine is now available in this link…: News […]

  6. […] podatkovne strukture. Vendar to na koncu nisem. Pred kratkim pa sem ugotovil, da tudi gdb podpira skriptiranje. Kar pomeni da lahko sedaj iteriram skozi seznam kar preko zanke. Ko sem še malo raziskoval gdb […]

  7. […] disposant de nombreuses commandes évoluées. Aussi, son usage simplifie facilement les besoins communs, mais il peut également apporter des solutions aux problèmes plus complexes, comme le fait de […]

  8. How do I make it truly dymamic?
    Eg: print_singly_list_of_struct (list_head, field_to_be_printed)
    Just pass list head and the field to be printed and have the data printed out. How can I pass a ‘string’ as a variable. I have tried but it doesn’t work.

    Krish

    August 1, 2013 at 12:52 pm

  9. There is a slight bug in the script, it won’t print the data for the last element. there should probably be antoher “p (char *)(GSList *)$list->data” after the first “end”

    Patrik Sevallius

    June 5, 2014 at 2:09 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 453 other followers

%d bloggers like this: