WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

You Can Hack .NET Without Buying Visual Studio .NET
Pages: 1, 2

Cordbg.exe, the .NET Debugger

The .NET Framework SDK utilities include a debugger, and if you learn to use it, your life as a .NET programmer will be much simpler. Let's use the debugger (cordbg.exe) to examine this problem. First, start the program under control of the debugger. You are greeted by the (cordbg) prompt. The command run Hello.exe is invoked automatically. Ignore that warning about mscorlib.dll; this just tells you that you can't debug the core library that contains many important Framework Class Library types:




C:\home>cordbg Hello.exe
Microsoft (R) Common Language Runtime Test 
Debugger Shell Version 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001.
All rights reserved.

(cordbg) run Hello.exe
Process 1132/0x46c created.
Warning: couldn't load symbols for 
c:\windows\microsoft.net\framework\v1.0.3705\mscorlib.dll
[thread 0xe10] Thread created.

007:     string[] words = {"hello", "goodbye"};
(cordbg)

The first thing you'll do is set a breakpoint on the offending line with the b command, and start the program with go. This will stop execution in the first iteration of the loop and prompt you for the next action:


007:     string[] words = {"hello", "goodbye"};
(cordbg) b 11
Breakpoint #1 has bound to C:\home\Hello.exe.
#1    C:\home\Hello.cs:11   Main+0x1d(il) [active]
(cordbg) go
break at #1 C:\home\Hello.cs:11
Main+0x1d(il) [active]

011:       Console.WriteLine(words[i]);
(cordbg)

At this point, the variable i should be set to zero, and words[i] should be "hello". You can use the print command to test this out:


(cordbg) print i
i=0x00000000
(cordbg) print words[0]
words[0]=(0x00c41844) "hello"

You can repeat this for the next iteration of the loop:


(cordbg) go
hello
break at #1   C:\home\Hello.cs:11
Main+0x1d(il) [active]

011:       Console.WriteLine(words[i]);
(cordbg) print i
i=0x00000001
(cordbg) print words[1]
words[1]=(0x00c41860) "goodbye"

Since there are two elements in the array, that should have been the last iteration of the loop, right? But when you type go for a third time, the program enters the body of the loop again:


(cordbg) go
goodbye
break at #1   C:\home\Hello.cs:11
Main+0x1d(il) [active]

011:       Console.WriteLine(words[i]);

And what's worse, the current value of i is outside the boundaries of the array (the array goes from 0 to 1):


(cordbg) print i
i=0x00000002
(cordbg) print words
words=(0x00c41880) array with dims=[2]
  words[0] = (0x00c41844) "hello"
  words[1] = (0x00c41860) "goodbye"

Use the show command to list five lines before and five lines after the current line of code:


(cordbg) show
006:     // create a list of strings
007:     string[] words = {"hello", "goodbye"};
008:     // iterate over the list
009:     for (int i = 0; i <= words.Length; i++)
010:     {
011:*      Console.WriteLine(words[i]);
012:     }
013:   }
014: }

Take a look at line 9, where the loop is controlled. It seems that I've introduced an off-by-one error by using <= instead of <. Problem solved! Quit the debugger with the quit command and open up your text editor again to correct this error. The following example shows the corrected program:


using System;
public class Hello 
{
  public static void Main()
  {
    // create a list of strings
    string[] words = {"hello", "goodbye"};
    // iterate over the list
    for (int i = 0; i < words.Length; i++)
    {
      Console.WriteLine(words[i]);
    }
  }
}

And here is the output of running the program--no error this time!


C:\home>Hello
hello
goodbye

A Few Good Text Editors

Notepad is useful, but it doesn't have a huge set of features. For some people, this is just right; others may need something more featureful. Here is a list of C# editors I have experimented with:

VIM (Vi IMproved)
VIM is a vi-compatible editor with many extra features. Grab csharp.vim to get C# syntax support.
GNU Emacs for Windows
GNU Emacs is an editor favored by many programmers for its completeness and extensibility. You can find a C# syntax mode for Emacs here.
jEdit
jEdit is a Java editor written in Java that has support for C# syntax.
SharpDevelop
SharpDevelop is a free C# and VB .NET editor that's quite featureful and popular. It's completely written in C#, released under the GNU GPL, and offers features typical of an integrated development environment (IDE).

For more C# editors, see csharpindex.com's list of text editors.

Other C# Compilers

If you're not using a Windows platform but you'd still like to play around with C#, check out Mono or dotGNU, two projects that provide ECMA-compliant C# compilers and partial implementations of the .NET Framework Class Library. At the time of this writing, the C# compilers are still in their early stages, but a determined user shouldn't have any trouble using the compilers to develop and test simple C# programs. Also, Microsoft has announced a shared-source implementation of .NET that will include a C# compiler.

Brian Jepson is an O'Reilly editor, programmer, and co-author of Mac OS X Panther for Unix Geeks and Learning Unix for Mac OS X Panther. He's also a volunteer system administrator and all-around geek for AS220, a non-profit arts center in Providence, Rhode Island. AS220 gives Rhode Island artists uncensored and unjuried forums for their work. These forums include galleries, performance space, and publications. Brian sees to it that technology, especially free software, supports that mission. You can follow Brian's blog here.


O'Reilly & Associates recently released (January 2002) C# Essentials, 2nd Edition.

Return to the .NET DevCenter.