Archive for the ‘CodeDebugging’ Category

I’m Aware My Code Is a Sad Affair

No Comments »

I was recently asked to hunt a bug in some of my old code. It was a shell script generating Postscript, generating .PNG files for a website. It had “organically” grown over some years and started generating bad Postscript starting May 1st. Since Postscript is well designed and has good error handling, it did not take long to find the problem. The word May had not been replaced by a 120 (the year day number of May 1st). Here is a sniped:


Note the ‘r’ that should be a ‘y’ in the center LOC. My bad. However, I realized that, in theory, this error could have been caught (by sed) because the second /[Mm]ar[^ ]*/ would not ever be satisfied. Sadly, I can’t make sed find my bad code, but I can dream, can’t I?

Eroor Propagation

1 Comment »

Basta-Pilot-Steady-Switch-LEDOk, it was 1 am as I switched off the TV and only than I remembered I had to fix the flat tire on my kid’s bike. No biggy, this would only take 20 minutes, so I get my gear and start to work. Bummer, I need to take out the wheel, the leak is too small to hear. Ok, maybe 30 minutes, no worries. So I disengage the hub dynamo, noting the plus wire has a white stripe on it. I find the hole, fix it, reassemble the tire, and in my head I am already in my bed, in just 2 more minutes everything is properly reaffixed. I test the light as a matter of double compulsion. After all I am both a coder as well as a caring father. The light refuses to work. Maybe the connector is dirty. I start debugging, working my way up from the hub to the lamp. Twenty minutes later, I have the whole thing off the bike, stripped naked on the table and I take my multimeter to it. I curse these digital IC controlled lights. It used to be so simple. Just one wire connecting a dynamo and a bulb. (Or spit into the carbide lamp.) Whoever asked for a “wunderlamp”? After an other five minutes, I find out the bulb looks ok, but the meter tells me it’s broken. Curses! Why did my kid not tell me her light needed fixing? I would have checked the bulb first thing. Because of the flat tire I started debugging at the wrong end. Because I assumed the light was working before, I assumed the connector was the most probable thing to be at fault. That good all MOAFU called assumption. It is bad enough that I have to endure bad error propagation at work, but from my own blood?

Clock Arithmetic Still Hard


Clock or modular arithmetic is hard. Take a look at the two code snippets below. Both seem reasonable integer comparison functions modeled after strcmp(). However, the left one contains an overflow bug.

BuggyIntCmp ClassicIntCmp

I have thirty odd years of programming experience, yet I had to be pointed out the bug. The left snippet looks obviously wrong to me, now. It’s like claiming that we go back in time if the short hand of a clock moves more than 6 hours. Ridiculous. (And, no, intermediate casting to long won’t help.) Just try a few extreme cases using INT_MIN, for example. Clock arithmetic is hard because if the modulus is sufficiently large we tend to ignore it. SimplifiedIntCmp So, if you have to go against the first directive, if you don’t trust the -O4 option, or if you just have to show off how brilliant you are, use something like the C code on the right. It won’t bring you much, but at least it does not contain an overflow bug. Though it might contain an other one, or show a incompatibility in your compiler. But that is left as an exercise to the reader.

Debugging 27 Year Old Code

1 Comment »

Remember those days? About twenty seven years ago? You were on a role with Donkey Kong, playing at some high level—level twenty two, actually. And boom, time is up, you’d be dead. That is, Jumpman (“Mario,” to you DSi wielding youngsters) would be dead. Dead as a doornail, how unfair! My, then, therapist told me, that it was not as important as I thought it was, the moron. That I would forget it eventually, and go on with my life, double moron. How can I forget? I still cry myself to seep every night over how unfair life is. But now, thanks to Don Hodges, the world (“my world” as my current therapist refers to it) is back in harmony again. Thanks Don, for fixing Donkey Kong. I can sleep in peace, now.

This is what the (overflow) bug looks like:

0F7F   17 RLA        ; Rotate Left the bits in A
0F84   80 ADD A,B    ; A = A + B
0F85   80 ADD A,B    ; A = A + B
0F86 C628 ADD A,#28  ; A = A + #28 (40 decimal)
0F88 FE51 CP #51     ; Is A >= #51 (81 decimal) ?
0F8A 3802 JR C,#0F8E ; No, then skip ahead to #0F8E
0F8C 3E50 LD A,#50   ; Yes, then A = #50 (80 decimal)

Look Ma No Web Developer Toolbar

No Comments »

I use the following bit of CSS to help visualize the structure of an XHTML (or HTML) document by putting a colored outline around the border of every element. At each level in the hierarchy the color changes so you can see when “depth” changes.

  * { outline: 2px dotted red }
  * * { outline: 2px dotted green }
  * * * { outline: 2px dotted orange }
  * * * * { outline: 2px dotted blue }
  * * * * * { outline: 1px solid red }
  * * * * * * { outline: 1px solid green }
  * * * * * * * { outline: 1px solid orange }
  * * * * * * * * { outline: 1px solid blue }

I usually keep this block of rules at the top of a stylesheet, commented out with /*…*/, which I remove when I want to see the structure. (Chris Page) [A Handy CSS Debugging Snippet] Almost as handy as Web Developer Toolbar Add-on for Firefox. [via: DI]

Nerd Test

No Comments »

“The following gdb script will handle this:

# Trace objective-c messages. - nemo 2009
b dyld_stub_objc_msgSend
        set $id  = *(long *)($esp+4)
        set $sel = *(long *)($esp+8)
        if(*(long *)($id+8) != 0)
                printf "[%s %s]\n", *(long *)($id+8),$sel
        set $isx = *(long *)($id)
        printf "[%s %s]\n", *(long *)($isx+8),$sel

We could also implement this with dtrace on Mac OS X quite easily.

#!/usr/sbin/dtrace -qs
/* usage: objcdump.d  */
    self->isa = *(long *)copyin(arg0,4);
    printf("-[%s %s]\n", 
        copyinstr(*(long *)copyin(self->isa + 8, 4)),

Let me correct myself on that, we /should/ be able to implement this with
dtrace on Mac OS X quite easily. However, dtrace is kind of like looking at
a beautiful painting through a kids kaleidescope toy.”
(nemo) [The Objective-C Runtime: Understanding and Abusing]

If you are only interested in the above text you are a halfling, if you just hit the link to RTFA, you are a nerd, if you think you could write the article, you are a script-kiddy, and if you go WFT, you are a n00b. But if you do read it, you might learn some new debugging techniques.