How to debug

I see an awful lot of posts on StackOverflow that show that the person asking the question hasn’t got the slightest clue how to go about debugging their problems. So here’s a few specifics for a few extremely common situations:

1. It’s not a bug in the compiler
It’s not a bug in the compiler, it’s never a bug in the compiler. Stop making that your default assumption. I’ve been a programmer for over 25 years, and the only time I saw a bug in the compiler was in the early versions of cfront, which was AT&T’s way to convert C++ programs into C programs so you could compile and link them with C tools. If you think there is even the slightest possibility that it’s a bug in the compiler, you’re going to stop looking before you see what you did wrong. And yes, you did something wrong. Similarly…

2. It’s probably not a bug in the library routines
The probability of a bug in the library routines depends a lot on the number of people using it. If it’s a core part of Java, chances are you’re not the first person to notice something the other 25 million Java developers somehow overlooked. If it’s a project that you found on SourceForge that hasn’t been updated in 4 years and only had one developer, it’s a possibility, but one you should discount until you’ve made sure you’re calling it right.

3. Null Pointer Exceptions happen for a reason
If you got a NullPointerException in your code, or any type of exception in library code, you did something wrong. Look at the stack trace. Look for the top-most entry that is your code. Look at the line there. Think about what you see on that line. Can one of those variables be null? Did you initialize all the variables in every possible way through the code to that point? Are you giving the correct arguments to whatever library code you’re calling? If necessary, put a breakpoint there or throw in some debugging statements and print out what you’re using in that line to make sure they’re what you expected.

4. Debugging AJAX calls is hard, but it’s easier than trying to explain it on StackOverflow
A large number of questions are of the nature “I do this ajax call, and it doesn’t work”. What doesn’t work? Are you making the call? Is the server receiving it? Is the server doing the right thing with it? Is it passing back what you’re expecting?

The first thing you need to do is use a good debugger. If the problem happens on Firefox, then you’re in luck because you can use Firebug + Firequery.

If you’re unlucky, and the problem only happens in IE (and face it, those are your only two alternatives because if a problem happens in any non-IE browser, it happens in all of them, whereas if you code works in IE8 you’re not 100% sure it works in IE7 or IE9), then you need to use whatever debugger options are available to you. I found some useful information here and I end up using a combination of Firebug Lite and IE Developer Toolbar. Fortunately most of the IE8 and IE7 problems I’ve encountered happen in IE9 with the Browser Mode and Document Mode set appropriately.

Once you’ve got your debugger up, you want to set a breakpoint on the actual ajax call (to verify that you’re actually getting to the call and not missing it for some other reason), on the success callback (to verify that the server has sent a response) and on the failure callback (to verify that the server didn’t throw up its hands and give up). It also helps if you’ve got access to the server side logs and can see what’s going on there as well, but that’s often not possible like when you’re calling somebody else’s web API. In the IE debugger, you need to go to the Network tab and “Start Capturing”, and in Firebug you just need to look at the “Console” tab. After the ajax call returns, you can look at the appropriate tab and see what was sent to the server and what came back. And in the success callback you can look at the returned response and single step through the logic to see if you’re doing the right thing with it. And you can do all that in less time than it would take to write a question to StackOverflow. If you’re still stumped, you also have a lot more information you can put in your question, which will help all the eager question answerers out there who don’t have the ability to step through your code.

5. A question and answer site is not the place to learn the syntax of a language
If you code doesn’t even compile, then you don’t know enough to ask a question that is useful to either you or the other users of StackOverflow. Pick up a book and learn the basics.

Internet Exploder, I hate you so much

Yesterday was a fun day in the continuing struggle against IE brokenness.

First problem: the form submit button used to work on IE, but now it doesn’t. Well, no matter, because the form had an onsubmit that did some AJAXy stuff and then cancelled the form submit. Rather than wasting time trying to figure out why it works on real browsers and not on IE, I just changed the submit button into an ordinary button that invoked my function. Problem solved.

Second problem: My form is very dynamic, allowing you to add, delete or clone table rows, each of which contains several select, checkbox, and textarea input fields, all with associated onchange or onclick callbacks. The problem was that when you cloned a row, the callbacks on the new row would apply to the original row. All the callbacks had the row id in the arguments list, and when I clone I use the jquery attr command and a regular expression to change the row id. That works for real browsers, and it apparently works in IE (if you examine the code in Firebug you see the new id), but apparently the actual callback data is stored internally somewhere. It didn’t seem to matter whether I called clone with true or false in the copyData argument. So I restructured all my callbacks so the were activated by the jquery on command, and grabbed the row id and other arguments using the jQuery(this).parents('tr').

It was annoying to have to do all this stuff because IE is so different from real browsers, but the code is probably better for it.

Somebody doesn’t quite understand how to configure their spamming software…

Saw this one in the comment spam for this blog: (the name of the product and other details elided)

{To my surprise|As it turned out}, {I stumbled upon|I ran across|I stumbled onto} {this program|this system|the program|this method|the deal|this product} by XXXX XXXXX. “XXXX XXXXX!” {caught|captured} my attention. {At first|In the beginning|Initially|At the beginning} {I was|I had been|I’d been} {baffled|confused|puzzled} by the name of the program. {But|However} as I read along, {I was|I had been|I became} convinced that {it could|it might|it may|it could possibly|it may possibly|it could actually|it would|it will|it may well|it will probably|it will possibly|it would likely|it can|may well|may possibly} {help me|assist me}. I’ve been {following the|following a|pursuing the|using the} {program|system|method|technique} {for 2|for two|for just two} weeks now. {Saying that|Stating that|Stating} I’m {seeing|experiencing|discovering|finding|witnessing|observing|having} results is an understatement! {The results|The outcomes} {are amazing|are fantastic}! Never {in my|during my} life did {I think|I believe} {that there|there} {would be a|will be a|has to be|has got to be} {solution to|treatment for} my XXXX {woes|problems|worries|issues|troubles} until {I found|I discovered|I came across|I ran across|I stumbled upon} “XXXX XXXXX!”

Well, that didn’t last long

A few hours after my last post, as I slept not very soundly, the hard disk that kept dropping out of the RAID as it rebuilt dropped again. This morning, I booted in the the Seagate “SEATools” disk and gave it a full on test, and it found a metric buttload of bad sectors. Not good on a brand new disk, so off to the RMA process I go. The machine is up and running again, with /dev/md128 running as a “degraded RAID 1” (ie a single drive with no mirror).

Does anybody else find it pretty shitty that the vendor can ship you a defective drive, and then make you pay for the shipping to return it? Or that they won’t ship the replacement until they get the old one back? Seems to me that they could ship a replacement immediately with the proviso that they’ll charge your credit card for the drive if you don’t return the old one.

A new start

I decided I needed some more room on my home Linux box, so I bought a couple of 2TB disks. I also decided it was time to do a wipe and re-install because the box has been upgraded many times and there are several things that just don’t work right any more. So I burned a bunch of different distros onto CDs and experimented with them, and decided that Kubuntu is the best combination of beauty and power. None of the other distros except Debian allows you to set up lvm on RAID-1 while installing, and Debian looks like a bit of a dogs breakfast compared to Kubuntu, even in KDE.

The two new disks are so big that as it is, I’m only using them for the new installation, so the I can mount the old system under /old_system and I can compare everything to make sure I haven’t lost any important configuration. But eventually I’ll be able to delete those partitions and add them back to the free pool in the LV and use them to expand any of the actual in-use partitions. I love lvm.

Installation has gone ok, I’ve got about 3/4s of the things I need to get working again working again, but I’ve hit the usual string of snags. The biggest is that one of the two new disks keeps dropping out of the RAID1, so I have to keep adding it back in. I’ve rejiggered all the cables as best as I can, and hopefully it will rebuild overnight and it will be ok. Fingers crossed, I’m off to bed.