<?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>M20 iPhone &#38; iPad development</title>
	<atom:link href="http://iphone.m20.nl/wp/feed/" rel="self" type="application/rss+xml" />
	<link>http://iphone.m20.nl/wp</link>
	<description>Better iOS software one page view at a time</description>
	<lastBuildDate>Wed, 27 Apr 2011 08:01:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Memories… programmer friendly assertions in Xcode</title>
		<link>http://iphone.m20.nl/wp/2010/10/xcode-iphone-debugger-halt-assertions/</link>
		<comments>http://iphone.m20.nl/wp/2010/10/xcode-iphone-debugger-halt-assertions/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 21:08:05 +0000</pubDate>
		<dc:creator>m20</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.m20.nl/wp/?p=1</guid>
		<description><![CDATA[Not even a full day of having Wordpress set up, and already an article of staggering genius for iPhone/iPad developers: how to neatly break into the debugger from code in your application.]]></description>
			<content:encoded><![CDATA[<p>Well, things certainly tend to come around. After <a title="Me, six years less wise" href="http://www.omnigroup.com/mailman/archive/macosx-dev/2004-September.txt" target="_blank">almost exactly 6 years</a>, I once again find myself wanting to programmatically pause a program at interesting times for inspection using gdb. Where the code decides what&#8217;s interesting, mind you, not the developer&#8217;s symbolic breakpoint. Those six years have brought a new platform (iOS) and three new CPU architectures, so that trusty macro referenced in the link needs some updating. And update it I did &#8211; see the code fragment below.</p>
<p>O, and by the way, that&#8217;s gdb in its friendly Xcode disguise, of course. There&#8217;s a limit to how much mental abuse I can take, and that limit is comfortably reached by having to manually manage memory in Objective-C. I feel your pain, my embedded hardware brethren.</p>
<h3>What I want…</h3>
<p>So, we need to insert some code to break at a certain point or condition. A staple assert (false) won&#8217;t do, because there&#8217;s only so much you can do after the assert fires the near-fatal SIGABRT signal. You won&#8217;t be able to continue the program, or rewind the program counter. You probably won&#8217;t be able to use all that symbolic debug information in a very meaningful way. Admittedly, you can probably set up signal handlers in the debugger to pause on SIGABRT, but being a developer, you will forget that at least on the one occasion when it really mattered. And sometimes the interesting time is just interesting, not the beginning of the end of your program.</p>
<p>But there are other signals! SIGINT is a fine candidate to bring up the debugger. It will catch the signal and generally save your life. But there&#8217;s a snag. And another snag. The first is that SIGINT will kill your program when not run under the debugger. Unless you set up a signal handler or sigmask, but you&#8217;ll only do that in at most one or two of your applications. This is bad, because I&#8217;ll often just Run, not Debug,  development builds trusting Xcode to debug &#8220;just in time&#8221;. And this works fine for, e.g., segmentation violations. So why shouldn&#8217;t I have that for my programmatic breakpoints?</p>
<p>The second snag when using a plain kill is that the debugger will stop in the <strong>wrong stack frame</strong>. I have chosen the bold type to prevent the use of expletives. This frame mismatch requires me (and you, should you choose this ill-advised way) to navigate all of the two stack frames up to the point where your actual call (and probably the failed condition) is. Only two clicks of the &#8220;step out&#8221; button for the casual observer, but a huge productivity and mood sink for the developer who&#8217;s been observing the app not so casually for half an hour waiting for the bug to occur. This does not happen with gdb breakpoints. This does not happen with segmentation violations. So why should I have to put up with it for my programmatic breakpoints?</p>
<p><span id="more-1"></span></p>
<h3>…is what you get</h3>
<p>Fortunately, here is a solution that does not have these drawbacks. Using the original 2004 approach no less, ported to Intel and ARM. In a nutshell, the code has the following features. Most importantly, it breaks at the right stack frame and source line (using an inline asm block to generate the syscall to SYS_kill). And when run under the debugger, the debugger will come to the front just as if a normal breakpoint was reached, for your stepping and symbol-printing pleasure. Even better, when not running under the debugger (i.e., when started directly from Springboard), the application will simply suspend itself so you can safely attach the debugger when returning from a field test (aka the coffee corner).</p>
<p>There are two small issues with the simulator left, waiting for some heroic, last-man-standing developer to solve them. In the simulator, when started using the &#8220;Run&#8221; command instead of the &#8220;Debug&#8221; command, the app will suspend itself instead of attaching just-in-time. You&#8217;ll notice soon enough, though, and a simple click of the &#8220;Pause&#8221; button in the debugger will do what it purports to do. And due to a more general non-feature of Xcode, if you started the app using Springboard and it hits the breakpoint code, you&#8217;ll have to attach the debugger by typing in the apps process ID.</p>
<h3>The Codes</h3>
<p>The AmIBeingDebugged function is courtesy of Apple. Get the original here: <a href="http://developer.apple.com/library/mac/qa/qa2004/qa1361.html">http://developer.apple.com/library/mac/qa/qa2004/qa1361.html</a></p>
<p>And don&#8217;t forget that you might need to #import (or #include) &lt;sys/signal.h&gt; and &lt;unistd.h&gt; in files where you&#8217;ll be using these macros.</p>
<p><code><br />
// In a suitable header<br />
extern bool AmIBeingDebugged (void);<br />
#if TARGET_CPU_ARM<br />
#define DEBUGSTOP(signal) __asm__ __volatile__ ("mov r0, %0\nmov r1, %1\nmov r12, #37\nswi 128\n" : : "r" (getpid ()), "r" (signal) : "r12", "r0", "r1", "cc");<br />
#define DEBUGGER do { int trapSignal = AmIBeingDebugged () ? SIGINT : SIGSTOP; DEBUGSTOP(trapSignal); if (trapSignal == SIGSTOP) { DEBUGSTOP (SIGINT); } } while (false);<br />
#else<br />
#define DEBUGGER do { int trapSignal = AmIBeingDebugged () ? SIGINT : SIGSTOP; __asm__ __volatile__ ("pushl %0\npushl %1\npush $0\nmovl %2, %%eax\nint $0x80\nadd $12, %%esp" : : "g" (trapSignal), "g" (getpid ()), "n" (37) : "eax", "cc"); } while (false);<br />
#endif</code></p>
<p><code> </code></p>
<p><code>// In an implementation file, far far away<br />
extern bool AmIBeingDebugged (void)<br />
{<br />
int mib[4] = { CTL_KERN,  KERN_PROC,  KERN_PROC_PID, getpid () };<br />
struct kinfo_proc info = { 0 };<br />
size_t size = sizeof (info);<br />
sysctl (mib, sizeof (mib) / sizeof (*mib), &amp;info, &amp;size, NULL, 0);</code></p>
<p><code> </code></p>
<p><code>// We're being debugged if the P_TRACED flag is set.<br />
return (info.kp_proc.p_flag &amp; P_TRACED) != 0;<br />
}</code></p>
<p><code> </code></p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.m20.nl/wp/2010/10/xcode-iphone-debugger-halt-assertions/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

