<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dev-Blog of DoesntMatter &#187; C++</title>
	<atom:link href="http://dev-blog.doesntmatter.de/category/scripting/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev-blog.doesntmatter.de</link>
	<description>Software technologies, tips, guides and tutorials</description>
	<lastBuildDate>Sun, 29 Apr 2012 12:57:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>HowTo: Debugging with GDB</title>
		<link>http://dev-blog.doesntmatter.de/2012/04/29/howto-debugging-with-gdb/</link>
		<comments>http://dev-blog.doesntmatter.de/2012/04/29/howto-debugging-with-gdb/#comments</comments>
		<pubDate>Sun, 29 Apr 2012 12:18:02 +0000</pubDate>
		<dc:creator>DoesntMatter</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[GDB]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://dev-blog.doesntmatter.de/?p=526</guid>
		<description><![CDATA[Since some month I am working with MMORPG emulators again, written in C++. Because of this I needed a powerful debugging tool and so I chose GDB. I will show you some nice-to-knows for debugging with GDB on a simple &#8230; <a href="http://dev-blog.doesntmatter.de/2012/04/29/howto-debugging-with-gdb/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since some month I am working with MMORPG emulators again, written in C++. Because of this I needed a powerful debugging tool and so I chose <a title="GDB" href="http://sources.redhat.com/gdb/" target="_blank">GDB</a>.<br />
I will show you some nice-to-knows for debugging with GDB on a simple code sample.</p>
<p><em>I recommend at least basic knowledge of C++, Linux and debugging to understand most of the things explained in the following part.</em></p>
<p>Assume we have this small C++ code block:</p>
<pre class="brush:cpp">int MyDivide(int, int);

int main()
{
    int x, y, result;
    x = 10, y = 2;
    result = MyDivide(x, y);
    x = 4; y = 0;
    result = MyDivide(x, y); // Division by zero
    return result;       
}                        

int MyDivide(int _x, int _y)
{   
    return _x / _y;
}</pre>
<p>Now put this in a file, called &#8220;divide.cpp&#8221;, and compile it to a binary, called &#8220;divide&#8221;:</p>
<pre class="brush:plain; gutter: false">$ g++ -g divide.cpp -o divide</pre>
<p>After doing this, let us start with the GDB magic and load the &#8220;divide&#8221; binary:</p>
<pre class="brush:plain; gutter: false">$ gdb divide 
...
(gdb)</pre>
<pre style="background: none;">

</pre>
<p><strong><u>Everything is prepared, so here are some useful things you can do with GDB:</u></strong></p>
<ol>
<li><u>Start application:</u></li>
<pre class="brush:plain; gutter: false">(gdb) run
Starting program: /home/dennis/Entwicklung/Misc/divide 

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400513 in MyDivide (_x=4, _y=0) at divide.cpp:15
15          return _x / _y;</pre>
<p>In our case the startup of the application results in a crash, due to division by zero.</p>
<li><u>Backtraces:</u></li>
<p>Because of the crash, we now can get more detailed information with the backtrace.</p>
<pre class="brush:plain; gutter: false">(gdb) bt
#0  0x0000000000400513 in MyDivide (_x=4, _y=0) at divide.cpp:15
#1  0x00000000004004f9 in main () at divide.cpp:9
(gdb) bt full
#0  0x0000000000400513 in MyDivide (_x=4, _y=0) at divide.cpp:15
No locals.
#1  0x00000000004004f9 in main () at divide.cpp:9
        x = 4
        y = 0
        result = 5</pre>
<li><u>Breakpoints:</u></li>
<pre class="brush:plain; gutter: false">(gdb) break divide.cpp:7
Breakpoint 1 at 0x4004ca: file divide.cpp, line 7.
(gdb) break divide.cpp:9
Breakpoint 2 at 0x4004ea: file divide.cpp, line 9.</pre>
<li><u>Step debugging:</u></li>
<pre class="brush:plain; gutter: false">(gdb) run
Starting program: /home/dennis/Entwicklung/Misc/divide 

Breakpoint 1, main () at divide.cpp:7
7           result = MyDivide(x, y);
(gdb) next
8           x = 4; y = 0;
(gdb) continue
Continuing.

Breakpoint 2, main () at divide.cpp:9
9           result = MyDivide(x, y); // Division by zero</pre>
<p>With the &#8220;next&#8221; command, GDB will go to the next line, which is executed by the application. As opposed to this the &#8220;continue&#8221; command will simply continue the application.</p>
<li><u>Variable manipulation:</u></li>
<pre class="brush:plain; gutter: false">(gdb) print y
$1 = 0
(gdb) set y = 1
(gdb) print y
$2 = 1</pre>
<li><u>Exiting application:</u></li>
<pre class="brush:plain; gutter: false">(gdb) quit</pre>
</ol>
<p>I hope this tutorial helps you to work with GDB and to get some personal achievements with debugging your applications.</p>
<p>If you have feedback, regards, corrections or questions please let me know and do not hesitate to comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-blog.doesntmatter.de/2012/04/29/howto-debugging-with-gdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
