Using WinDbg to Log Exceptions
September 17th, 2005

Since I discovered that I am currently—and quite inexplicably—the #1 Google Blog Search result for the term “WinDbg,” I decided that I would attempt to actually produce some WinDbg content.

If you are not familiar with WinDbg, check it out with the rest of the Debugging Tools for Windows here.

To get things started, I thought I would post some examples that I’ve found useful lately.

Introduction

I use this technique quite a bit when I’m doing QA work on an application. I can think of a bunch of other scenarios where it might be useful, but one chore in particular that is well suited for this is finding ASP.NET page compilation exceptions.

There isn’t really a good way to clean up page compilation exceptions in a massive ASP.NET application. Why would you bother? Well, there are a few reasons.

  1. They encourage you to disable break-on-exceptions in a debugger like VS.NET, since you’ll be inundated with a lot of other people’s exceptions when trying to attach to the worker process. If you do that, you might miss swallowed exceptions or exceptions on background threads that you would have otherwise noticed and fixed.
  2. They might indicate a problem in the HTML being sent back to the user.
  3. They cause your application to take longer to start up.
  4. Some of us are just sticklers for the details, ok?
  5. So, one good way to trim them down to a bearable amount is to log all of the exceptions that happen when the app starts.
Getting Started

The first thing you need to do is open up a log:

0:003> .logopen c:\temp\exceptions.txt
Opened log file 'c:\temp\exceptions.txt'

And if this is managed code you’ll want to load the SOS extension:

.load clr10\sos

We’ll use a breakpoint with a custom command to print out the stack when the exception occurs.

0:003> bp kernel32!RaiseException "!clrstack!clrstack; g"

The !clrstack command will print out the stack, and g (go) resumes execution. If the application is unmanaged, you will want to replace !clrstack with one of the k (display stack) commands. Both managed and unmanaged code will use the same underlying call to the kernel32!RaiseException function.

Once you have this set up, you’ll see a trickle or a flood of stacks, depending on the relative health of your app. They’ll probably be more interesting than this:

(848.d18): CLR exception - code e0434f4d (first chance)
Thread 0
ESP         EIP
0x0012f648  0x7c81eae1 [FRAME: HelperMethodFrame]
0x0012f674  0x02e000a0 [DEFAULT] Void CsConsoleApp.Program.Main(SZArray String)
    at [+0x48] [+0xf] c:\src\scratch\csconsoleapp\class2.cs:26
0x0012f8b0  0x791d94bc [FRAME: GCFrame]
0x0012f9b0  0x791d94bc [FRAME: GCFrame]
0x0012fa94  0x791d94bc [FRAME: GCFrame]

But you get the idea.

Adding More Information

Now, notice that in the case of a managed exception, WinDbg didn’t give us much information, other than the fact that it occurred:

(848.d18): CLR exception - code e0434f4d (first chance)

It would be nice to get the exception text, right? You can do that by changing the breakpoint to this:

0:000> bp mscorwks!JIT_Throw "du poi(@ecx+10)+c; !clrstack!clrstack; g"

Homework: explain this command and how I came up with it. I will post the answer soon.

Now we’ll get something more useful:

(848.d18): CLR exception - code e0434f4d (first chance)
00aaab88  "You stink!"
Other Mods

One thing you might want to do in the case of rarer exceptions is create a dump of the process. This is especially useful if your issue only happens in production. The simplest way to do that is very similar to what we just did:

0:003> bp kernel32!RaiseException ".dump /ma /o c:\\temp\\myapp.dmp; g"
breakpoint 0 redefined

The /ma switch produces a full dump. I set this breakpoint to overwrite a single dump file.

More homework: modify the breakpoint to create a series of dumps. The answer will be posted in the near future.


Dvorak at it Again
September 14th, 2005

John Dvorak recently posted this plug for what appears to be a pseudoscience site, holoscience.com. Quote:

This site is devoted to looking at space science in a holistic, interdisciplinary manner. It also has some exceptionally cool images.

The comments on his blog appear to be either disabled or not working, so I will post a reply here.

This site appears to be yet another “theory of everything, the scientific establishment is against me” kind of site. Example quote:

“The Big Bang is already dead! The unheralded “Galileo of the 20th century”, Halton Arp, has proven that the universe is not expanding.”

Everything written in the News area of the site appears to be self published and not peer-reviewed. There seems to be a war going on at Wikipedia over another apparently self-published article about the “Electric Universe” theory espoused on the site. View the discussion here.

Hi William, please refer to the list of papers on the article page going back to 1930 to show serious study in this field. A Google search will reveal 6,240 pages returned on a search for “‘electric universe’ + plasma” …. . —Bongani 19:38, 9 July 2005 (UTC)

No, thats a long list of papers which have something to do with electricity in space, and have nothing to do with EUM. One of the characteristics of pet/psuedo science [sic] is appropriating irrelevant papers that have the right-looking words in them. Any article that states That the Sun and stars are powered by an external electric current is (to be frank for just a moment) wacko nonsense. Any article that makes such a controversial statement, and then fails to back it up with a peer-reviewed paper, is unsuitable for wiki as “original research”. … William M. Connolley 20:05:57, 2005-07-09 (UTC).

And as you can see the neutrality of the original article is disputed.

I can’t believe Dvorak posted this, frankly. But it fits with the horoscope ad displayed prominently on his site. Holoscience does have “purty pictures,” though.

Update - It appears that Dvorak might have posters on his site other than himself. Not sure why or who they are, but I thought I should mention that the opinion I quoted above may not be directly attributable to the popular PC Magazine columnist.


Don Quixote vs. the Bookstore, Part II
September 13th, 2005

Here is the latest in my correspondence with a bookstore “recommending” Kevin Trudeau’s “Natural Cures ‘They’ Don’t Want You to Know About.”

Click here for Part I

From: Gary McBrayer

To: Dan McKinley


Dear Mr. McKinley:

Thank you for your email regarding your concerns about the title “Natural Cures They Don’t Want You to Know About”. While we understand that this book has created controversy regarding its content, it is also a title being requested by a number of our customers. Our position as a Company is to offer our customers the opportunity to view its contents and to decide for themselves whether or not to purchase the book and accept or reject the information and recommendations of its author. However, I am forwarding your comments to our Vice-President of Book Buying and Promotions so that she is aware of your concerns about the manner in which the title is being displayed in our stores.

We value your input and appreciate your patronage. Thank you for contacting us.

Sincerely,

Gary McBrayer Manager of Customer Service

 

From: Dan McKinley

To: Gary McBrayer


Mr. McBrayer:

Thank you for your reply. While I certainly respect—and advocate—your legal right to stock any book you please, I cannot accept the idea that you bear no responsibility for what you offer for sale.

Should you have the constitutional right to stock a book advocating suicide? A book published by NAMBLA? I might err on the side of saying you should have that prerogative, though in neither case could it be argued that the advice is victimless when followed.

The existence of that prerogative does not make such an action on your part responsible or morally defensible. I submit that peddling a book that provides sick persons with a negative portrayal of modern science and medicine is only marginally more appropriate.

I point out again that the book was not merely on your shelves, it was shelved as a recommended book.

I thank you for your time.

-Dan McKinley