<?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>technochakra.com &#187; visualstudio</title>
	<atom:link href="http://www.technochakra.com/tag/visualstudio/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.technochakra.com</link>
	<description>Wheels Of Technology</description>
	<lastBuildDate>Thu, 04 Feb 2010 11:48:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Debugging &#8211; Modifying Code At Runtime</title>
		<link>http://www.technochakra.com/debugging-modifying-code-at-runtime/</link>
		<comments>http://www.technochakra.com/debugging-modifying-code-at-runtime/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 12:15:44 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[visualstudio]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=343</guid>
		<description><![CDATA[Introduction The build and debug cycle can be tedious especially when you are unsure whether the change you have in mind solves your problem or not. Sometimes it is good to be able to tweak the logic of your code during a debugging session. This article explains how to make minor adjustments to code without [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>The build and debug cycle can be tedious especially when you are unsure whether the change you have in mind solves your problem or not.  Sometimes it is good to be able to tweak the logic of your code during a debugging session.  This article explains how to make minor adjustments to code without having to rebuild your application.</p>
<p>The article requires tweaking the assembly  and dealing with opcodes.  If you are not very familar with debugging in assembly, you may want to read <a href="http://www.technochakra.com/assembly-and-the-art-of-debugging/" target="_blank">this article</a> first.  The tips shared here are also useful while debugging libraries where you either do not have the source code or the build environment is unavailable.</p>
<p>Debuggers work on the common principle of launching the executable as a child program and requesting special permissions from the operating system to access and alter the state of the program.  This allows it to show and/or modify the memory, registers, code, etc in a debugging session.  If a debugger attaches itself to another program already loaded in memory, it requests special permission from the operating system to take control of the program for the purpose of debugging.  It will now seem very obvious why the same program in memory cannot be attached by two debuggers at the same time.  The operating system caters to the request on a first come first basis and refuses permissions to the second debugger.</p>
<p>The debugger&#8217;s access to a program&#8217;s memory and registers not only allows users to view the state of the program but also set software breakpoints.  Software breakpoints are set by altering and restoring the instructions in memory and has already been covered indepth in an <a href="http://www.technochakra.com/software-breakpoints/" target="_blank">earlier article</a>.</p>
<h3>How to modify code at runtime</h3>
<p>Altering instructions without compilation of code during debugging works on a similar logic.  If one uses the debugger to alter instructions of the program which it controls, then one can essentially change the logic compiled into the executable that is currently loaded in memory.  It is worth noting that altering a program in memory is temporary.  When the program is restarted and reloaded in memory, all changes are lost and no real harm has been done.  As this requires directly tweaking the opcodes in memory, one needs to know how to deal with assembly and this cannot be accomplished with knowledge of a high level language alone.<br />
Independent of the operating system and debugger, the steps needed to change the logic from within the debugger are as follows -</p>
<ol>
<li>Launch the program in a debugger.</li>
<li>Set a breakpoint in the code at the location which you want to alter execution.</li>
<li>Execute the program and drive the program so that your breakpoint is hit.</li>
<li>Request the debugger to display the disassembly of the code.</li>
<li>Get comfortable with the source-assembly mapping.</li>
<li>Identify the address of the assembly line you would like to alter.  Altering usually means changing an if statement to if NOT or &#8220;jump if zero&#8221; to &#8220;jump if not zero&#8221;.</li>
<li>Take the address and dump the memory contents at that address.  You will see opcodes corresponding to the assembly you just saw.</li>
<li>Modify the memory location with new opcodes (more on this later).</li>
<li>Cross your fingers so that your change won&#8217;t cause a crash <img src='http://www.technochakra.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Disable the breakpoint so that execution doesn&#8217;t stop too many times at this breakpoint as it makes debugging distracting.</li>
<li>Continue execution and now the program will respond to the new logic in the program.</li>
<li>To undo the effect of change, restart the program.</li>
</ol>
<h3>Example</h3>
<p>Let us look at the following simple snippet of code below.</p>
<p><code> bool flag = true;<br />
if( !flag )<br />
cout &lt;&lt; "flag is false" ;<br />
else<br />
cout &lt;&lt; "flag is true" ; </code></p>
<p>The code when compiled and run normally would print &#8220;flag is true&#8221; and this is obvious from the code above.  Say we at runtime would like to alter the logic by replacing if(!flag) with if(flag) so that the statement &#8220;flag is false&#8221; is printed instead.<br />
Let us see how to do this with the two most commonly available debuggers.</p>
<h4>GDB</h4>
<p>The steps below are in the same order as the generic steps explained earlier.</p>
<ul>
<li>gdb &lt;myprogram&gt;</li>
<li>break &lt;line_number_of_if_statement&gt;</li>
<li>run   Code will stop at the breakpoint set above.</li>
<li>disassemble.  The dump on my machine is as follows -</li>
</ul>
<blockquote><p>0&#215;00401170 &lt;main+32&gt;:   call   0x40f260 &lt;_alloca&gt;<br />
0&#215;00401175 &lt;main+37&gt;:   call   0&#215;410410 &lt;__main&gt;<br />
0x0040117a &lt;main+42&gt;:   movb   $0&#215;1,-0&#215;1(%ebp)<br />
0x0040117e &lt;main+46&gt;:   cmpb   $0&#215;0,-0&#215;1(%ebp)<br />
0&#215;00401182 &lt;main+50&gt;:   jne    0x40119a &lt;main+74&gt;<br />
0&#215;00401184 &lt;main+52&gt;:   movl   $0&#215;443000,0&#215;4(%esp)<br />
0x0040118c &lt;main+60&gt;:   movl   $0x4463a0,(%esp)<br />
0&#215;00401193 &lt;main+67&gt;:   call   0x43e720 &lt;_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc&gt;<br />
0&#215;00401198 &lt;main+72&gt;:   jmp    0x4011ae &lt;main+94&gt;<br />
0x0040119a &lt;main+74&gt;:   movl   $0x44300e,0&#215;4(%esp)<br />
0x004011a2 &lt;main+82&gt;:   movl   $0x4463a0,(%esp)<br />
0x004011a9 &lt;main+89&gt;:   call   0x43e720 &lt;_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc&gt;<br />
0x004011ae &lt;main+94&gt;:   mov    $0&#215;0,%eax</p></blockquote>
<ul>
<li>Note that main+67 and main+89 are calls to cout.  We have now identified the two important statements and the if check should be just above it.  It usually helps to corelate the assembly with the source code in this manner.</li>
<li>The if check is on lines main+42, main+46 and main+50 (if !(flag==0) ).  If we change jne to je, we can negate the if condition and thus alter the logic of the code.  The address of the opcode jne above is 0&#215;00401182.</li>
<li>x/6x 0&#215;00401182  will dump the opcodes and operands in hexadecimal.  In the dump below, 0&#215;75 is the opcode for jne and the operand is 0&#215;16.  For details, refer to the Intel instruction set.</li>
</ul>
<blockquote><p>0&#215;401182 &lt;main+50&gt;:     0&#215;75    0&#215;16    0xc7    0&#215;44    0&#215;24    0&#215;04</p></blockquote>
<ul>
<li>set *(char*)0&#215;00401182=0&#215;74 where 0&#215;74 is the opcode for je. Effectively &#8220;jump if not equal&#8221; has been changed to &#8220;jump if equal&#8221; thereby inverting the logic of the if statement.</li>
<li>cross your fingers.</li>
<li>disable  1.  Disable breakpoint set earlier.</li>
<li>continue</li>
</ul>
<p>You will observe that as we have modified the runtime code using gdb, the output of the program after the above steps would be &#8220;flag is false&#8221;.</p>
<h4>Visual Studio</h4>
<p><a href="http://www.technochakra.com/wp-content/uploads/assembly_vs.jpg"><img class="alignnone size-full wp-image-345" title="assembly_vs" src="http://www.technochakra.com/wp-content/uploads/assembly_vs.jpg" alt="Assembly Screen Shot in Visual Studio" width="566" height="111" /></a><br />
The above screen shot shows the debugger displaying the assembly after hitting the breakpoint at the if statement.  Do make sure that when you right click in the assembly, &#8220;Show Code Bytes&#8221; and &#8220;Show Address&#8221; are checked.<br />
From the screen shot above, the address of jne instruction is 0x003314B8 and its opcode is 0&#215;75.  If we modify the opcode at location 0x003314B8 and change it to 0&#215;74 (opcode for je), we effectively negate the if statement at runtime.</p>
<p>To do this, open the memory window (Debug-&gt;Windows-&gt;Memory-&gt;Memory 1) and enter 0x003314B8 in the Address field.  The first byte shows as 0&#215;75.  Put the cursor on 0&#215;75 and type 74 to change the opcode.  You can confirm this by seeing the effect in the disassembly window.</p>
<p>This modifies the code in memory and now when the program execution is continued, the output would be &#8220;flag is false&#8221;.</p>
<h4>Conclusion</h4>
<p>Another useful tip to keep in mind is that a lot of code can be disabled at runtime if all corresponding opcodes and operands are replaced with NOPs which has the opcode 0&#215;90 on an Intel machine.  Similarly useful results can also be achived by altering the operands instead of the opcodes.</p>
<p>The ability to change code at runtime is an effective tool for quick debugging though it requires a good understanding of the code at the assembly level.   This technique is meant for small tweaks because it is faster to recompile in case complex changes in assembly are needed.   Moreover, when the program finishes execution, the modification needs to be redone as this technique does not modify the image on the disk.</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-2898169159040774";
/* 468x60, created 7/1/09 */
google_ad_slot = "5882807646";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/debugging-modifying-code-at-runtime/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Cross-platform Debugging Cheat Sheet</title>
		<link>http://www.technochakra.com/cross-platform-debugging-cheat-sheet/</link>
		<comments>http://www.technochakra.com/cross-platform-debugging-cheat-sheet/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 06:06:36 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[visualstudio]]></category>
		<category><![CDATA[windbg]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=231</guid>
		<description><![CDATA[If you work on multiple platforms and use different debuggers, you are expected to know the debugger&#8217;s user interfaces well enough.  At times this gets confusing especially if you have one primary  platform and you work on other platforms rather infrequently.   I have compiled a list of my favorite features in a debugger and [...]]]></description>
			<content:encoded><![CDATA[<p>If you work on multiple platforms and use different debuggers, you are expected to know the debugger&#8217;s user interfaces well enough.  At times this gets confusing especially if you have one primary  platform and you work on other platforms rather infrequently.  </p>
<p>I have compiled a list of my favorite features in a debugger and how to invoke them on different debuggers (Visual Studio, XCode, gdb and Windbg). </p>
<p>This is not a substitute for the debugger&#8217;s documentation but helpful for quickly switching to an unfamiliar debugging environment.  Click the image below for viewing the table or download the PDF version as a ready reference.</p>
<p><a rel="attachment wp-att-249" href="http://www.technochakra.com/cross-platform-debugging-cheat-sheet/debuggersv01/"><img class="alignnone size-medium wp-image-249" title="Debugger Cheatsheet" src="http://www.technochakra.com/wp-content/uploads/debuggersV01-300x212.jpg" alt="Debugger Cheatsheet" width="300" height="212" /></a></p>
<p><a href="http://www.technochakra.com/wp-content/uploads/debuggersV01.pdf" target="_blank">Debugger Cheat Sheet (Download PDF)</a></p>
<p>Please note :</p>
<ul>
<li>The commands (especially for gdb) are not necessarily complete and the debugger&#8217;s help should be consulted for detailed usage.</li>
<li>The list is not comprehensive and I have only put in my favorite commands that I use while debugging.  </li>
<li>A square bracket [ ] denotes a keyboard shortcut.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/cross-platform-debugging-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Software Breakpoints</title>
		<link>http://www.technochakra.com/software-breakpoints/</link>
		<comments>http://www.technochakra.com/software-breakpoints/#comments</comments>
		<pubDate>Wed, 13 May 2009 19:05:53 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[breakpoints]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[visualstudio]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=192</guid>
		<description><![CDATA[It is very useful to be able to break execution of code at a line number of your choice.  Breakpoints are provided in debuggers to do exactly that.  It is fun getting to the root of the problem by setting breakpoints in a debug session.  It is even more fun to know how do breakpoints work in the first place. ]]></description>
			<content:encoded><![CDATA[<p><em>Line breakpoints can be implemented either in hardware or software.  This article discusses the latter in detail. </em></p>
<p>It is very useful to be able to break execution of code at a line number of your choice.  Breakpoints are provided in debuggers to do exactly that.  It is fun getting to the root of the problem by setting breakpoints in a debug session.  It is even more fun to know how do breakpoints work in the first place.</p>
<p>Software breakpoints work by  inserting a special instruction in the program being debugged.  This special instruction on the Intel platform is &#8220;int 3&#8243;.  When executed it calls the debugger&#8217;s exception handler.</p>
<h3>Example</h3>
<p>Let us look at a very simple example that inserts a breakpoint in a program at compile time and not through a debugger.  The code uses the Intel instruction &#8220;int 3&#8243; and you may need to figure out the equivalent instruction for a non-Intel platform.<br />
<code><br />
// The code below works well with Visual Studio.<br />
int main()<br />
{<br />
__asm int 3;<br />
printf("Hello World\n");<br />
return 0;<br />
}</code><br />
<code><br />
// The code below works well with gcc + gdb<br />
int main()<br />
{<br />
asm("int $3");<br />
printf("Hello World\n");<br />
return 0;<br />
}</code></p>
<p>If you run this program in Visual Studio, you get a dialog saying &#8220;<em><strong>helloworld</strong><strong>.exe has triggered a breakpoint</strong></em>&#8220;.</p>
<p>In gdb you get the message &#8220;<strong><em>Program received signal SIGTRAP, Trace/breakpoint trap.</em></strong>&#8221;</p>
<p>In the example above, a call to &#8220;int 3&#8243; invokes the debugger&#8217;s exception handler.</p>
<p>It is also interesting to note the assembly instructions generated for the program above.</p>
<p>In Visual Studio, right click on the code and click on &#8220;Show Disassembly&#8221;. Also ensure that &#8220;Show Code Bytes&#8221; is on in the same context menu.</p>
<div class="mceTemp">
<dl id="attachment_198" class="wp-caption alignnone" style="width: 455px;">
<dt class="wp-caption-dt"><img class="size-full wp-image-198" title="vs2008_disassembly" src="http://www.technochakra.com/wp-content/uploads/vs2008_disassembly.jpg" alt="Visual Studio 2008 Disassembly" width="445" height="49" /></dt>
</dl>
</div>
<p>In gdb type disassemble at the gdb command.</p>
<blockquote><p>0x0040107a &lt;main+42&gt;:   int3</p></blockquote>
<p>Now obtain the opcode of the int3 instruction using the x (examine memory) command</p>
<blockquote><p>(gdb) x/x main+42</p>
<p>0x40107a &lt;main+42&gt;:     0xcc</p></blockquote>
<p>As seen above, the breakpoint opcode we inserted during compilation is 0xCC .</p>
<h3>How Do Debuggers Insert Breakpoints?</h3>
<p>For practical reasons, it is unwise to ask for a recompilation whenever a breakpoint is added or deleted.  Debuggers change the loaded image of the executable in memory and insert the &#8220;int 3&#8243; instruction at runtime.  The common steps a debugger performs to provide the functionality of a line breakpoint to a user are as follows -</p>
<ol>
<li> When a user inserts a breakpoint in a line of code, the debugger saves the opcode at that given location and replaces it with 0xCC (int 3).</li>
<li>When the program is run and it executes the &#8220;int 3&#8243; instruction, control is passed to the debugger&#8217;s exception handler.</li>
<li>The debugger notifies the user that a breakpoint has been hit.  Say that the user instructs the debugger to resume execution of the program.</li>
<li>The debugger replaces the opcode 0xCC with the one it had saved earlier.  This is done to restore the instructions to their original state.</li>
<li>The debugger then single steps the program.</li>
<li>It then resaves the original instruction and re-inserts the opcode 0xCC.  If this step were not done, the breakpoint would have been lost.  <a href="http://www.technochakra.com/temporary-breakpoint-now-you-see-it-now-you-dont/">Temporary breakpoints</a> on the other hand skip this step.</li>
<li>The debugger then resumes execution of the program.</li>
</ol>
<p>Hardware breakpoints are limited in number but debuggers are able to provide unlimited breakpoints by implementing them through software.</p>
<p>Knowing what goes behind the scenes makes debugging a bit easier.  A debugger may defer setting a breakpoint if the module is not loaded in memory yet.  It needs to replace some opcode with 0xCC and that can happen only when the module is in memory.  Likewise, a mismatch between a binary, its sources and its debug symbols (or the lack of it) may cause breakpoints to be hit at unexpected locations because the debugger is not able to correctly map the source line to the opcode that it needs to replace with 0xCC.  At times debuggers complain about the mismatch and refuse to set the breakpoints.</p>
<p>Many of the setup issues with breakpoints become obvious once we know how they work internally.  And when all else fails and release build breakpoints  adamantly refuse to work, you always have the option of compiling an &#8220;int 3&#8243; breakpoint right into your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/software-breakpoints/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
