{"id":72,"date":"2008-07-18T10:03:18","date_gmt":"2008-07-18T18:03:18","guid":{"rendered":"http:\/\/www.codebelay.com\/blog\/?p=72"},"modified":"2008-07-18T10:03:18","modified_gmt":"2008-07-18T18:03:18","slug":"fun-with-gdb-gnus-debugger","status":"publish","type":"post","link":"https:\/\/www.codebelay.com\/blog\/2008\/07\/18\/fun-with-gdb-gnus-debugger\/","title":{"rendered":"Fun With GDB, Gnu&#8217;s Debugger"},"content":{"rendered":"<p>Here&#8217;s a pretty compact version of strcmp:<\/p>\n<pre>\r\nint bstrcmp(char *s1,char *s2) {\r\n   while(*s1 == *s2++) {\r\n      if(*s1++ == 0){ return 0; }\r\n   }\r\n   return (*(unsigned char *)s1 - *(unsigned char*)--s2);\r\n}\r\n<\/pre>\n<p>The source that I used for compiling and calling this version of strcmp is <a href=\"http:\/\/www.codebelay.com\/strcmp.c\">here<\/a>.<\/p>\n<p>Compile that code using:<br \/>\ngcc -o strcmp -g strcmp.c<\/p>\n<p>Fire up the debugger using:<br \/>\ngdb strcmp<\/p>\n<p>You&#8217;ll get the gdb prompt:<br \/>\n(gdb)<\/p>\n<p>Set a break point at the start:<br \/>\nb main<\/p>\n<p>The debugger will echo something like:<br \/>\nBreakpoint 1 at 0x80483d5: file strcmp.c, line 6.<br \/>\n(gdb) <\/p>\n<p>Then run the program:<br \/>\nrun<\/p>\n<p>The debugger will print out something like:<br \/>\nStarting program: \/home\/somedude\/bin\/strcmp <\/p>\n<p>Breakpoint 1, main () at strcmp.c:6<br \/>\n6\t{<br \/>\n(gdb) <\/p>\n<p>If you type n a few times, you&#8217;ll eventually get to some variable assignments.<\/p>\n<pre>\r\n(gdb) n\r\n8\tchar s1[] = \"better\";\r\n(gdb) n\r\n9\tchar s2[] = \"better than\"; \/* than this\";*\/\r\n(gdb) n\r\n11\tint i_result = 0;\r\n(gdb) n\r\n13\ti_result = bstrcmp(s1,s2);\r\n<\/pre>\n<p>If you want to the values of these variables type:<br \/>\np i_result<\/p>\n<p>You get back:<br \/>\n(gdb) p i_result<br \/>\n$1 = 0<\/p>\n<p>To step into a function, type s:<\/p>\n<pre>\r\n(gdb) s\r\nbstrcmp (s1=0xbf86b469 \"better\", s2=0xbf86b474 \"better than\") at strcmp.c:26\r\n26\t\twhile(*s1 == *s2++) { \r\n(gdb) n\r\n27\t\t\tif(*s1++ == 0){ return 0; } \r\n(gdb) n\r\n26\t\twhile(*s1 == *s2++) { \r\n(gdb) n\r\n27\t\t\tif(*s1++ == 0){ return 0; } \r\n(gdb) n\r\n26\t\twhile(*s1 == *s2++) { \r\n(gdb) \r\n<\/pre>\n<p>At this point you can type things like:<br \/>\np s1<br \/>\np *s1<br \/>\np s2<br \/>\np *s2<\/p>\n<p>And you&#8217;ll get back the value of the pointers and what&#8217;s in memory.<\/p>\n<p>Next time we&#8217;ll go over how to do this with PHP running single threaded on debug mode on Apache.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a pretty compact version of strcmp: int bstrcmp(char *s1,char *s2) { while(*s1 == *s2++) { if(*s1++ == 0){ return 0; } } return (*(unsigned char *)s1 &#8211; *(unsigned char*)&#8211;s2); } The source that I used for compiling and calling this version of strcmp is here. Compile that code using: gcc -o strcmp -g strcmp.c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[90,76,100,99,98],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-how-to","category-uncategorized","tag-c","tag-coding","tag-debugging","tag-gcc","tag-gdb"],"_links":{"self":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":0,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}