]> git.saurik.com Git - wxWidgets.git/blame - src/msw/debughlp.cpp
Override PLATFORM_WIN32 setting if using Wine on Linux/Unix
[wxWidgets.git] / src / msw / debughlp.cpp
CommitLineData
3d8b5d85
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: msw/debughlp.cpp
3// Purpose: various Win32 debug helpers
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 2005-01-08 (extracted from crashrpt.cpp)
7// RCS-ID: $Id$
8// Copyright: (c) 2003-2005 Vadim Zeitlin <vadim@wxwindows.org>
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#include "wx/msw/debughlp.h"
27
28#if wxUSE_DBGHELP
29
30// ----------------------------------------------------------------------------
31// globals
32// ----------------------------------------------------------------------------
33
34// error message from Init()
35static wxString gs_errMsg;
36
37// ============================================================================
38// wxDbgHelpDLL implementation
39// ============================================================================
40
41// ----------------------------------------------------------------------------
42// static members
43// ----------------------------------------------------------------------------
44
45#define DEFINE_SYM_FUNCTION(func) wxDbgHelpDLL::func ## _t wxDbgHelpDLL::func = 0
46
47wxDO_FOR_ALL_SYM_FUNCS(DEFINE_SYM_FUNCTION);
48
49#undef DEFINE_SYM_FUNCTION
50
51// ----------------------------------------------------------------------------
52// initialization methods
53// ----------------------------------------------------------------------------
54
55// load all function we need from the DLL
56
57static bool BindDbgHelpFunctions(const wxDynamicLibrary& dllDbgHelp)
58{
59 #define LOAD_SYM_FUNCTION(name) \
60 wxDbgHelpDLL::name = (wxDbgHelpDLL::name ## _t) \
61 dllDbgHelp.GetSymbol(_T(#name)); \
62 if ( !wxDbgHelpDLL::name ) \
63 { \
64 gs_errMsg += _T("Function ") _T(#name) _T("() not found.\n"); \
65 return false; \
66 }
67
68 wxDO_FOR_ALL_SYM_FUNCS(LOAD_SYM_FUNCTION);
69
70 #undef LOAD_SYM_FUNCTION
71
72 return true;
73}
74
75// called by Init() if we hadn't done this before
76static bool DoInit()
77{
78 wxDynamicLibrary dllDbgHelp(_T("dbghelp.dll"), wxDL_VERBATIM);
79 if ( dllDbgHelp.IsLoaded() )
80 {
81 if ( BindDbgHelpFunctions(dllDbgHelp) )
82 {
83 // turn on default options
84 DWORD options = wxDbgHelpDLL::SymGetOptions();
85
86 options |= SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME | SYMOPT_DEBUG;
87
88 wxDbgHelpDLL::SymSetOptions(options);
89
90 dllDbgHelp.Detach();
91 return true;
92 }
93
94 gs_errMsg += _T("\nPlease update your dbghelp.dll version, ")
95 _T("at least version 5.1 is needed!\n")
96 _T("(if you already have a new version, please ")
97 _T("put it in the same directory where the program is.)\n");
98 }
99 else // failed to load dbghelp.dll
100 {
101 gs_errMsg += _T("Please install dbghelp.dll available free of charge ")
102 _T("from Microsoft to get more detailed crash information!");
103 }
104
105 gs_errMsg += _T("\nLatest dbghelp.dll is available at ")
106 _T("http://www.microsoft.com/whdc/ddk/debugging/\n");
107
108 return false;
109}
110
111/* static */
112bool wxDbgHelpDLL::Init()
113{
114 // this flag is -1 until Init() is called for the first time, then it's set
115 // to either false or true depending on whether we could load the functions
116 static int s_loaded = -1;
117
118 if ( s_loaded == -1 )
119 {
120 s_loaded = DoInit();
121 }
122
123 return s_loaded != 0;
124}
125
126// ----------------------------------------------------------------------------
127// error handling
128// ----------------------------------------------------------------------------
129
130/* static */
131const wxString& wxDbgHelpDLL::GetErrorMessage()
132{
133 return gs_errMsg;
134}
135
136/* static */
137void wxDbgHelpDLL::LogError(const wxChar *func)
138{
139 ::OutputDebugString(wxString::Format(_T("dbghelp: %s() failed: %s\r\n"),
140 func, wxSysErrorMsg(::GetLastError())));
141}
142
143// ----------------------------------------------------------------------------
144// data dumping
145// ----------------------------------------------------------------------------
146
147static inline
148bool
149DoGetTypeInfo(DWORD64 base, ULONG ti, IMAGEHLP_SYMBOL_TYPE_INFO type, void *rc)
150{
151 static HANDLE s_hProcess = ::GetCurrentProcess();
152
153 return wxDbgHelpDLL::SymGetTypeInfo
154 (
155 s_hProcess,
156 base,
157 ti,
158 type,
159 rc
160 ) != 0;
161}
162
163static inline
164bool
165DoGetTypeInfo(PSYMBOL_INFO pSym, IMAGEHLP_SYMBOL_TYPE_INFO type, void *rc)
166{
167 return DoGetTypeInfo(pSym->ModBase, pSym->TypeIndex, type, rc);
168}
169
170static inline
171wxDbgHelpDLL::BasicType GetBasicType(PSYMBOL_INFO pSym)
172{
173 wxDbgHelpDLL::BasicType bt;
174 return DoGetTypeInfo(pSym, TI_GET_BASETYPE, &bt)
175 ? bt
176 : wxDbgHelpDLL::BASICTYPE_NOTYPE;
177}
178
179/* static */
180wxString wxDbgHelpDLL::GetSymbolName(PSYMBOL_INFO pSym)
181{
182 wxString s;
183
184 WCHAR *pwszTypeName;
185 if ( SymGetTypeInfo
186 (
187 GetCurrentProcess(),
188 pSym->ModBase,
189 pSym->TypeIndex,
190 TI_GET_SYMNAME,
191 &pwszTypeName
192 ) )
193 {
194 s = wxConvCurrent->cWC2WX(pwszTypeName);
195
196 ::LocalFree(pwszTypeName);
197 }
198
199 return s;
200}
201
202/* static */ wxString
203wxDbgHelpDLL::DumpBaseType(BasicType bt, DWORD64 length, PVOID pAddress)
204{
205 if ( !pAddress )
206 {
207 return _T("null");
208 }
209
210 if ( ::IsBadReadPtr(pAddress, length) != 0 )
211 {
212 return _T("BAD");
213 }
214
215
216 wxString s;
217 s.reserve(256);
218
219 if ( length == 1 )
220 {
221 const BYTE b = *(PBYTE)pAddress;
222
223 if ( bt == BASICTYPE_BOOL )
224 s = b ? _T("true") : _T("false");
225 else
226 s.Printf(_T("%#04x"), b);
227 }
228 else if ( length == 2 )
229 {
230 s.Printf(bt == BASICTYPE_UINT ? _T("%#06x") : _T("%d"),
231 *(PWORD)pAddress);
232 }
233 else if ( length == 4 )
234 {
235 bool handled = false;
236
237 if ( bt == BASICTYPE_FLOAT )
238 {
239 s.Printf(_T("%f"), *(PFLOAT)pAddress);
240
241 handled = true;
242 }
243 else if ( bt == BASICTYPE_CHAR )
244 {
245 // don't take more than 32 characters of a string
246 static const size_t NUM_CHARS = 64;
247
248 const char *pc = *(PSTR *)pAddress;
249 if ( ::IsBadStringPtrA(pc, NUM_CHARS) == 0 )
250 {
251 s += _T('"');
252 for ( size_t n = 0; n < NUM_CHARS && *pc; n++, pc++ )
253 {
254 s += *pc;
255 }
256 s += _T('"');
257
258 handled = true;
259 }
260 }
261
262 if ( !handled )
263 {
264 // treat just as an opaque DWORD
265 s.Printf(_T("%#x"), *(PDWORD)pAddress);
266 }
267 }
268 else if ( length == 8 )
269 {
270 if ( bt == BASICTYPE_FLOAT )
271 {
272 s.Printf(_T("%lf"), *(double *)pAddress);
273 }
274 else // opaque 64 bit value
275 {
276 s.Printf(_T("%#" wxLongLongFmtSpec _T("x")), *(PDWORD *)pAddress);
277 }
278 }
279
280 return s;
281}
282
283wxString
284wxDbgHelpDLL::DumpField(PSYMBOL_INFO pSym, void *pVariable, unsigned level)
285{
286 wxString s;
287
288 // avoid infinite recursion
289 if ( level > 100 )
290 {
291 return s;
292 }
293
294 SymbolTag tag = SYMBOL_TAG_NULL;
295 if ( !DoGetTypeInfo(pSym, TI_GET_SYMTAG, &tag) )
296 {
297 return s;
298 }
299
300 switch ( tag )
301 {
302 case SYMBOL_TAG_UDT:
303 case SYMBOL_TAG_BASE_CLASS:
304 s = DumpUDT(pSym, pVariable, level);
305 break;
306
307 case SYMBOL_TAG_DATA:
120678ee 308 if ( !pVariable )
3d8b5d85 309 {
120678ee 310 s = _T("NULL");
3d8b5d85 311 }
120678ee
VZ
312 else // valid location
313 {
314 wxDbgHelpDLL::DataKind kind;
315 if ( !DoGetTypeInfo(pSym, TI_GET_DATAKIND, &kind) ||
316 kind != DATA_MEMBER )
317 {
318 // maybe it's a static member? we're not interested in them...
319 break;
320 }
3d8b5d85 321
120678ee
VZ
322 // get the offset of the child member, relative to its parent
323 DWORD ofs = 0;
324 if ( !DoGetTypeInfo(pSym, TI_GET_OFFSET, &ofs) )
325 break;
3d8b5d85 326
120678ee 327 pVariable = (void *)((DWORD_PTR)pVariable + ofs);
3d8b5d85
VZ
328
329
120678ee
VZ
330 // now pass to the type representing the type of this member
331 SYMBOL_INFO sym = *pSym;
332 if ( !DoGetTypeInfo(pSym, TI_GET_TYPEID, &sym.TypeIndex) )
333 break;
3d8b5d85 334
120678ee
VZ
335 ULONG64 size;
336 DoGetTypeInfo(&sym, TI_GET_LENGTH, &size);
3d8b5d85 337
120678ee
VZ
338 switch ( DereferenceSymbol(&sym, &pVariable) )
339 {
340 case SYMBOL_TAG_BASE_TYPE:
3d8b5d85 341 {
120678ee
VZ
342 BasicType bt = GetBasicType(&sym);
343 if ( bt )
344 {
345 s = DumpBaseType(bt, size, pVariable);
346 }
3d8b5d85 347 }
120678ee 348 break;
3d8b5d85 349
120678ee
VZ
350 case SYMBOL_TAG_UDT:
351 case SYMBOL_TAG_BASE_CLASS:
352 s = DumpUDT(&sym, pVariable, level);
353 break;
354 }
3d8b5d85
VZ
355 }
356
357 if ( !s.empty() )
358 {
359 s = GetSymbolName(pSym) + _T(" = ") + s;
360 }
361 break;
362 }
363
364 if ( !s.empty() )
365 {
366 s = wxString(_T('\t'), level + 1) + s + _T('\n');
367 }
368
369 return s;
370}
371
372/* static */ wxString
373wxDbgHelpDLL::DumpUDT(PSYMBOL_INFO pSym, void *pVariable, unsigned level)
374{
375 wxString s;
737c443b
VZ
376
377 // we have to limit the depth of UDT dumping as otherwise we get in
378 // infinite loops trying to dump linked lists... 10 levels seems quite
379 // reasonable, full information is in minidump file anyhow
380 if ( level > 10 )
381 return s;
382
3d8b5d85
VZ
383 s.reserve(512);
384 s = GetSymbolName(pSym);
385
386#if !wxUSE_STL
387 // special handling for ubiquitous wxString: although the code below works
388 // for it as well, it shows the wxStringBase class and takes 4 lines
389 // instead of only one as this branch
390 if ( s == _T("wxString") )
391 {
392 wxString *ps = (wxString *)pVariable;
393 s << _T("(\"") << *ps << _T(")\"");
394 }
395 else // any other UDT
396#endif // !wxUSE_STL
397 {
398 // Determine how many children this type has.
399 DWORD dwChildrenCount = 0;
400 DoGetTypeInfo(pSym, TI_GET_CHILDRENCOUNT, &dwChildrenCount);
401
402 // Prepare to get an array of "TypeIds", representing each of the children.
403 TI_FINDCHILDREN_PARAMS *children = (TI_FINDCHILDREN_PARAMS *)
404 malloc(sizeof(TI_FINDCHILDREN_PARAMS) +
405 (dwChildrenCount - 1)*sizeof(ULONG));
406 if ( !children )
407 return s;
408
409 children->Count = dwChildrenCount;
410 children->Start = 0;
411
412 // Get the array of TypeIds, one for each child type
413 if ( !DoGetTypeInfo(pSym, TI_FINDCHILDREN, children) )
414 {
415 free(children);
416 return s;
417 }
418
419 s << _T(" {\n");
420
421 // Iterate through all children
422 SYMBOL_INFO sym;
423 wxZeroMemory(sym);
424 sym.ModBase = pSym->ModBase;
425 for ( unsigned i = 0; i < dwChildrenCount; i++ )
426 {
427 sym.TypeIndex = children->ChildId[i];
428
429 // children here are in lexicographic sense, i.e. we get all our nested
430 // classes and not only our member fields, but we can't get the values
431 // for the members of the nested classes, of course!
432 DWORD nested;
433 if ( DoGetTypeInfo(&sym, TI_GET_NESTED, &nested) && nested )
434 continue;
435
436 // avoid infinite recursion: this does seem to happen sometimes with
437 // complex typedefs...
438 if ( sym.TypeIndex == pSym->TypeIndex )
439 continue;
440
441 s += DumpField(&sym, pVariable, level + 1);
442 }
443
444 free(children);
445
446 s << wxString(_T('\t'), level + 1) << _T('}');
447 }
448
449 return s;
450}
451
452/* static */
453wxDbgHelpDLL::SymbolTag
454wxDbgHelpDLL::DereferenceSymbol(PSYMBOL_INFO pSym, void **ppData)
455{
456 SymbolTag tag = SYMBOL_TAG_NULL;
457 for ( ;; )
458 {
459 if ( !DoGetTypeInfo(pSym, TI_GET_SYMTAG, &tag) )
460 break;
461
462 if ( tag != SYMBOL_TAG_POINTER_TYPE )
463 break;
464
465 ULONG tiNew;
466 if ( !DoGetTypeInfo(pSym, TI_GET_TYPEID, &tiNew) ||
467 tiNew == pSym->TypeIndex )
468 break;
469
470 pSym->TypeIndex = tiNew;
471
472 // remove one level of indirection except for the char strings: we want
473 // to dump "char *" and not a single "char" for them
474 if ( ppData && *ppData && GetBasicType(pSym) != BASICTYPE_CHAR )
120678ee
VZ
475 {
476 DWORD_PTR *pData = (DWORD_PTR *)*ppData;
477
478 if ( ::IsBadReadPtr(pData, sizeof(DWORD_PTR *)) )
479 {
480 break;
481 }
482
483 *ppData = (void *)*pData;
484 }
3d8b5d85
VZ
485 }
486
487 return tag;
488}
489
490/* static */ wxString
491wxDbgHelpDLL::DumpSymbol(PSYMBOL_INFO pSym, void *pVariable)
492{
493 wxString s;
494 SYMBOL_INFO symDeref = *pSym;
495 switch ( DereferenceSymbol(&symDeref, &pVariable) )
496 {
497 case SYMBOL_TAG_UDT:
498 // show UDT recursively
499 s = DumpUDT(&symDeref, pVariable);
500 break;
501
502 case SYMBOL_TAG_BASE_TYPE:
503 // variable of simple type, show directly
504 BasicType bt = GetBasicType(&symDeref);
505 if ( bt )
506 {
507 s = DumpBaseType(bt, pSym->Size, pVariable);
508 }
509 break;
510 }
511
512 return s;
513}
514
515// ----------------------------------------------------------------------------
516// debugging helpers
517// ----------------------------------------------------------------------------
518
e84ba59e
VZ
519// this code is very useful when debugging debughlp.dll-related code but
520// probably not worth having compiled in normally, please do not remove it!
521#if 0 // ndef NDEBUG
3d8b5d85
VZ
522
523static wxString TagString(wxDbgHelpDLL::SymbolTag tag)
524{
525 static const wxChar *tags[] =
526 {
527 _T("null"),
528 _T("exe"),
529 _T("compiland"),
530 _T("compiland details"),
531 _T("compiland env"),
532 _T("function"),
533 _T("block"),
534 _T("data"),
535 _T("annotation"),
536 _T("label"),
537 _T("public symbol"),
538 _T("udt"),
539 _T("enum"),
540 _T("function type"),
541 _T("pointer type"),
542 _T("array type"),
543 _T("base type"),
544 _T("typedef"),
545 _T("base class"),
546 _T("friend"),
547 _T("function arg type"),
548 _T("func debug start"),
549 _T("func debug end"),
550 _T("using namespace"),
551 _T("vtable shape"),
552 _T("vtable"),
553 _T("custom"),
554 _T("thunk"),
555 _T("custom type"),
556 _T("managed type"),
557 _T("dimension"),
558 };
559
560 wxCOMPILE_TIME_ASSERT( WXSIZEOF(tags) == wxDbgHelpDLL::SYMBOL_TAG_MAX,
561 SymbolTagStringMismatch );
562
563 wxString s;
564 if ( tag < WXSIZEOF(tags) )
565 s = tags[tag];
566 else
567 s.Printf(_T("unrecognized tag (%d)"), tag);
568
569 return s;
570}
571
572static wxString KindString(wxDbgHelpDLL::DataKind kind)
573{
574 static const wxChar *kinds[] =
575 {
576 _T("unknown"),
577 _T("local"),
578 _T("static local"),
579 _T("param"),
580 _T("object ptr"),
581 _T("file static"),
582 _T("global"),
583 _T("member"),
584 _T("static member"),
585 _T("constant"),
586 };
587
588 wxCOMPILE_TIME_ASSERT( WXSIZEOF(kinds) == wxDbgHelpDLL::DATA_MAX,
589 DataKindStringMismatch );
590
591 wxString s;
592 if ( kind < WXSIZEOF(kinds) )
593 s = kinds[kind];
594 else
595 s.Printf(_T("unrecognized kind (%d)"), kind);
596
597 return s;
598}
599
600static wxString UdtKindString(wxDbgHelpDLL::UdtKind kind)
601{
602 static const wxChar *kinds[] =
603 {
604 _T("struct"),
605 _T("class"),
606 _T("union"),
607 };
608
609 wxCOMPILE_TIME_ASSERT( WXSIZEOF(kinds) == wxDbgHelpDLL::UDT_MAX,
610 UDTKindStringMismatch );
611
612 wxString s;
613 if ( kind < WXSIZEOF(kinds) )
614 s = kinds[kind];
615 else
616 s.Printf(_T("unrecognized UDT (%d)"), kind);
617
618 return s;
619}
620
621static wxString TypeString(wxDbgHelpDLL::BasicType bt)
622{
623 static const wxChar *types[] =
624 {
625 _T("no type"),
626 _T("void"),
627 _T("char"),
628 _T("wchar"),
629 _T(""),
630 _T(""),
631 _T("int"),
632 _T("uint"),
633 _T("float"),
634 _T("bcd"),
635 _T("bool"),
636 _T(""),
637 _T(""),
638 _T("long"),
639 _T("ulong"),
640 _T(""),
641 _T(""),
642 _T(""),
643 _T(""),
644 _T(""),
645 _T(""),
646 _T(""),
647 _T(""),
648 _T(""),
649 _T(""),
650 _T("CURRENCY"),
651 _T("DATE"),
652 _T("VARIANT"),
653 _T("complex"),
654 _T("bit"),
655 _T("BSTR"),
656 _T("HRESULT"),
657 };
658
659 wxCOMPILE_TIME_ASSERT( WXSIZEOF(types) == wxDbgHelpDLL::BASICTYPE_MAX,
660 BasicTypeStringMismatch );
661
662 wxString s;
663 if ( bt < WXSIZEOF(types) )
664 s = types[bt];
665
666 if ( s.empty() )
667 s.Printf(_T("unrecognized type (%d)"), bt);
668
669 return s;
670}
671
672// this function is meant to be called from under debugger to see the
673// proprieties of the given type id
674extern "C" void DumpTI(ULONG ti)
675{
676 SYMBOL_INFO sym = { sizeof(SYMBOL_INFO) };
677 sym.ModBase = 0x400000; // it's a constant under Win32
678 sym.TypeIndex = ti;
679
680 wxDbgHelpDLL::SymbolTag tag = wxDbgHelpDLL::SYMBOL_TAG_NULL;
681 DoGetTypeInfo(&sym, TI_GET_SYMTAG, &tag);
682 DoGetTypeInfo(&sym, TI_GET_TYPEID, &ti);
683
684 OutputDebugString(wxString::Format(_T("Type 0x%x: "), sym.TypeIndex));
685 wxString name = wxDbgHelpDLL::GetSymbolName(&sym);
686 if ( !name.empty() )
687 {
688 OutputDebugString(wxString::Format(_T("name=\"%s\", "), name.c_str()));
689 }
690
691 DWORD nested;
692 if ( !DoGetTypeInfo(&sym, TI_GET_NESTED, &nested) )
693 {
694 nested = FALSE;
695 }
696
697 OutputDebugString(wxString::Format(_T("tag=%s%s"),
f31a4098 698 nested ? _T("nested ") : wxEmptyString,
3d8b5d85
VZ
699 TagString(tag).c_str()));
700 if ( tag == wxDbgHelpDLL::SYMBOL_TAG_UDT )
701 {
702 wxDbgHelpDLL::UdtKind udtKind;
703 if ( DoGetTypeInfo(&sym, TI_GET_UDTKIND, &udtKind) )
704 {
705 OutputDebugString(_T(" (") + UdtKindString(udtKind) + _T(')'));
706 }
707 }
708
709 wxDbgHelpDLL::DataKind kind = wxDbgHelpDLL::DATA_UNKNOWN;
710 if ( DoGetTypeInfo(&sym, TI_GET_DATAKIND, &kind) )
711 {
712 OutputDebugString(wxString::Format(
713 _T(", kind=%s"), KindString(kind).c_str()));
714 if ( kind == wxDbgHelpDLL::DATA_MEMBER )
715 {
716 DWORD ofs = 0;
717 if ( DoGetTypeInfo(&sym, TI_GET_OFFSET, &ofs) )
718 {
719 OutputDebugString(wxString::Format(_T(" (ofs=0x%x)"), ofs));
720 }
721 }
722 }
723
724 wxDbgHelpDLL::BasicType bt = GetBasicType(&sym);
725 if ( bt )
726 {
727 OutputDebugString(wxString::Format(_T(", type=%s"),
728 TypeString(bt).c_str()));
729 }
730
731 if ( ti != sym.TypeIndex )
732 {
733 OutputDebugString(wxString::Format(_T(", next ti=0x%x"), ti));
734 }
735
e84ba59e 736 OutputDebugString(_T("\r\n"));
3d8b5d85
VZ
737}
738
739#endif // NDEBUG
740
741#endif // wxUSE_DBGHELP