1
|
1 /* Ddbg - Win32 Debugger for the D programming language
|
|
2 * Copyright (c) 2007 Jascha Wetzel
|
|
3 * All rights reserved. See LICENSE.TXT for details.
|
|
4 */
|
|
5 module cli.ddbgcli;
|
|
6
|
|
7 import std.string;
|
|
8 static import std.regexp;
|
|
9 import std.c.stdio : sscanf;
|
|
10 import std.c.string;
|
|
11 import std.demangle;
|
|
12
|
|
13 import minidump;
|
|
14 import container;
|
|
15 import util;
|
|
16 import codeview.codeview;
|
|
17 import breakpoint;
|
|
18 import debugger;
|
|
19 import disasm;
|
|
20 import callstack;
|
|
21 import dbgprocess;
|
|
22 import dbgthread;
|
|
23 import expression.expression_apd;
|
|
24 import expression.datahandler : SymbolValue, DataHandler;
|
|
25 import expression.evaluationcontext;
|
|
26 import cli.userinterface;
|
|
27
|
|
28 import win32.winbase;
|
|
29 import win32.winuser;
|
|
30 import win32.winnt;
|
|
31 import win32.dbghelp;
|
|
32
|
|
33 /**************************************************************************************************
|
|
34
|
|
35 **************************************************************************************************/
|
|
36 class DdbgCLI : UserInterfaceBase
|
|
37 {
|
|
38 const int PRINT_SOURCE_LINES = 3;
|
|
39
|
|
40 string[] lastcmd,
|
|
41 onexCommands,
|
|
42 ontermCommands;
|
|
43 string debuggee,
|
|
44 command_line;
|
|
45
|
|
46 uint current_frame_level;
|
|
47
|
|
48 bool quit;
|
|
49
|
|
50 const ubyte CPU_REGISTERS = 1,
|
|
51 FPU_REGISTERS = 2,
|
|
52 MMX_REGISTERS = 4,
|
|
53 SSE_REGISTERS = 8;
|
|
54 ubyte dump_registers = CPU_REGISTERS;
|
|
55
|
|
56 uint lastEvaluationDepth = 1;
|
|
57
|
|
58 bool auto_find_scope_frame, /// find frame of last active scope if current frame has no source/symbols
|
|
59 jump_to_last_known_location_on_exception = true;
|
|
60
|
|
61 /**********************************************************************************************
|
|
62
|
|
63 **********************************************************************************************/
|
|
64 void init(string[] args)
|
|
65 {
|
|
66 DbgIO.println(WELCOME_STRING);
|
|
67 if ( args.length < 2 )
|
|
68 throw new DebuggerException("Usage: ddbg [-cli=<mode>] [-cmd=<commands>] <exe file> [arguments]");
|
|
69
|
|
70 debuggee = args[1];
|
|
71 foreach ( inout arg; args )
|
|
72 {
|
|
73 if ( find(arg, ' ') >= 0 )
|
|
74 arg = "\""~arg~"\"";
|
|
75 }
|
|
76 command_line = join(args[1..$], " ");
|
|
77
|
|
78 dbg = new Debugger(debuggee, this);
|
|
79 }
|
|
80
|
|
81 int start()
|
|
82 {
|
|
83 while ( !quit )
|
|
84 {
|
|
85 debug
|
|
86 readCommand();
|
|
87 else
|
|
88 {
|
|
89 try readCommand();
|
|
90 catch ( Exception e )
|
|
91 DbgIO.println(e.msg);
|
|
92 }
|
|
93 }
|
|
94 return 0;
|
|
95 }
|
|
96
|
|
97 /**********************************************************************************************
|
|
98
|
|
99 **********************************************************************************************/
|
|
100 void singleStep()
|
|
101 {
|
|
102 DbgIO.println(describeLocation(dbg.current_address));
|
|
103 }
|
|
104
|
|
105 /**********************************************************************************************
|
|
106
|
|
107 **********************************************************************************************/
|
|
108 void exitProcess()
|
|
109 {
|
|
110 DbgIO.println("Process terminated");
|
|
111 cmdQueue ~= ontermCommands;
|
|
112 }
|
|
113
|
|
114 /**********************************************************************************************
|
|
115
|
|
116 **********************************************************************************************/
|
|
117 void loadedDLL(DLL dll)
|
|
118 {
|
|
119 if( dll !is null && dll.image.name.length ) {
|
|
120 DbgIO.write(dll.image.name);
|
|
121 DbgIO.println(" loaded at 0x%08x", dll.base);
|
|
122 }
|
|
123 else
|
|
124 DbgIO.writeln("unknown DLL loaded");
|
|
125 }
|
|
126
|
|
127 /**********************************************************************************************
|
|
128
|
|
129 **********************************************************************************************/
|
|
130 void win32exception(uint threadId, EXCEPTION_RECORD* exrec)
|
|
131 {
|
|
132 DbgIO.println(
|
|
133 "Unhandled Exception: %s(0x%x) at %s thread(%d)",
|
|
134 getExceptionName(exrec.ExceptionCode), exrec.ExceptionCode, describeLocation(dbg.current_address), threadId
|
|
135 );
|
|
136 }
|
|
137
|
|
138 /**********************************************************************************************
|
|
139
|
|
140 **********************************************************************************************/
|
|
141 void exception(uint threadId, string class_name, string msg, size_t obj_ptr)
|
|
142 {
|
|
143 if ( jump_to_last_known_location_on_exception )
|
|
144 {
|
|
145 uint index;
|
|
146 current_frame_level = 0;
|
|
147 Location loc = dbg.images.findLocation(dbg.current_address);
|
|
148
|
|
149 if ( loc.codeblock is null )
|
|
150 {
|
|
151 dbg.stack.firstFrame(index);
|
|
152 ubyte[] frame = dbg.stack.prevFrame(index, index);
|
|
153 while ( loc.codeblock is null && frame !is null )
|
|
154 {
|
|
155 uint ret_adr = (cast(uint[])frame)[1];
|
|
156 loc = dbg.images.findLocation(ret_adr);
|
|
157 frame = dbg.stack.prevFrame(index, index);
|
|
158 ++current_frame_level;
|
|
159 }
|
|
160 }
|
|
161 }
|
|
162
|
|
163 DbgIO.println("Unhandled D Exception (%s", class_name);
|
|
164 DbgIO.write(msg.length>0?" \""~msg~"\"":"");
|
|
165 DbgIO.println(") at %s thread(%d)", describeLocation(dbg.current_address), threadId);
|
|
166 cmdQueue ~= onexCommands;
|
|
167 }
|
|
168
|
|
169 /**********************************************************************************************
|
|
170
|
|
171 **********************************************************************************************/
|
|
172 void userInterrupt()
|
|
173 {
|
|
174 DbgIO.println("User interrupt at %s", describeLocation(dbg.current_address));
|
|
175 }
|
|
176
|
|
177 /**********************************************************************************************
|
|
178
|
|
179 **********************************************************************************************/
|
|
180 bool breakpoint(int index, Breakpoint bp, DbgThread thread)
|
|
181 {
|
|
182 if ( bp.hardware )
|
|
183 {
|
|
184 DbgIO.println("Hardware breakpoint for 0x%x hit at %s thread(%d)",
|
|
185 bp.address, describeLocation(dbg.current_address), thread.id
|
|
186 );
|
|
187 }
|
|
188 else if ( bp.file is null || bp.line == 0 )
|
|
189 DbgIO.println("Unknown breakpoint hit at %s thread(%d)", describeLocation(bp.address), thread.id);
|
|
190 else
|
|
191 {
|
|
192 string[] source = dbg.getSourceFile(bp.file);
|
4
|
193 string source_lines;
|
1
|
194 if ( source !is null && bp.line <= source.length )
|
4
|
195 {
|
|
196 const showmore= 2;
|
|
197 int startfrom = bp.line- 1 - showmore;
|
|
198 int startend = bp.line- 1 + showmore;
|
|
199 if (startfrom <0)
|
|
200 startfrom=0;
|
|
201 if (startend >=source.length)
|
|
202 startend=source.length-1;
|
|
203 int left = showmore;
|
|
204 foreach (line;source[startfrom..startend+1])
|
|
205 {
|
|
206 if (left==0)
|
|
207 source_lines ~= ">>"~line ~"\n";
|
|
208 else
|
|
209 source_lines ~= " "~line ~"\n";
|
|
210 left--;
|
|
211 }
|
|
212 }
|
1
|
213
|
|
214 if ( !bp.temporary )
|
|
215 DbgIO.print("Breakpoint %d hit at ", index);
|
4
|
216 debug DbgIO.println("before printing location info");
|
1
|
217 DbgIO.println("%s:%d 0x%x thread(%d)", bp.file, bp.line, bp.address, thread.id);
|
4
|
218 if ( source_lines.length > 0 )
|
|
219 DbgIO.write(source_lines);
|
1
|
220 }
|
|
221 return true;
|
|
222 }
|
|
223
|
|
224 /**********************************************************************************************
|
|
225
|
|
226 **********************************************************************************************/
|
|
227 void debugString(string str)
|
|
228 {
|
|
229 printf("OUTPUT DEBUG STRING:\n%s\n", toStringz(str));
|
|
230 }
|
|
231
|
|
232 /**********************************************************************************************
|
|
233 Command line parser. Gets called when debuggee is suspended.
|
|
234 **********************************************************************************************/
|
|
235 bool parseCommand(string input)
|
|
236 {
|
|
237 if ( strip(input).length > 0 )
|
|
238 {
|
|
239 auto r = std.regexp.RegExp("(([^\" \\t]+)|(\"[^\"]+\"))+");
|
|
240 lastcmd.length = 0;
|
|
241 foreach ( m; r.search(input) )
|
4
|
242 {
|
|
243 //DbgIO.writeln("match:"~r.match(0));
|
1
|
244 lastcmd ~= r.match(0);
|
4
|
245 }
|
|
246
|
1
|
247 }
|
|
248 if ( lastcmd.length <= 0 )
|
|
249 return false;
|
4
|
250 if (lastcmd[0][0]=='=' && lastcmd[0].length>1)
|
|
251 {
|
|
252 DbgIO.writeln("after ="~lastcmd[0][1..$]);
|
|
253 lastcmd=["=",lastcmd[0][1..$]]~lastcmd[1..$];
|
|
254 }
|
1
|
255 switch ( lastcmd[0] )
|
|
256 {
|
|
257 case "help":
|
|
258 case "h":
|
|
259 case "?":
|
|
260 DbgIO.write(import("ddbg_help.txt"));
|
|
261 break;
|
|
262 case "=":
|
|
263 if ( !dbg.process_loaded && dbg.miniDump is null )
|
|
264 break;
|
|
265 if ( lastcmd.length > 1 )
|
|
266 {
|
|
267 try DbgIO.writeln(symbolValueToString(dbg.handleData(dbg.evaluateExpression(lastcmd[1], current_frame_level), false)));
|
|
268 catch ( EvaluationException e ) {
|
|
269 DbgIO.writeln(e.toString);
|
|
270 }
|
|
271 }
|
|
272 break;
|
|
273 case "arg":
|
|
274 if ( dbg.process_loaded )
|
|
275 DbgIO.println("Warning: process already started");
|
|
276 command_line = debuggee~" "~join(lastcmd[1..$], " ");
|
|
277 break;
|
|
278 // add breakpoint
|
|
279 case "bp":
|
|
280 int index;
|
|
281 if ( lastcmd.length < 2 ) {
|
|
282 DbgIO.println("invalid syntax - see help for details");
|
|
283 break;
|
|
284 }
|
|
285
|
|
286 int pos = find(lastcmd[1], '#');
|
|
287 uint threadId;
|
|
288 if ( pos > 0 )
|
|
289 {
|
|
290 threadId = cast(uint)atoi(lastcmd[1][pos+1..$]);
|
|
291 lastcmd[1] = lastcmd[1][0..pos];
|
|
292 }
|
|
293 Location loc = new Location(lastcmd[1]);
|
|
294 loc.bind(dbg.images, dbg.source_search_paths);
|
|
295 if ( lastcmd.length > 2 )
|
|
296 index = cast(int)atoi(lastcmd[2]);
|
|
297
|
|
298 Breakpoint bp;
|
|
299 if ( index <= 0 && dbg.breakpoints.length > 0 )
|
|
300 index = dbg.breakpoints.keys.dup.sort[$-1]+1;
|
|
301 bp = dbg.setBreakpoint(loc, index, threadId);
|
|
302 DbgIO.println("Breakpoint set: %s", bp.toString);
|
|
303 break;
|
|
304 // delete breakpoint
|
|
305 case "dbp":
|
|
306 if ( lastcmd.length > 1 )
|
|
307 {
|
|
308 if ( lastcmd[1] == "*" )
|
|
309 {
|
|
310 foreach ( uint i; dbg.breakpoints.keys )
|
|
311 dbg.removeBreakpoint(i);
|
|
312 }
|
|
313 else if ( isNumeric(lastcmd[1]) )
|
|
314 {
|
|
315 if ( !dbg.removeBreakpoint(cast(uint)atoi(lastcmd[1])) )
|
|
316 DbgIO.println("invalid breakpoint index: %s",lastcmd[1]);
|
|
317 }
|
|
318 else
|
|
319 {
|
|
320 Location loc = new Location(lastcmd[1]);
|
|
321 loc.bind(dbg.images, dbg.source_search_paths);
|
|
322 if ( loc !is null )
|
|
323 {
|
|
324 int bp_index;
|
|
325 dbg.getBreakpoint(loc, bp_index);
|
|
326 if ( bp_index >= 0 && !dbg.removeBreakpoint(bp_index) )
|
|
327 DbgIO.println("No breakpoint set at "~lastcmd[1]);
|
|
328 }
|
|
329 else
|
|
330 DbgIO.println("dbp [<source file>:<line>|#index|*]");
|
|
331 }
|
|
332 }
|
|
333 else
|
|
334 DbgIO.println("dbp [<source file>:<line>|#index|*]");
|
|
335 break;
|
|
336 // set evaluation depth
|
|
337 case "er":
|
|
338 lastcmd.length = 1;
|
|
339 if ( dbg.evaluationDepth > 1 ) {
|
|
340 lastEvaluationDepth = dbg.evaluationDepth;
|
|
341 lastcmd ~= "1";
|
|
342 }
|
|
343 else
|
|
344 lastcmd ~= .toString(lastEvaluationDepth);
|
|
345 case "ed":
|
|
346 if ( lastcmd.length < 2 || !isNumeric(lastcmd[1]) )
|
|
347 DbgIO.println("Usage: ed <depth>");
|
|
348 else {
|
|
349 dbg.evaluationDepth = cast(int)atoi(lastcmd[1]);
|
|
350 DbgIO.println("Expression evaluation depth is %d", dbg.evaluationDepth);
|
|
351 }
|
|
352 break;
|
|
353 case "el":
|
|
354 if ( lastcmd.length < 2 || !isNumeric(lastcmd[1]) )
|
|
355 DbgIO.println("Usage: el <length>");
|
|
356 else {
|
|
357 DataHandler.max_elem_count = cast(int)atoi(lastcmd[1]);
|
|
358 DbgIO.println("Array evaluation length is %d", DataHandler.max_elem_count);
|
|
359 }
|
|
360 break;
|
|
361 // select frame
|
|
362 case "f":
|
|
363 if ( lastcmd.length > 1 )
|
|
364 current_frame_level = cast(uint)atoi(lastcmd[1]);
|
|
365 DbgIO.println("Current frame level is %d", current_frame_level);
|
|
366 break;
|
|
367 // image base address
|
|
368 case "ii":
|
|
369 foreach ( img; dbg.images.images )
|
|
370 {
|
|
371 uint ibase = img.imageBase;
|
|
372 DbgIO.println("ImageBase\t0x%x", ibase);
|
|
373 DbgIO.println("CodeBase\t0x%x", img.getCodeBase);
|
|
374 DbgIO.println("Sections:\nname\t\taddress\t\tsize\t\tcharacteristics");
|
|
375 foreach ( s; img.sections )
|
|
376 with ( *s.header ) DbgIO.println("%s\t0x%08x\t0x%08x\t0x%08x", cast(char[8])Name, ibase+VirtualAddress, Misc.VirtualSize, Characteristics);
|
|
377 }
|
|
378 break;
|
|
379 // jump to last known source frame
|
|
380 case "jkf":
|
|
381 jump_to_last_known_location_on_exception = !jump_to_last_known_location_on_exception;
|
|
382 DbgIO.println("%s to from of last known source location on exception",
|
|
383 jump_to_last_known_location_on_exception?"Jumping":"Not jumping"
|
|
384 );
|
|
385 break;
|
|
386 // walk memory
|
|
387 case "mi":
|
|
388 if ( !dbg.process_loaded )
|
|
389 break;
|
|
390 uint filteredStates = MEM_COMMIT;
|
|
391 foreach ( lc; lastcmd[1..$] )
|
|
392 {
|
|
393 switch ( lc )
|
|
394 {
|
|
395 case "free": filteredStates |= MEM_FREE; break;
|
|
396 case "rsrv": filteredStates |= MEM_RESERVE; break;
|
|
397 default:
|
|
398 }
|
|
399 }
|
|
400
|
|
401 MEMORY_BASIC_INFORMATION[] mbis = dbg.process.walkMemory;
|
|
402
|
|
403 DbgIO.println("Base AlcBase AlcProt RgnSize Stat Protect Type");
|
|
404 foreach ( mbi; mbis )
|
|
405 {
|
|
406 if ( (mbi.State & filteredStates) == 0 )
|
|
407 continue;
|
|
408
|
|
409 string state;
|
|
410 switch ( mbi.State )
|
|
411 {
|
|
412 case MEM_COMMIT: state = "comt"; break;
|
|
413 case MEM_FREE: state = "free"; break;
|
|
414 case MEM_RESERVE: state = "rsrv"; break;
|
|
415 default: state = "unkn"; break;
|
|
416 }
|
|
417
|
|
418 string type;
|
|
419 switch ( mbi.Type )
|
|
420 {
|
|
421 case MEM_IMAGE: type = "imag"; break;
|
|
422 case MEM_MAPPED: type = "mapd"; break;
|
|
423 case MEM_PRIVATE: type = "priv"; break;
|
|
424 default: type = "unkn"; break;
|
|
425 }
|
|
426
|
|
427 string protectName(uint prot)
|
|
428 {
|
|
429 string protStr;
|
|
430 switch ( prot & 0xff )
|
|
431 {
|
|
432 case 0: protStr = "?"; break;
|
|
433 case PAGE_EXECUTE: protStr = "x"; break;
|
|
434 case PAGE_EXECUTE_READ: protStr = "xr"; break;
|
|
435 case PAGE_EXECUTE_READWRITE: protStr = "xrw"; break;
|
|
436 case PAGE_EXECUTE_WRITECOPY: protStr = "xwc"; break;
|
|
437 case PAGE_NOACCESS: protStr = "na"; break;
|
|
438 case PAGE_READONLY: protStr = "ro"; break;
|
|
439 case PAGE_READWRITE: protStr = "rw"; break;
|
|
440 case PAGE_WRITECOPY: protStr = "wc"; break;
|
|
441 default: protStr = format("%02x", prot & 0xff); break;
|
|
442 }
|
|
443 protStr ~= " ";
|
|
444 if ( prot & PAGE_GUARD )
|
|
445 protStr ~= "g";
|
|
446 if ( prot & PAGE_NOCACHE )
|
|
447 protStr ~= "nc";
|
|
448 return protStr;
|
|
449 }
|
|
450
|
|
451 DbgIO.println("%08x %08x %- 7s %08x %s %- 7s %s",
|
|
452 cast(size_t)mbi.BaseAddress, cast(size_t)mbi.AllocationBase, protectName(mbi.AllocationProtect),
|
|
453 mbi.RegionSize, state, protectName(mbi.Protect), type
|
|
454 );
|
|
455 }
|
|
456 break;
|
|
457 // list breakpoints
|
|
458 case "lbp":
|
|
459 if ( dbg.breakpoints.length <= 0 )
|
|
460 DbgIO.println("no breakpoints set");
|
|
461 foreach ( uint i, bp; dbg.breakpoints )
|
|
462 DbgIO.println("%d %s", i, bp.toString);
|
|
463 break;
|
|
464 // ldll
|
|
465 case "ldll":
|
|
466 if ( !dbg.process_loaded )
|
|
467 break;
|
|
468 DbgIO.println("Base Name");
|
|
469 foreach ( dll; dbg.process.loaded_dlls )
|
|
470 DbgIO.println("%08x %s", dll.base, dll.image.name);
|
|
471 break;
|
|
472 // list temporary breakpoints
|
|
473 case "ltbp":
|
|
474 if ( dbg.temp_breakpoints.empty )
|
|
475 DbgIO.println("no temporary breakpoints set");
|
|
476 foreach ( bp; dbg.temp_breakpoints )
|
|
477 DbgIO.writeln(bp.value.toString);
|
|
478 break;
|
|
479 // list debug modules
|
|
480 case "lm":
|
|
481 Module[] modules_noinfo;
|
|
482 foreach ( img; dbg.images.images )
|
|
483 {
|
|
484 foreach ( m; img.codeView.modulesByIndex )
|
|
485 {
|
|
486 string name = m.name;
|
|
487
|
|
488 if ( m.header.iLib > 0 )
|
|
489 name ~= " from "~img.codeView.libraries[m.header.iLib];
|
|
490
|
|
491 if ( lastcmd.length > 1 && find(name, lastcmd[1]) < 0 )
|
|
492 continue;
|
|
493
|
|
494 bool has_info = false;
|
|
495 with ( m.symbols )
|
|
496 {
|
|
497 if ( proc_symbols.length+stack_symbols.length+data_symbols.length > 0 )
|
|
498 {
|
|
499 DbgIO.println(
|
|
500 "%s\n\tSymbols: %d proc %d stack %d data",
|
|
501 name, proc_symbols.length, stack_symbols.length, data_symbols.length
|
|
502 );
|
|
503 has_info = true;
|
|
504 }
|
|
505 }
|
|
506 if ( m.source_module !is null )
|
|
507 {
|
|
508 DbgIO.println("\tSource files:");
|
|
509 has_info = true;
|
|
510 foreach ( sf; m.source_module.files )
|
|
511 DbgIO.println("\t\t%s", sf.name);
|
|
512 }
|
|
513 if ( !has_info )
|
|
514 modules_noinfo ~= m;
|
|
515 }
|
|
516 }
|
|
517 if ( modules_noinfo.length > 0 )
|
|
518 {
|
|
519 DbgIO.println("Modules without debug information:");
|
|
520 foreach ( img; dbg.images.images )
|
|
521 {
|
|
522 foreach ( m; modules_noinfo )
|
|
523 {
|
|
524 string name = m.name;
|
|
525
|
|
526 if ( m.header.iLib > 0 )
|
|
527 name ~= " from "~img.codeView.libraries[m.header.iLib];
|
|
528
|
|
529 DbgIO.println("%s #segs=%d", name, m.seginfos.length);
|
|
530 if ( m.source_module !is null )
|
|
531 {
|
|
532 DbgIO.println("\tSource files:");
|
|
533 foreach ( sf; m.source_module.files )
|
|
534 DbgIO.println("\t\t%s", sf.name);
|
|
535 }
|
|
536 }
|
|
537 }
|
|
538 }
|
|
539 break;
|
|
540 // list source modules
|
|
541 case "lsm":
|
|
542 uint[string] line_counters;
|
|
543 uint[string] seg_counters;
|
|
544 foreach ( img; dbg.images.images )
|
|
545 {
|
|
546 if ( img.codeView is null )
|
|
547 continue;
|
|
548 foreach ( m; img.codeView.modulesByIndex )
|
|
549 {
|
|
550 if ( m.source_module is null )
|
|
551 continue;
|
|
552 foreach ( sf; m.source_module.files )
|
|
553 {
|
|
554 if ( lastcmd.length > 1 && find(sf.name, lastcmd[1]) < 0 )
|
|
555 continue;
|
|
556 uint lines = sf.lines.length;
|
|
557 if ( (sf.name in line_counters) is null ) {
|
|
558 seg_counters[sf.name] = sf.segments.length;
|
|
559 line_counters[sf.name] = lines;
|
|
560 }
|
|
561 else {
|
|
562 seg_counters[sf.name] += sf.segments.length;
|
|
563 line_counters[sf.name] += lines;
|
|
564 }
|
|
565 }
|
|
566 }
|
|
567 }
|
|
568 string[] lc_keys = line_counters.keys.dup;
|
|
569 lc_keys.sort;
|
|
570 foreach ( key; lc_keys )
|
|
571 DbgIO.println("%s\t\tsegs=%d lines=%d", key, seg_counters[key], line_counters[key]);
|
|
572 break;
|
|
573 // list source lines per module
|
|
574 case "lsl":
|
|
575 foreach ( img; dbg.images.images )
|
|
576 {
|
|
577 if ( img.codeView is null )
|
|
578 continue;
|
|
579 foreach ( m; img.codeView.modulesByIndex )
|
|
580 {
|
|
581 if ( m.source_module is null )
|
|
582 continue;
|
|
583 DbgIO.println("module %s", m.name);
|
|
584 foreach ( sf; m.source_module.files )
|
|
585 {
|
|
586 DbgIO.println("file %s", sf.name);
|
|
587 if ( lastcmd.length > 1 && find(sf.name, lastcmd[1]) < 0 )
|
|
588 continue;
|
|
589 foreach ( l; sf.lines.reverse )
|
|
590 {
|
|
591 DbgIO.println("line %d", l);
|
|
592 }
|
|
593 }
|
|
594 }
|
|
595 }
|
|
596 break;
|
|
597 // list all symbols
|
|
598 case "ls":
|
|
599 /*
|
|
600 void dumpSymbolAVL(AVLNode!(NamedSymbol) node, uint indent=0)
|
|
601 {
|
|
602 string indentstr = new char[indent*2];
|
|
603 indentstr[0..indent*2] = ' ';
|
|
604 DbgIO.println("%s%s", indentstr, node.value.name_notype);
|
|
605 if ( node.left !is null )
|
|
606 dumpSymbolAVL(node.left, indent+1);
|
|
607 if ( node.right !is null )
|
|
608 dumpSymbolAVL(node.right, indent+1);
|
|
609 }
|
|
610 DbgIO.println("AVL dump:");
|
|
611 dumpSymbolAVL(dbg.image.codeView.globalNamedSymbols.root);
|
|
612 */
|
|
613 foreach ( img; dbg.images.images )
|
|
614 {
|
|
615 if ( img.codeView is null )
|
|
616 continue;
|
|
617 printSymbols(img.codeView, img.codeView.global_pub.named_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
618 printSymbols(img.codeView, img.codeView.global_sym.named_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
619 printSymbols(img.codeView, img.codeView.static_sym.named_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
620 foreach ( m; img.codeView.modulesByIndex )
|
|
621 printSymbols(img.codeView, m.symbols.named_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
622 }
|
|
623 break;
|
|
624 // list source search paths
|
|
625 case "lsp":
|
|
626 foreach ( s; dbg.source_search_paths )
|
|
627 DbgIO.writeln(s);
|
|
628 break;
|
|
629 // list function symbols
|
|
630 case "lf":
|
|
631 foreach ( img; dbg.images.images )
|
|
632 {
|
|
633 if ( img.codeView is null )
|
|
634 continue;
|
|
635 printSymbols(img.codeView, img.codeView.global_pub.proc_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
636 printSymbols(img.codeView, img.codeView.global_sym.proc_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
637 printSymbols(img.codeView, img.codeView.static_sym.proc_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
638 foreach ( m; img.codeView.modulesByIndex )
|
|
639 printSymbols(img.codeView, m.symbols.proc_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
640 }
|
|
641 break;
|
|
642 // list data symbols
|
|
643 case "ld":
|
|
644 foreach ( img; dbg.images.images )
|
|
645 {
|
|
646 if ( img.codeView is null )
|
|
647 continue;
|
|
648 printSymbols(img.codeView, img.codeView.global_pub.data_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
649 printSymbols(img.codeView, img.codeView.global_sym.data_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
650 printSymbols(img.codeView, img.codeView.static_sym.data_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
651 foreach ( m; img.codeView.modulesByIndex )
|
|
652 printSymbols(img.codeView, m.symbols.data_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
653 }
|
|
654 break;
|
|
655 // list global symbols
|
|
656 case "lg":
|
|
657 foreach ( img; dbg.images.images )
|
|
658 {
|
|
659 if ( img.codeView is null )
|
|
660 continue;
|
|
661 printSymbols(img.codeView, img.codeView.global_pub.named_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
662 printSymbols(img.codeView, img.codeView.global_sym.named_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
663 }
|
|
664 break;
|
|
665 // list global publics
|
|
666 case "lp":
|
|
667 foreach ( img; dbg.images.images )
|
|
668 {
|
|
669 if ( img.codeView is null )
|
|
670 continue;
|
|
671 printSymbols(img.codeView, img.codeView.global_pub.named_symbols, lastcmd.length>1?lastcmd[1]:null);
|
|
672 }
|
|
673 break;
|
|
674 // list scope variables
|
|
675 case "lsv":
|
|
676 if ( dbg.process_loaded || dbg.miniDump !is null )
|
|
677 evalScopeSymbols();
|
|
678 break;
|
|
679 // list threads
|
|
680 case "lt":
|
|
681 if ( dbg.miniDump !is null )
|
|
682 {
|
|
683 DbgIO.println(" id pri sus location");
|
|
684 MINIDUMP_THREAD[] threads = dbg.miniDump.threads;
|
|
685 foreach ( thread; threads )
|
|
686 {
|
|
687 CONTEXT* ctx;
|
|
688 ctx = cast(CONTEXT*)dbg.miniDump.rvaToVa(thread.ThreadContext.Rva);
|
|
689 Location loc = dbg.images.findLocation(ctx.Eip);
|
|
690
|
|
691 DbgIO.println("%s%s%- 6d %- 3d %- 2d %s",
|
|
692 thread.ThreadId==dbg.thread_id?">":" ", thread.ThreadId==dbg.miniDump.threadInfo.mainThreadId?"*":" ",
|
|
693 thread.ThreadId, thread.Priority, thread.SuspendCount, describeLocation(loc)
|
|
694 );
|
|
695 }
|
|
696 }
|
|
697 else
|
|
698 {
|
|
699 DbgIO.println(" id pri sus creation exit kernel user location");
|
|
700 foreach ( t; dbg.process.threads.values )
|
|
701 {
|
|
702 // id name location pri pri-boost start-info times
|
|
703 ulong creation,
|
|
704 exit,
|
|
705 kernel,
|
|
706 user;
|
|
707
|
|
708 t.times(creation, exit, kernel, user);
|
|
709
|
|
710 CONTEXT ctx;
|
|
711 t.getContext(ctx);
|
|
712 Location loc = dbg.images.findLocation(ctx.Eip);
|
|
713
|
|
714 DbgIO.println("%s%s%- 6d %- 3d%s %- 2d %s %s %s %s %s",
|
|
715 t.id==dbg.thread_id?">":" ", t.id==dbg.process.mainThreadId?"*":" ", t.id,
|
|
716 t.priority, t.priorityBoost?"b":" ", t.suspendCount,
|
|
717 formatTicks(creation), formatTicks(exit), formatTicks(kernel), formatTicks(user),
|
|
718 describeLocation(loc)
|
|
719 );
|
|
720 }
|
|
721 }
|
|
722 break;
|
|
723 // no console
|
|
724 case "nc":
|
|
725 dbg.create_new_console = !dbg.create_new_console;
|
|
726 DbgIO.println("Starting debuggee in %s console", dbg.create_new_console?"new":"this");
|
|
727 break;
|
|
728 // on exception
|
|
729 case "onex":
|
|
730 if ( lastcmd.length < 2 )
|
|
731 {
|
|
732 foreach ( cmd; onexCommands )
|
|
733 DbgIO.writeln(cmd[0..$-1]);
|
|
734 }
|
|
735 else
|
|
736 {
|
|
737 onexCommands = null;
|
|
738 string cmdList;
|
|
739 foreach ( cmd; lastcmd[1..$] )
|
|
740 {
|
|
741 if ( cmd[0] == '"' && cmd[$-1] == '"' )
|
|
742 cmdList ~= " "~cmd[1..$-1];
|
|
743 else
|
|
744 cmdList ~= " "~cmd;
|
|
745 }
|
|
746 foreach ( cmd; split(cmdList, ";") )
|
|
747 onexCommands ~= strip(cmd)~";";
|
|
748 }
|
|
749 break;
|
|
750 // on termination
|
|
751 case "onterm":
|
|
752 if ( lastcmd.length < 2 )
|
|
753 {
|
|
754 foreach ( cmd; ontermCommands )
|
|
755 DbgIO.writeln(cmd[0..$-1]);
|
|
756 }
|
|
757 else
|
|
758 {
|
|
759 ontermCommands = null;
|
|
760 string cmdList;
|
|
761 foreach ( cmd; lastcmd[1..$] )
|
|
762 {
|
|
763 if ( cmd[0] == '"' && cmd[$-1] == '"' )
|
|
764 cmdList ~= " "~cmd[1..$-1];
|
|
765 else
|
|
766 cmdList ~= " "~cmd;
|
|
767 }
|
|
768 foreach ( cmd; split(cmdList, ";") )
|
|
769 ontermCommands ~= strip(cmd)~";";
|
|
770 }
|
|
771 break;
|
|
772 // print source
|
|
773 case "ps":
|
|
774 Location loc;
|
|
775 string[] source;
|
|
776
|
|
777 do
|
|
778 {
|
|
779 if ( current_frame_level > 0 )
|
|
780 {
|
|
781 auto frame = dbg.stack.getFrame(current_frame_level);
|
|
782 if ( frame is null )
|
|
783 goto Lnotfound;
|
|
784 uint ret_adr = (cast(uint[])frame)[1];
|
|
785 loc = dbg.images.findLocation(ret_adr);
|
|
786 }
|
|
787 else
|
|
788 loc = dbg.images.findLocation(dbg.current_address);
|
|
789 debug DbgIO.println("found location %s", loc.file);
|
|
790
|
|
791 source = dbg.getSourceFile(loc.file);
|
|
792 if ( source !is null )
|
|
793 break;
|
|
794 if ( auto_find_scope_frame ) {
|
|
795 ++current_frame_level;
|
|
796 continue;
|
|
797 }
|
|
798 Lnotfound:
|
|
799 DbgIO.writeln("Source file for current location unknown");
|
|
800 break;
|
|
801 }
|
|
802 while( source is null );
|
|
803
|
|
804 int start_line, end_line;
|
|
805 if ( lastcmd.length > 1 ) {
|
|
806 start_line = cast(uint)atoi(lastcmd[1]);
|
|
807 end_line = loc.line + start_line;
|
|
808 start_line = loc.line - start_line;
|
|
809 }
|
|
810 else if ( loc.scope_sym !is null )
|
|
811 {
|
|
812 ProcedureSymbol psym = cast(ProcedureSymbol)loc.scope_sym;
|
|
813 auto start_address = loc.getCodeBase + psym.cvdata.offset;
|
|
814 auto start_loc = dbg.images.findLocation(start_address);
|
|
815 auto end_loc = dbg.images.findLocation(start_address+psym.cvdata.proc_length-1);
|
|
816 debug DbgIO.println("address range: 0x%x 0x%x", start_address, start_address+psym.cvdata.proc_length-1);
|
|
817 debug DbgIO.println("lines before prev: %d - %d", start_loc.line, end_loc.line);
|
|
818 end_loc = dbg.images.findPrevSrcLine(end_loc);
|
|
819 start_line = start_loc.line;
|
|
820 if ( end_loc !is null )
|
|
821 end_line = end_loc.line;
|
|
822 }
|
|
823 if ( end_line == 0 ) {
|
|
824 start_line = loc.line - PRINT_SOURCE_LINES;
|
|
825 end_line = loc.line + PRINT_SOURCE_LINES;
|
|
826 }
|
|
827
|
|
828 debug DbgIO.println("lines: %d - %d", start_line, end_line);
|
|
829 for ( int l = dmax(0, start_line-1); l < dmin(cast(int)source.length, end_line); ++l )
|
|
830 {
|
|
831 if ( l+1 == loc.line )
|
|
832 DbgIO.write(">");
|
|
833 DbgIO.writeln(source[l]);
|
|
834 }
|
|
835 break;
|
|
836 // quit
|
|
837 case "q":
|
|
838 dbg.abort = true;
|
|
839 quit = true;
|
|
840 return true;
|
|
841 // run/continue
|
|
842 case "r":
|
|
843 if ( dbg.miniDump !is null ) {
|
|
844 DbgIO.println("Command not valid in post-mortem mode");
|
|
845 break;
|
|
846 }
|
|
847 if ( dbg.process_loaded ) {
|
|
848 dbg.resume;
|
|
849 return true;
|
|
850 }
|
|
851 else {
|
4
|
852 dbg.breakonmain = false;
|
|
853 dbg.start(command_line);
|
1
|
854 return true;
|
|
855 }
|
|
856 // single-step
|
|
857 case "s":
|
|
858 if ( dbg.miniDump !is null ) {
|
|
859 DbgIO.println("Command not valid in post-mortem mode");
|
|
860 break;
|
|
861 }
|
|
862 if ( dbg.process_loaded ) {
|
|
863 dbg.single_step = true;
|
|
864 dbg.activateSingleStep(true);
|
|
865 }
|
|
866 return true;
|
|
867 // add source search path
|
|
868 case "sp":
|
|
869 if ( lastcmd.length > 1 )
|
|
870 {
|
|
871 string sp = lastcmd[1].dup;
|
|
872 if ( sp[$-1] != '\\' )
|
|
873 sp ~= '\\';
|
|
874 dbg.source_search_paths ~= sp;
|
|
875 }
|
|
876 else
|
|
877 DbgIO.println("usage: sp <search_path>");
|
|
878 break;
|
|
879 // switch thread
|
|
880 case "st":
|
|
881 if ( lastcmd.length < 2 ) {
|
|
882 DbgIO.writeln("Usage: st <threadID>");
|
|
883 break;
|
|
884 }
|
|
885 size_t threadId = cast(size_t)atoi(lastcmd[1]);
|
|
886 if ( dbg.miniDump !is null )
|
|
887 {
|
|
888 foreach ( thread; dbg.miniDump.threads )
|
|
889 {
|
|
890 if ( threadId == thread.ThreadId ) {
|
|
891 dbg.selectThread(threadId);
|
|
892 break;
|
|
893 }
|
|
894 }
|
|
895 }
|
|
896 else
|
|
897 {
|
|
898 foreach ( t; dbg.process.threads.values )
|
|
899 {
|
|
900 if ( threadId == t.id ) {
|
|
901 dbg.selectThread(threadId);
|
|
902 break;
|
|
903 }
|
|
904 }
|
|
905 }
|
|
906 break;
|
|
907 // step over
|
|
908 case "ov":
|
|
909 if ( dbg.miniDump !is null ) {
|
|
910 DbgIO.println("Command not valid in post-mortem mode");
|
|
911 break;
|
|
912 }
|
|
913 if ( dbg.process_loaded && dbg.step(StepMode.e_over) )
|
|
914 {
|
|
915 debug foreach ( bp; dbg.temp_breakpoints )
|
|
916 DbgIO.writeln(bp.value.toString);
|
|
917 return true;
|
|
918 }
|
4
|
919 if (!dbg.process_loaded)
|
|
920 {
|
|
921 dbg.breakonmain = true;
|
|
922 dbg.start(command_line);
|
|
923 return true;
|
|
924 }
|
1
|
925 break;
|
|
926 // step into
|
|
927 case "in":
|
|
928 if ( dbg.miniDump !is null ) {
|
|
929 DbgIO.println("Command not valid in post-mortem mode");
|
|
930 break;
|
|
931 }
|
|
932 if ( dbg.process_loaded && dbg.step(StepMode.e_in) )
|
|
933 {
|
|
934 debug foreach ( bp; dbg.temp_breakpoints )
|
|
935 DbgIO.writeln(bp.value.toString);
|
|
936 return true;
|
|
937 }
|
|
938 break;
|
|
939 // step out
|
|
940 case "out":
|
|
941 if ( dbg.miniDump !is null ) {
|
|
942 DbgIO.println("Command not valid in post-mortem mode");
|
|
943 break;
|
|
944 }
|
|
945 if ( dbg.process_loaded && dbg.step(StepMode.e_out) )
|
|
946 {
|
|
947 debug foreach ( bp; dbg.temp_breakpoints )
|
|
948 DbgIO.writeln(bp.value.toString);
|
|
949 return true;
|
|
950 }
|
|
951 break;
|
|
952 // disassemble
|
|
953 case "da":
|
|
954 if ( !dbg.process_loaded )
|
|
955 break;
|
|
956
|
|
957 uint start_address;
|
|
958 if ( current_frame_level > 0 ) {
|
|
959 auto frame = dbg.stack.getFrame(current_frame_level);
|
|
960 start_address = (cast(uint[])frame)[1];
|
|
961 }
|
|
962 else
|
|
963 start_address = dbg.current_address;
|
|
964
|
|
965 if ( lastcmd.length > 1 )
|
|
966 sscanf(toStringz(lastcmd[1]), "%x", &start_address);
|
|
967 else
|
|
968 {
|
|
969 Location loc = dbg.images.findLocation(dbg.current_address);
|
|
970 if ( loc.scope_sym !is null ) {
|
|
971 ProcedureSymbol psym = cast(ProcedureSymbol)loc.scope_sym;
|
|
972 start_address = loc.getCodeBase + psym.cvdata.offset;
|
|
973 DisAsm.disasm(dbg.process, start_address, start_address+psym.cvdata.proc_length, &printDisassembly);
|
|
974 break;
|
|
975 }
|
|
976 }
|
|
977 DisAsm.disasm(dbg.process, start_address, 0, &printDisassembly);
|
|
978 break;
|
|
979 // disassemble line
|
|
980 case "dal":
|
|
981 if ( !dbg.process_loaded )
|
|
982 break;
|
|
983
|
|
984 int num_lines = 1;
|
|
985 if ( lastcmd.length > 1 )
|
|
986 {
|
|
987 sscanf(toStringz(lastcmd[1]), "%d", &num_lines);
|
|
988 if ( num_lines < 0 )
|
|
989 --num_lines;
|
|
990 }
|
|
991
|
|
992 uint start_address;
|
|
993 if ( current_frame_level > 0 ) {
|
|
994 auto frame = dbg.stack.getFrame(current_frame_level);
|
|
995 start_address = (cast(uint[])frame)[1];
|
|
996 }
|
|
997 else
|
|
998 start_address = dbg.current_address;
|
|
999
|
|
1000 for ( ; num_lines != 0; num_lines<0?++num_lines:--num_lines )
|
|
1001 {
|
|
1002 Location loc = dbg.images.findLocation(start_address);
|
|
1003 if ( loc is null ) {
|
|
1004 DbgIO.println("No source line information available");
|
|
1005 break;
|
|
1006 }
|
|
1007 DisAsm.disasm(
|
|
1008 dbg.process,
|
|
1009 loc.getCodeBase+loc.codeblock.start,
|
|
1010 loc.getCodeBase+loc.codeblock.end,
|
|
1011 &printDisassembly
|
|
1012 );
|
|
1013 }
|
|
1014 break;
|
|
1015 // dump registers
|
|
1016 case "dr":
|
|
1017 if ( lastcmd.length > 1 )
|
|
1018 {
|
|
1019 foreach ( lc; lastcmd[1..$] )
|
|
1020 {
|
|
1021 switch ( lc )
|
|
1022 {
|
|
1023 case "cpu": dump_registers |= CPU_REGISTERS; break;
|
|
1024 case "fpu": dump_registers |= FPU_REGISTERS; break;
|
|
1025 case "mmx": dump_registers |= MMX_REGISTERS; break;
|
|
1026 case "sse": dump_registers |= SSE_REGISTERS; break;
|
|
1027 default:
|
|
1028 DbgIO.println("Unknown register set \"%s\"", lc);
|
|
1029 }
|
|
1030 }
|
|
1031 }
|
|
1032 if ( dump_registers == 0 ) {
|
|
1033 DbgIO.println("No register set selected. See help for usage of \"dr\"");
|
|
1034 break;
|
|
1035 }
|
|
1036 if ( dbg.miniDump !is null )
|
|
1037 printRegisterDump(dbg.miniDump.getContext);
|
|
1038 else if ( dbg.process_loaded )
|
|
1039 printRegisterDump(dbg.process.threads[dbg.thread_id]);
|
|
1040 break;
|
|
1041 // dump stack
|
|
1042 case "ds":
|
|
1043 if ( !dbg.process_loaded )
|
|
1044 break;
|
|
1045
|
|
1046 int dump_length;
|
|
1047 if ( lastcmd.length > 1 )
|
|
1048 dump_length = cast(int)atoi(lastcmd[1])*4;
|
|
1049 else
|
|
1050 {
|
|
1051 CONTEXT ctx;
|
|
1052 if ( dbg.process.threads[dbg.thread_id].getContext(ctx, CONTEXT_CONTROL) )
|
|
1053 dump_length = ctx.Ebp-ctx.Esp+8;
|
|
1054 if ( dump_length <= 0 )
|
|
1055 dump_length = 16*4;
|
|
1056 }
|
|
1057 int top = dbg.stack.data.length>dump_length?dump_length:dbg.stack.data.length;
|
|
1058 dumpMemory(dbg.stack.top_ptr, top, dbg.stack.data);
|
|
1059 break;
|
|
1060 case "dm":
|
|
1061 if ( !dbg.process_loaded )
|
|
1062 break;
|
|
1063
|
|
1064 if ( lastcmd.length < 3 ) {
|
|
1065 DbgIO.println("usage: dm <start> <length>");
|
|
1066 break;
|
|
1067 }
|
|
1068 uint start;
|
|
1069 sscanf(toStringz(lastcmd[1]), "%x", &start);
|
|
1070 dumpMemory(start, cast(uint)atoi(lastcmd[2]));
|
|
1071 break;
|
|
1072 case "hwbp":
|
|
1073 int index;
|
|
1074 if ( lastcmd.length < 2 ) {
|
|
1075 DbgIO.println("invalid syntax - see help for details");
|
|
1076 break;
|
|
1077 }
|
|
1078
|
|
1079 int pos = find(lastcmd[1], '#');
|
|
1080 uint threadId;
|
|
1081 if ( pos > 0 )
|
|
1082 {
|
|
1083 threadId = cast(uint)atoi(lastcmd[1][pos+1..$]);
|
|
1084 lastcmd[1] = lastcmd[1][0..pos];
|
|
1085 }
|
|
1086
|
|
1087 Location loc;
|
|
1088
|
|
1089 size_t address;
|
|
1090 sscanf(toStringz(lastcmd[1]), "%x", &address);
|
|
1091 if ( address > 0 )
|
|
1092 loc = new Location(address);
|
|
1093 else
|
|
1094 loc = new Location(lastcmd[1]);
|
|
1095 loc.bind(dbg.images, dbg.source_search_paths);
|
|
1096 if ( lastcmd.length > 2 )
|
|
1097 index = cast(int)atoi(lastcmd[2]);
|
|
1098
|
|
1099 Breakpoint bp;
|
|
1100 if ( index <= 0 && dbg.breakpoints.length > 0 )
|
|
1101 index = dbg.breakpoints.keys.dup.sort[$-1]+1;
|
|
1102 bp = dbg.setBreakpoint(loc, index, threadId, true);
|
|
1103 DbgIO.println("Hardware Breakpoint set: %s", bp.toString);
|
|
1104 break;
|
|
1105 // type of expression
|
|
1106 case "t":
|
|
1107 if ( dbg.process_loaded && lastcmd.length > 1 )
|
|
1108 {
|
|
1109 SymbolData sd;
|
|
1110 try {
|
|
1111 sd = dbg.evaluateExpression(lastcmd[1], current_frame_level);
|
|
1112 string type = demangle("_D0"~sd.type);
|
|
1113 DbgIO.println("%s\n%s", sd.type, type);
|
|
1114 }
|
|
1115 catch ( EvaluationException e ) {
|
|
1116 DbgIO.writeln(e.toString);
|
|
1117 }
|
|
1118 }
|
|
1119 break;
|
|
1120 // unwind stack
|
|
1121 case "us":
|
|
1122 if ( dbg.process_loaded || dbg.miniDump !is null )
|
|
1123 unwindStack();
|
|
1124 break;
|
|
1125 // write minidump
|
|
1126 case "wmd":
|
|
1127 if ( dbg.miniDump !is null ) {
|
|
1128 DbgIO.println("Command not valid in post-mortem mode");
|
|
1129 break;
|
|
1130 }
|
|
1131 if ( !dbg.process_loaded )
|
|
1132 break;
|
|
1133 if ( !MiniDump.haveMiniDump ) {
|
|
1134 DbgIO.println("DbgHelp.dll required for MiniDump support.\nInstall the Microsoft Platform SDK or Windows XP.");
|
|
1135 break;
|
|
1136 }
|
|
1137 if ( lastcmd.length < 2 ) {
|
|
1138 DbgIO.println("Usage: wmd <filename>");
|
|
1139 break;
|
|
1140 }
|
|
1141 dbg.writeMiniDump(lastcmd[1]);
|
|
1142 break;
|
|
1143 // read minidump
|
|
1144 case "rmd":
|
|
1145 if ( dbg.process_loaded ) {
|
|
1146 DbgIO.println("For post-mortem debugging, the process must not be started");
|
|
1147 break;
|
|
1148 }
|
|
1149 if ( !MiniDump.haveMiniDump ) {
|
|
1150 DbgIO.println("DbgHelp.dll required for MiniDump support.\nInstall the Microsoft Platform SDK or Windows XP.");
|
|
1151 break;
|
|
1152 }
|
|
1153 if ( lastcmd.length < 2 ) {
|
|
1154 DbgIO.println("Usage: rmd <filename>");
|
|
1155 break;
|
|
1156 }
|
|
1157 dbg.readMiniDump(lastcmd[1]);
|
|
1158 break;
|
|
1159 // unknown command
|
|
1160 default:
|
|
1161 DbgIO.println("Unknown command '%s' ignored!", lastcmd[0]);
|
|
1162 break;
|
|
1163 }
|
|
1164
|
|
1165 return false;
|
|
1166 }
|
|
1167
|
|
1168 /**********************************************************************************************
|
|
1169
|
|
1170 **********************************************************************************************/
|
|
1171 void dumpMemory(uint start, uint length, ubyte[] _data=null)
|
|
1172 {
|
|
1173 ubyte[] data;
|
|
1174 if ( _data is null )
|
|
1175 {
|
|
1176 data.length = length;
|
|
1177 if ( !dbg.process.readProcessMemory(start, data.ptr, data.length) )
|
|
1178 return;
|
|
1179 }
|
|
1180 else
|
|
1181 data = _data;
|
|
1182 for ( uint i = 0; i < length; ++i )
|
|
1183 {
|
|
1184 if ( i % (4*4) == 0 )
|
|
1185 {
|
|
1186 if ( i > 0 )
|
|
1187 DbgIO.println("");
|
|
1188 DbgIO.print("%08x:", start+i);
|
|
1189 }
|
|
1190 if ( i % 4 == 0 )
|
|
1191 DbgIO.print(" ");
|
|
1192 DbgIO.print("%02x", data[i]);
|
|
1193 }
|
|
1194 DbgIO.println("");
|
|
1195 }
|
|
1196
|
|
1197 /**********************************************************************************************
|
|
1198
|
|
1199 **********************************************************************************************/
|
|
1200 void evalScopeSymbols()
|
|
1201 {
|
|
1202 uint scope_address = dbg.getScopeAddress(current_frame_level);
|
|
1203
|
|
1204 ScopeSymbol scope_sym = dbg.images.findProcedureSymbol(scope_address);
|
|
1205 if ( scope_sym is null ) {
|
|
1206 DbgIO.println("No valid scope active");
|
|
1207 return;
|
|
1208 }
|
|
1209
|
|
1210 for ( ; scope_sym !is null; scope_sym = scope_sym.parent_scope )
|
|
1211 {
|
|
1212 DbgIO.println("Scope: %s, parent: %x", scope_sym.name_type, cast(void*)scope_sym.parent_scope);
|
|
1213
|
|
1214 StackSymbol[] locals_args = scope_sym.symbols.stack_symbols;
|
|
1215 auto psym = cast(ProcedureSymbol)scope_sym;
|
|
1216 if ( psym !is null )
|
|
1217 locals_args ~= psym.arguments.stack_symbols;
|
|
1218 foreach ( sym; locals_args.sort )
|
|
1219 {
|
|
1220 string name = sym.mangled_name;
|
|
1221 DbgIO.print("%s = ", name);
|
|
1222 // DbgIO.print("%s = [ebp%s%d] ", name, sym.cvdata.offset>0?"+":"", sym.cvdata.offset);
|
|
1223 try DbgIO.writeln(symbolValueToString(dbg.handleData(dbg.evaluateExpression(name, current_frame_level, sym), true)));
|
|
1224 catch ( EvaluationException e ) {
|
|
1225 DbgIO.println(e.msg);
|
|
1226 }
|
|
1227 }
|
|
1228 }
|
|
1229 }
|
|
1230
|
|
1231 /**********************************************************************************************
|
|
1232
|
|
1233 **********************************************************************************************/
|
|
1234 void printAsmLine(uint address, string bytes, string asmsource, string symbol, string location, string source)
|
|
1235 {
|
|
1236 bool nl = false;
|
|
1237 if ( location !is null ) {
|
|
1238 DbgIO.print("%s ", location);
|
|
1239 nl = true;
|
|
1240 }
|
|
1241 if ( source !is null ) {
|
|
1242 DbgIO.write(source);
|
|
1243 nl = true;
|
|
1244 }
|
|
1245 if ( nl )
|
|
1246 DbgIO.println;
|
|
1247
|
|
1248 // align next column
|
|
1249 char[] indent;
|
|
1250 int indcount = 2*12-bytes.length;
|
|
1251 if ( indcount > 0 ) {
|
|
1252 indent.length = indcount;
|
|
1253 indent[0..indcount] = ' ';
|
|
1254 }
|
|
1255 // print aligned asm source
|
|
1256 assert(asmsource !is null);
|
|
1257 assert(bytes !is null);
|
|
1258 DbgIO.print("%08x: ", address, bytes, indent, asmsource);
|
|
1259
|
|
1260 if ( symbol !is null )
|
|
1261 DbgIO.write(symbol);
|
|
1262 DbgIO.println;
|
|
1263 }
|
|
1264
|
|
1265 /**********************************************************************************************
|
|
1266 Prints the register contents of the given thread's context.
|
|
1267 **********************************************************************************************/
|
|
1268 void printRegisterDump(DbgThread thread)
|
|
1269 {
|
|
1270 CONTEXT ctxMem;
|
|
1271 uint context_flags;
|
|
1272 if ( dump_registers & CPU_REGISTERS )
|
|
1273 context_flags |= CONTEXT_FULL;
|
|
1274 if ( dump_registers & FPU_REGISTERS )
|
|
1275 context_flags |= CONTEXT_FLOATING_POINT;
|
|
1276 if ( dump_registers & (MMX_REGISTERS|SSE_REGISTERS) )
|
|
1277 context_flags |= CONTEXT_EXTENDED_REGISTERS;
|
|
1278 if ( thread.getContext(ctxMem, context_flags) )
|
|
1279 printRegisterDump(&ctxMem);
|
|
1280 else
|
|
1281 DbgIO.println("ERROR: Couldn't get main thread's context");
|
|
1282 }
|
|
1283
|
|
1284 void printRegisterDump(CONTEXT* ctx)
|
|
1285 {
|
|
1286 assert ( ctx !is null );
|
|
1287
|
|
1288 bool first = true;
|
|
1289
|
|
1290 if ( dump_registers & CPU_REGISTERS )
|
|
1291 {
|
|
1292 DbgIO.println("EAX = %08x\tEBX = %08x\tECX = %08x\tEDX = %08x", ctx.Eax, ctx.Ebx, ctx.Ecx, ctx.Edx);
|
|
1293 DbgIO.println("EDI = %08x\tESI = %08x\tEBP = %08x\tESP = %08x", ctx.Edi, ctx.Esi, ctx.Ebp, ctx.Esp);
|
|
1294 DbgIO.println("EIP = %08x\tEFL = %08x", ctx.Eip, ctx.EFlags);
|
|
1295 DbgIO.println(" CS = %08x\t DS = %08x\t ES = %08x\t FS = %08x", ctx.SegCs, ctx.SegDs, ctx.SegEs, ctx.SegFs);
|
|
1296 DbgIO.println(" GS = %08x\t SS = %08x", ctx.SegGs, ctx.SegSs);
|
|
1297 first = false;
|
|
1298 }
|
|
1299
|
|
1300 if ( dump_registers & FPU_REGISTERS )
|
|
1301 {
|
|
1302 if ( !first )
|
|
1303 DbgIO.println();
|
|
1304 DbgIO.println("FCW = %04x\tFSW = %04x\tFTW = %04x\tFOP = %04x",
|
|
1305 cast(ushort)ctx.FloatSave.ControlWord, cast(ushort)ctx.FloatSave.StatusWord,
|
|
1306 cast(ushort)ctx.FloatSave.TagWord, (cast(ushort[])ctx.ExtendedRegisters)[3]
|
|
1307 );
|
|
1308 DbgIO.println("IP = %08x\tCS = %04x\tDP = %08x\tDS = %04x",
|
|
1309 (cast(uint[])ctx.ExtendedRegisters)[2], (cast(ushort[])ctx.ExtendedRegisters)[6],
|
|
1310 (cast(uint[])ctx.ExtendedRegisters)[4], (cast(ushort[])ctx.ExtendedRegisters)[10]
|
|
1311 );
|
|
1312 for ( int i = 0; i < 8; ++i )
|
|
1313 DbgIO.println("ST%d = % .16e", i, (cast(real[])ctx.FloatSave.RegisterArea)[i]);
|
|
1314 first = false;
|
|
1315 }
|
|
1316
|
|
1317 if ( dump_registers & MMX_REGISTERS )
|
|
1318 {
|
|
1319 if ( !first )
|
|
1320 DbgIO.println();
|
|
1321 for ( int i = 0; i < 8; ++i )
|
|
1322 {
|
|
1323 DbgIO.println("MM%d = %016x", i, (cast(long[])ctx.ExtendedRegisters)[4+i*2]);
|
|
1324 DbgIO.println(" = [%.6g, %.6g]",
|
|
1325 (cast(float[])ctx.ExtendedRegisters)[8+i*4],
|
|
1326 (cast(float[])ctx.ExtendedRegisters)[8+i*4+1]
|
|
1327 );
|
|
1328 }
|
|
1329 first = false;
|
|
1330 }
|
|
1331
|
|
1332 if ( dump_registers & SSE_REGISTERS )
|
|
1333 {
|
|
1334 if ( !first )
|
|
1335 DbgIO.println();
|
|
1336 DbgIO.println("MXCSR = %08x", (cast(uint[])ctx.ExtendedRegisters)[6]);
|
|
1337 for ( int i = 0; i < 8; ++i )
|
|
1338 {
|
|
1339 DbgIO.println("XMM%d = %016x%016x", i, (cast(long[])ctx.ExtendedRegisters)[20+i*2+1], (cast(long[])ctx.ExtendedRegisters)[20+i*2]);
|
|
1340 DbgIO.println(" = [%.6g, %.6g, %.6g, %.6g]",
|
|
1341 (cast(float[])ctx.ExtendedRegisters)[40+i*4],
|
|
1342 (cast(float[])ctx.ExtendedRegisters)[40+i*4+1],
|
|
1343 (cast(float[])ctx.ExtendedRegisters)[40+i*4+2],
|
|
1344 (cast(float[])ctx.ExtendedRegisters)[40+i*4+3]
|
|
1345 );
|
|
1346 DbgIO.println(" = [%.12g, %.12g]",
|
|
1347 (cast(double[])ctx.ExtendedRegisters)[20+i*2],
|
|
1348 (cast(double[])ctx.ExtendedRegisters)[20+i*2+1]
|
|
1349 );
|
|
1350 }
|
|
1351 }
|
|
1352 }
|
|
1353
|
|
1354 /**********************************************************************************************
|
|
1355
|
|
1356 **********************************************************************************************/
|
|
1357 string symbolValueToString(SymbolValue val)
|
|
1358 {
|
|
1359 return symbolValueToString(val,"");
|
|
1360 }
|
|
1361
|
|
1362 string symbolValueToString(SymbolValue val, string indent)
|
|
1363 {
|
|
1364 string str;
|
|
1365 if ( val.name !is null )
|
|
1366 str = val.name~" = ";
|
|
1367 if ( val.value !is null )
|
|
1368 str ~= val.value;
|
|
1369 else
|
|
1370 {
|
|
1371 if ( val.children.length > 0 )
|
|
1372 {
|
|
1373 str ~= "{";
|
|
1374 bool first = true;
|
|
1375 foreach ( c; val.children )
|
|
1376 {
|
|
1377 if ( first )
|
|
1378 first = false;
|
|
1379 else
|
|
1380 str ~= ",";
|
|
1381 str ~= "\n "~indent~symbolValueToString(c, indent~" ");
|
|
1382 }
|
|
1383 str ~= "\n"~indent~"}";
|
|
1384 }
|
|
1385 else
|
|
1386 str ~= "{}";
|
|
1387 }
|
|
1388 return str;
|
|
1389 }
|
|
1390
|
|
1391 /**********************************************************************************************
|
|
1392 Read command and call CLI supplied parser function.
|
|
1393 Gets called when debuggee is suspended.
|
|
1394 **********************************************************************************************/
|
|
1395 bool readCommand()
|
|
1396 {
|
|
1397 if ( cmdQueue.length <= 0 ) {
|
|
1398 DbgIO.write("->");
|
|
1399 string input = DbgIO.readln();
|
|
1400 cmdQueue ~= input;
|
|
1401 }
|
|
1402 if ( cmdQueue.length <= 0 )
|
|
1403 return false;
|
|
1404
|
|
1405 string cmd = strip(cmdQueue[0]);
|
|
1406 cmdQueue = cmdQueue[1..$];
|
|
1407 if ( cmd.length > 0 && cmd[$-1] == ';' ) {
|
|
1408 cmd = cmd[0..$-1];
|
|
1409 DbgIO.writeln("->"~cmd);
|
|
1410 }
|
|
1411 return parseCommand(cmd);
|
|
1412 }
|
|
1413 }
|