• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MSCRM janmson
MS CRM顾问及二次开发
博客园    首页    新随笔    联系   管理    订阅  订阅

Debug

因为某些原因需要在服务器上写个小工具,这样的环境自然不会有什么 VS2005 之类的开发工具了,好在系统已经安装了 .NET Framework。接下来我们开始一次 "朴素" 的 "纯真之旅" 。

1. 源代码

首先用记事本写好源代码,不要告诉我没有智能感知和自动完成,你连代码都不会写了。 [wink] (以下代码仅供演示~~~)

Program.cs
using System;

namespace ConsoleApplication1
{
  public class Program
  {
    static void Test(int i)
    {
      Console.WriteLine(i);
    }

    [STAThread]
    static void Main(string[] args)
    {
      int i = new Random(DateTime.Now.Millisecond).Next(10000);
      Test(i);

      Console.WriteLine("Press any key to exit...");
      Console.ReadKey(true);
      Environment.Exit(0);
    }
  }
}

2. 编译

接下来,我们用 .NET Framework 中的 csc.exe 编译器完成编译。
D:"Temp>C:"WINDOWS"Microsoft.NET"Framework"v2.0.50727"csc.exe /out:test.exe /debug+ program.cs

编译完成后,应该多出 test.exe、test.pdb 这两个文件。

3. 调试

我们还要进行一些调试工具,看看程序逻辑是否正确。由于 SDK 太大懒得去下载,因此 DbgCLR 和 CorDbg 都不能用,好在从网上找了个 MDbg。

1. 启动 MDbg。
D:"Temp>MDbg
MDbg (Managed debugger) v2.0.50727.42 (RTM.050727-4200) started.
Copyright (C) Microsoft Corporation. All rights reserved.

For information about commands type "help";
to exit program type "quit".

mdbg>

2. 载入 test.exe
mdbg> run test.exe
STOP: Breakpoint Hit
14: {
[p#:0, t#:0] mdbg>

3. 设置断点。我们在 18 行和 Test() 方法处设置两个断点。
[p#:0, t#:0] mdbg> b 18
Breakpoint #1 bound (line 18 in Program.cs)
[p#:0, t#:0] mdbg> b ConsoleApplication1.Program.Test
Breakpoint #2 bound (ConsoleApplication1.Program::Test(+0))
[p#:0, t#:0] mdbg> b
Current breakpoints:
Breakpoint #1 bound (line 18 in Program.cs)
Breakpoint #2 bound (ConsoleApplication1.Program::Test(+0))
[p#:0, t#:0] mdbg>

4. 继续调试。我们可以用 sh 指令显示源代码,其中 * 符号表示当前执行位置。
[p#:0, t#:0] mdbg> go
STOP: Breakpoint 2 Hit
8: {
[p#:0, t#:0] mdbg> sh
5 public class Program
6 {
7 static void Test(int i)
8:* {
9 Console.WriteLine(i);
10 }
[p#:0, t#:0] mdbg>

我们可以用 p 指令查看当前变量的值,还以用 set 指令来实现 Edit & Continue。
[p#:0, t#:0] mdbg> p
i=6083
[p#:0, t#:0] mdbg> set i=1234
i=1234
[p#:0, t#:0] mdbg> p
i=1234
[p#:0, t#:0] mdbg>

继续到下一个断点。
[p#:0, t#:0] mdbg> go
1234
STOP: Breakpoint 1 Hit
18: Console.WriteLine("Press any key to exit...");
[p#:0, t#:0] mdbg>

完成调试。
[p#:0, t#:0] mdbg> go
Press any key to exit...
STOP: Process Exited
mdbg>

当然,MDbg 还有很多其他的高级功能。MDbg 和 CorDbg 使用上非常类似,同样包含在 SDK 中。我们可以在 Mike Stall's .NET Debugging Blog 下载其最新版本,安装包中还包含托管类库,我们甚至可以开发自己的调试器。这些调试工具除了进行手工调试外,还常常用来进行反编译和破解。如果你熟悉 WinDbg, 那么一切都不是问题……

4. Release
D:"Temp>C:"WINDOWS"Microsoft.NET"Framework"v2.0.50727"csc.exe /out:test.exe /o program.cs

至此,我们在没有使用任何集成开发环境(IDE)的情况下,完成了开发和调试工作。当然这种方式并不适合日常工作,但了解一下也没有坏处。 [lol]
posted @ 2009-08-13 09:59  韩建兴  阅读(546)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3