| 1 |
Ddoc |
|---|
| 2 |
|
|---|
| 3 |
$(D_S Debugging D on Windows, |
|---|
| 4 |
|
|---|
| 5 |
$(P The Microsoft Windows debugger $(TT $(DMDDIR)\windows\bin\windbg.exe) can be used to |
|---|
| 6 |
symbolically debug D programs, even though it is a C++ debugger. |
|---|
| 7 |
Versions of $(TT windbg.exe) other than the one supplied may not work with D. |
|---|
| 8 |
(Jascha Wetzel's $(LINK2 http://ddbg.mainia.de/releases.html, Ddbg for Windows) |
|---|
| 9 |
is also available.) |
|---|
| 10 |
) |
|---|
| 11 |
|
|---|
| 12 |
$(P To prepare a program for symbolic debugging, compile |
|---|
| 13 |
with the $(B -gc) switch: |
|---|
| 14 |
) |
|---|
| 15 |
|
|---|
| 16 |
$(CONSOLE |
|---|
| 17 |
dmd myprogram -gc |
|---|
| 18 |
) |
|---|
| 19 |
|
|---|
| 20 |
$(P To invoke the debugger on it: |
|---|
| 21 |
) |
|---|
| 22 |
|
|---|
| 23 |
$(CONSOLE |
|---|
| 24 |
windbg myprogram args... |
|---|
| 25 |
) |
|---|
| 26 |
|
|---|
| 27 |
$(P |
|---|
| 28 |
where $(TT args...) are the command line arguments to myprogram.exe. |
|---|
| 29 |
) |
|---|
| 30 |
|
|---|
| 31 |
$(P When the debugger comes up, entering the command in the command window:) |
|---|
| 32 |
|
|---|
| 33 |
$(CONSOLE |
|---|
| 34 |
g _Dmain |
|---|
| 35 |
) |
|---|
| 36 |
|
|---|
| 37 |
$(P will execute the program up until the entry into $(TT main()). |
|---|
| 38 |
From thence, pressing the $(B F10) key will single step each line |
|---|
| 39 |
of code.) |
|---|
| 40 |
|
|---|
| 41 |
$(P Basic Commands:) |
|---|
| 42 |
|
|---|
| 43 |
$(DL |
|---|
| 44 |
|
|---|
| 45 |
$(DT F5) |
|---|
| 46 |
$(DD Go until breakpoint, an exception is thrown, or the end of the program.) |
|---|
| 47 |
|
|---|
| 48 |
$(DT F7) |
|---|
| 49 |
$(DD Continue until cursor.) |
|---|
| 50 |
|
|---|
| 51 |
$(DT F8) |
|---|
| 52 |
$(DD Single step, stepping into function calls.) |
|---|
| 53 |
|
|---|
| 54 |
$(DT F10) |
|---|
| 55 |
$(DD Single step, stepping over function calls.) |
|---|
| 56 |
) |
|---|
| 57 |
|
|---|
| 58 |
$(P For more comprehensive information on $(B windbg), consult the |
|---|
| 59 |
file $(TT $(DMDDIR)\windows\bin\windbg.hlp). |
|---|
| 60 |
) |
|---|
| 61 |
|
|---|
| 62 |
$(COMMENT |
|---|
| 63 |
<h2>Sample Debug Session</h2> |
|---|
| 64 |
|
|---|
| 65 |
$(P This is a walkthrough of a typical debugging session. Given the program:) |
|---|
| 66 |
|
|---|
| 67 |
---------- |
|---|
| 68 |
import std.stdio; |
|---|
| 69 |
|
|---|
| 70 |
class Foo |
|---|
| 71 |
{ |
|---|
| 72 |
int x; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
int main() |
|---|
| 76 |
{ |
|---|
| 77 |
Foo p; |
|---|
| 78 |
bar(p); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
void bar(Foo p) |
|---|
| 82 |
{ |
|---|
| 83 |
abc(p); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
void abc(Foo p) |
|---|
| 87 |
{ |
|---|
| 88 |
p.x++; |
|---|
| 89 |
} |
|---|
| 90 |
--------- |
|---|
| 91 |
|
|---|
| 92 |
$(P It is compiled and run with the following commands:) |
|---|
| 93 |
|
|---|
| 94 |
$(CONSOLE |
|---|
| 95 |
C:\bug>dmd bug -g |
|---|
| 96 |
\dm\bin\link bug,,,user32+kernel32/co/noi; |
|---|
| 97 |
|
|---|
| 98 |
C:\bug>bug |
|---|
| 99 |
Error: Access Violation |
|---|
| 100 |
|
|---|
| 101 |
C:\bug> |
|---|
| 102 |
) |
|---|
| 103 |
|
|---|
| 104 |
$(P It's obviously got a bug, so fire up the debugger with:) |
|---|
| 105 |
|
|---|
| 106 |
$(CONSOLE |
|---|
| 107 |
C:\bug>windbg bug.exe |
|---|
| 108 |
) |
|---|
| 109 |
|
|---|
| 110 |
$(P and the debugger window comes up:) |
|---|
| 111 |
|
|---|
| 112 |
<img src="foo.bmp"> |
|---|
| 113 |
|
|---|
| 114 |
$(P Advance to the beginning of $(TT main()) by entering $(TT g _Dmain):) |
|---|
| 115 |
|
|---|
| 116 |
<img src="windbg2.gif"> |
|---|
| 117 |
|
|---|
| 118 |
$(P now were at the beginning of main(). The upper left black window shows |
|---|
| 119 |
the console output so far, the middle window shows the current location |
|---|
| 120 |
and next instruction (the $(TT xor)), The lower right window shows the |
|---|
| 121 |
current location in the source code, highlighted in yellow.) |
|---|
| 122 |
|
|---|
| 123 |
$(P In order to run until the exception happens, use the $(TT g) command:) |
|---|
| 124 |
|
|---|
| 125 |
<img src="windbg3.gif"> |
|---|
| 126 |
|
|---|
| 127 |
$(P The $(TT First chance exception) says an exception was thrown. The |
|---|
| 128 |
lower right window now shows the line on which the exception happened |
|---|
| 129 |
highlighted in yellow.) |
|---|
| 130 |
|
|---|
| 131 |
$(P Now click on the [Window] menu and Select [Calls]:) |
|---|
| 132 |
|
|---|
| 133 |
<img src="windbg4.gif"> |
|---|
| 134 |
|
|---|
| 135 |
$(P and a window will appear showing the call stack:) |
|---|
| 136 |
|
|---|
| 137 |
<img src="windbg6.gif"> |
|---|
| 138 |
|
|---|
| 139 |
$(P Clicking on the [Disassembly] command brings up |
|---|
| 140 |
the Disassembly window where the instruction that faulted is highlighted |
|---|
| 141 |
in yellow. Clicking on the [Registers] command brings up |
|---|
| 142 |
the register window, where EAX holds the value 00000000.) |
|---|
| 143 |
|
|---|
| 144 |
<img src="windbg7.gif"> |
|---|
| 145 |
|
|---|
| 146 |
$(P The trouble is clearly that $(TT p) is $(TT null). Fix it by allocating |
|---|
| 147 |
an instance for $(TT p):) |
|---|
| 148 |
|
|---|
| 149 |
---------- |
|---|
| 150 |
import std.stdio; |
|---|
| 151 |
|
|---|
| 152 |
class Foo |
|---|
| 153 |
{ |
|---|
| 154 |
int x; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
int main() |
|---|
| 158 |
{ |
|---|
| 159 |
Foo p = new Foo; // the fix |
|---|
| 160 |
bar(p); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
void bar(Foo p) |
|---|
| 164 |
{ |
|---|
| 165 |
abc(p); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
void abc(Foo p) |
|---|
| 169 |
{ |
|---|
| 170 |
p.x++; |
|---|
| 171 |
} |
|---|
| 172 |
--------- |
|---|
| 173 |
|
|---|
| 174 |
$(P and it should now compile and run without error.) |
|---|
| 175 |
) |
|---|
| 176 |
|
|---|
| 177 |
) |
|---|
| 178 |
|
|---|
| 179 |
Macros: |
|---|
| 180 |
TITLE=windbg Debugger |
|---|
| 181 |
WIKI=Windbg |
|---|