summaryrefslogtreecommitdiff
path: root/src/ToolBox/SOS/DacTableGen/diautil.cs
blob: d0e9c811a744455294c3456f6914ad3fa0f4a50e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Text;
using System.IO;
using Dia;
using System.Runtime.InteropServices;
using System.Diagnostics;

/***************************************************************************************
 *
 ***************************************************************************************/

namespace Dia.Util
{

/***************************************************************************************
 *
 ***************************************************************************************/

class Util
{
    public static IDiaSymbol FindSymbol(String symName, IDiaSymbol parent, SymTagEnum symTag)
    {
        IDiaEnumSymbols e;
        parent.findChildren(symTag,
                            symName,
                            (uint)(NameSearchOptions.nsfCaseSensitive),
                            out e);

        IDiaSymbol s;
        uint celt;

        if (e == null || e.count == 0)
            return null;

        e.Next(1, out s, out celt);

        if (e.count > 1)
        {
            for (int i = 1; i < e.count; i++)
            {
                IDiaSymbol s2;
                e.Next(1, out s2, out celt);

                // Diasym reader returns multiple symbols with same RVA in some cases. Issue the warning only
                // if the returned symbols actually differ.
                if (s.virtualAddress != s2.virtualAddress)
                {
                    Shell.Error("Symbol " + symName + " has " + e.count + " matches. Taking first.");
                    break;
                }
            }
        }

        return s;
    }

    public static IDiaSymbol FindClassSymbol(String name, IDiaSymbol sym, SymTagEnum tag)
    {
        IDiaSymbol res = null;
        //Console.WriteLine("Looking for " + name + " in " + sym.name);

        res = Util.FindSymbol(name, sym, tag);

        if (res == null)
        {
            IDiaEnumSymbols e;

            sym.findChildren(
                SymTagEnum.SymTagBaseClass,
                null,
                (uint)NameSearchOptions.nsNone,
                out e);

            if (e == null || e.count == 0)
                return null;

            for (int i = 0; i < e.count && res == null; i++)
            {
                UInt32 celt;
                IDiaSymbol s;
                e.Next(1, out s, out celt);
                res = FindClassSymbol(name, s.type, tag);
            }
        }

        return res;
    }
}

/***************************************************************************************
 *
 ***************************************************************************************/

public class DiaFile
{
    DiaSourceClass  m_dsc;
    IDiaSession     m_session;
    DiaSymbol       m_global;
    IDiaEnumSymbols m_publicsEnum;
    UInt32          m_debugTimestamp;
    String          m_loadedPdbPath;

    public DiaFile(String pdbFile, String dllFile)
    {
        m_dsc = new DiaSourceClass();
        string pdbPath = System.IO.Path.GetDirectoryName(pdbFile);

        // Open the PDB file, validating it matches the supplied DLL file
        DiaLoadCallback loadCallback = new DiaLoadCallback();
        try
        {
            m_dsc.loadDataForExe(dllFile, pdbPath, loadCallback);
        }
        catch (System.Exception diaEx)
        {
            // Provide additional diagnostics context and rethrow
            string msg = "ERROR from DIA loading PDB for specified DLL";
            COMException comEx = diaEx as COMException;
            if (comEx != null)
            {
                if (Enum.IsDefined(typeof(DiaHResults), comEx.ErrorCode))
                {
                    // This is a DIA-specific error code,
                    DiaHResults hr = (DiaHResults)comEx.ErrorCode;
                    msg += ": " + hr.ToString();

                    // Additional clarification for the common case of the DLL not matching the PDB
                    if (hr == DiaHResults.E_PDB_NOT_FOUND)
                    {
                        msg += " - The specified PDB file does not match the specified DLL file";
                    }
                }
            }
            throw new ApplicationException(msg, diaEx);
        }

        // Save the path of the PDB file actually loaded
        Debug.Assert(loadCallback.LoadedPdbPath != null, "Didn't get PDB load callback");
        m_loadedPdbPath = loadCallback.LoadedPdbPath;

        // Also use DIA to get the debug directory entry in the DLL referring
        // to the PDB, and save it's timestamp comparison at runtime.
        m_debugTimestamp = loadCallback.DebugTimeDateStamp;
        Debug.Assert(m_debugTimestamp != 0, "Didn't find debug directory entry");

        m_dsc.openSession(out m_session);
        m_global = new DiaSymbol(m_session.globalScope);
        m_publicsEnum = null;
    }

    public DiaSymbol GlobalSymbol
    {
        get
        { return m_global; }
    }

    /// <summary>
    /// Path of the PDB file actually loaded (must be non-null)
    /// </summary>
    public String LoadedPdbPath
    {
        get { return m_loadedPdbPath; }
    }

    /// <summary>
    /// Timestamp in the debug directory of the DLL corresponding to the PDB loaded (always set non-zero).
    /// </summary>
    public UInt32 DebugTimestamp
    {
        get { return m_debugTimestamp; }
    }

    private void GetPublicsEnum()
    {
        if (m_publicsEnum != null)
            return;

        m_session.findChildren(m_global.IDiaSymbol, SymTagEnum.SymTagPublicSymbol,
                               null, (UInt32) NameSearchOptions.nsNone, out m_publicsEnum);
    }

    public IDiaEnumSymbols PublicSymbols
    {
        get
        {
            GetPublicsEnum();

            if (m_publicsEnum != null)
            {
                IDiaEnumSymbols en;
                m_publicsEnum.Clone(out en);
                return en;
            }
            else
                return null;
        }
    }

    public IDiaEnumSymbols FindPublicSymbols(String name)
    {
        IDiaEnumSymbols se;
        m_session.findChildren(m_global.IDiaSymbol, SymTagEnum.SymTagPublicSymbol,
                               name, (UInt32) NameSearchOptions.nsNone, out se);

        return se;
    }

    // Use only the path we supply, don't look for PDBs anywhere else
    private class DiaLoadCallback : IDiaLoadCallback2
    {
        /// <summary>
        /// The path from with the PDB file was actually loaded, or null if none yet.
        /// </summary>
        public string LoadedPdbPath
        {
            get { return m_loadedPdbPath; }
        }

        /// <summary>
        /// The time stamp in the debug directory corresponding to the PDB that was loaded
        /// </summary>
        public UInt32 DebugTimeDateStamp
        {
            get { return m_debugTimeDateStamp; }
        }

        private string m_loadedPdbPath = null;
        private UInt32 m_debugTimeDateStamp = 0;

        public void NotifyDebugDir(int fExecutable, uint cbData, ref IMAGE_DEBUG_DIRECTORY pbData)
        {
            Debug.Assert(cbData == Marshal.SizeOf(typeof(IMAGE_DEBUG_DIRECTORY)), "Got unexpected size for IMAGE_DEBUG_DIRECTORY");
            // There may be mutliple calls, or calls with no timestamp, but only one entry should be 
            // for the code-view record describing the PDB file.
            if (pbData.Type == ImageDebugType.IMAGE_DEBUG_TYPE_CODEVIEW)
            {
                Debug.Assert(fExecutable == 1, "Got debug directory that wasn't read from an executable");
                Debug.Assert(m_debugTimeDateStamp == 0, "Got unexpected duplicate NotifyDebugDir callback");
                Debug.Assert(pbData.TimeDateStamp != 0, "Got unexpected 0 debug timestamp in DLL");
                m_debugTimeDateStamp = pbData.TimeDateStamp;
            }
        }

        public void NotifyOpenDBG(string dbgPath, int resultCode)
        {
            Debug.Assert(false, "Unexpected DBG opening: " + dbgPath);
        }

        public void NotifyOpenPDB(string pdbPath, int resultCode)
        {
            // Keep track of the path from which DIA loaded the PDB.
            // If resultCode is non-zero, it means DIA is not loading this PDB (eg. just probing paths
            // that may not exist).
            // The DIA SDK docs say the caller will generally ignore any error HResult we return, so we
            // don't get the chance to reject any non-matching paths.
            if (resultCode >= 0)
            {
                Debug.Assert(m_loadedPdbPath == null, "DIA indicated it was loading more than one PDB file!");
                m_loadedPdbPath = pdbPath;
            }
        }

        public int RestrictRegistryAccess()
        {
            return 1;   // don't query the registry
        }

        public int RestrictSymbolServerAccess()
        {
            return 1;   // don't use the symbol server
        }

        public int RestrictDBGAccess()
        {
            return 1;   // don't look for DBG files
        }

        public int RestrictOriginalPathAccess()
        {
            return 1;   // don't look in the full path specified in the debug directory 
        }

        public int RestrictReferencePathAccess()
        {
            return 1;   // Don't look in the directory next to the DLL
        }

        public int RestrictSystemRootAccess()
        {
            return 1;   // Don't look in the system directory
        }
    }

    // From WinNt.h
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct IMAGE_DEBUG_DIRECTORY
    {
        public UInt32 Characteristics;
        public UInt32 TimeDateStamp;
        public UInt16 MajorVersion;
        public UInt16 MinorVersion;
        public ImageDebugType Type;
        public UInt32 SizeOfData;
        public UInt32 AddressOfRawData;
        public UInt32 PointerToRawData;
    }
    private enum ImageDebugType : uint
    {
        IMAGE_DEBUG_TYPE_UNKNOWN = 0,
        IMAGE_DEBUG_TYPE_COFF = 1,
        IMAGE_DEBUG_TYPE_CODEVIEW = 2,
        IMAGE_DEBUG_TYPE_FPO = 3,
        IMAGE_DEBUG_TYPE_MISC = 4,
        IMAGE_DEBUG_TYPE_EXCEPTION = 5,
        IMAGE_DEBUG_TYPE_FIXUP = 6,
        IMAGE_DEBUG_TYPE_BORLAND = 9
    }

    // DIA Callback interface definitions
    // These should be in the interop assembly, but due to a bug in dia2.idl, they're missing.
    // dia2.idl should include these interfaces in the library section to get them into the TLB.
    // However, it's handy to be able to define them ourselves, because it lets us easily
    // set the interface that's most convenient (specifically PreserveSig, and explicit use
    // of IMAGE_DEBUG_DIRECTORY)
    [ComImport,
     InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
     Guid("C32ADB82-73F4-421B-95D5-A4706EDF5DBE"),
     System.Security.SuppressUnmanagedCodeSecurity]
    private interface IDiaLoadCallback
    {
        void NotifyDebugDir(int fExecutable, uint cbData, [In] ref IMAGE_DEBUG_DIRECTORY pbData);

        void NotifyOpenDBG([In, MarshalAs(UnmanagedType.LPWStr)] string dbgPath, int resultCode);

        void NotifyOpenPDB([In, MarshalAs(UnmanagedType.LPWStr)] string pdbPath, int resultCode);

        [PreserveSig]
        // return S_OK (0) to allow symbol path lookup from registry
        int RestrictRegistryAccess();

        [PreserveSig]
        // return S_OK (0) to allow symbol server lookup
        int RestrictSymbolServerAccess();
    }

    [ComImport,
     Guid("4688A074-5A4D-4486-AEA8-7B90711D9F7C"),
     InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
     System.Security.SuppressUnmanagedCodeSecurity]
    private interface IDiaLoadCallback2 : IDiaLoadCallback
    {
        new void NotifyDebugDir(int fExecutable, uint cbData, [In] ref IMAGE_DEBUG_DIRECTORY pbData);

        new void NotifyOpenDBG([In, MarshalAs(UnmanagedType.LPWStr)] string dbgPath, int resultCode);

        new void NotifyOpenPDB([In, MarshalAs(UnmanagedType.LPWStr)] string pdbPath, int resultCode);

        [PreserveSig]
        // return S_OK (0) to allow symbol path lookup from registry
        new int RestrictRegistryAccess();

        [PreserveSig]
        // return S_OK (0) to allow symbol server lookup
        new int RestrictSymbolServerAccess();

        [PreserveSig]
        // return S_OK (0) to allow symbol path lookup from registry
        int RestrictDBGAccess();

        [PreserveSig]
        // return S_OK (0) to allow symbol path lookup from registry
        int RestrictOriginalPathAccess();

        [PreserveSig]
        // return S_OK (0) to allow symbol path lookup from registry
        int RestrictReferencePathAccess();

        [PreserveSig]
        // return S_OK (0) to allow symbol lookup in the system directory
        int RestrictSystemRootAccess();
    }

    // DIA specific HResults from dia2.idl, for providing better diagnostics messages
    private enum DiaHResults : int
    {
        E_PDB_OK = unchecked((int)0x806d0001),
        E_PDB_USAGE                 ,
        E_PDB_OUT_OF_MEMORY         , // not used, use E_OUTOFMEMORY
        E_PDB_FILE_SYSTEM           ,
        E_PDB_NOT_FOUND             ,
        E_PDB_INVALID_SIG           ,
        E_PDB_INVALID_AGE           ,
        E_PDB_PRECOMP_REQUIRED      ,
        E_PDB_OUT_OF_TI             ,
        E_PDB_NOT_IMPLEMENTED       ,   // use E_NOTIMPL
        E_PDB_V1_PDB                ,
        E_PDB_FORMAT                ,
        E_PDB_LIMIT                 ,
        E_PDB_CORRUPT               ,
        E_PDB_TI16                  ,
        E_PDB_ACCESS_DENIED         ,  // use E_ACCESSDENIED
        E_PDB_ILLEGAL_TYPE_EDIT     ,
        E_PDB_INVALID_EXECUTABLE    ,
        E_PDB_DBG_NOT_FOUND         ,
        E_PDB_NO_DEBUG_INFO         ,
        E_PDB_INVALID_EXE_TIMESTAMP ,
        E_PDB_RESERVED              ,
        E_PDB_DEBUG_INFO_NOT_IN_PDB ,
        E_PDB_SYMSRV_BAD_CACHE_PATH ,
        E_PDB_SYMSRV_CACHE_FULL     ,
        E_PDB_MAX           
    }
}

/***************************************************************************************
 *
 ***************************************************************************************/

public class DiaSymbol
{
    protected IDiaSymbol      m_symbol;

    public DiaSymbol(IDiaSymbol symbol)
    {
        if (symbol == null)
            throw new ArgumentNullException();

        m_symbol = symbol;
    }

    public DiaSymbol FindSymbol(String name, SymTagEnum tag)
    {
        DiaSymbol res = null;
        IDiaSymbol sym = Util.FindSymbol(name, m_symbol, tag);

        if (sym != null)
        {
            if ((SymTagEnum)sym.symTag == SymTagEnum.SymTagData)
                res = new DiaDataSymbol(sym);
            else
                res = new DiaSymbol(sym);
        }

        return res;
    }

    public DiaSymbol FindSymbol(String name)
    {
        return FindSymbol(name, SymTagEnum.SymTagNull);
    }

    public DiaSymbol FindClassSymbol(String name, SymTagEnum tag)
    {
        DiaSymbol res = null;
        IDiaSymbol sym = Util.FindClassSymbol(name, m_symbol, tag);

        if (sym != null)
        {
            if ((SymTagEnum)sym.symTag == SymTagEnum.SymTagData)
                res = new DiaDataSymbol(sym);
            else
                res = new DiaSymbol(sym);
        }

        return res;
    }

    public DiaSymbol FindClassSymbol(String name)
    {
        return FindClassSymbol(name, SymTagEnum.SymTagNull);
    }

    public DiaSymbol FindUDTSymbol(String name)
    {
        DiaSymbol res = null;
        IDiaSymbol sym = Util.FindSymbol(name, m_symbol, SymTagEnum.SymTagUDT);

        if (sym == null)
        {
            sym = Util.FindSymbol(name, m_symbol, SymTagEnum.SymTagTypedef);

            if (sym != null)
                sym = sym.type;
        }

        if (sym != null)
            res = new DiaSymbol(sym);

        return res;
    }

    private String GetVariantString(Object o)
    {
        /*
        switch( v.vt )
        {
     //*    LONGLONG       VT_I8
        case VT_I8:
            printf( "%ld", v.llVal );
            break;
     //*    LONG           VT_I4
        case VT_I4:
            printf( "%d", v.lVal );
            break;
     //*    BYTE           VT_UI1
        case VT_UI1:
            printf( "%d", v.bVal);
            break;
     //*    SHORT          VT_I2
        case VT_I2:
            printf( "%d", v.iVal);
            break;
     //*    CHAR           VT_I1
        case VT_I1:
            printf( "%d", v.cVal);
            break;
     //*    USHORT         VT_UI2
        case VT_UI2:
            printf( "%d", v.uiVal);
            break;
    //*    ULONG          VT_UI4
        case VT_UI4:
            printf( "%d", v.ulVal);
            break;
     //*    ULONGLONG      VT_UI8
        case VT_UI8:
            printf( "%ld", v.ullVal);
            break;
     //*    INT            VT_INT
        case VT_INT:
            printf( "%d", v.intVal);
            break;
     //*    UINT           VT_UINT
        case VT_UINT:
            printf( "%d", v.uintVal);
            break;
        default:
            printf( "<Not implemented>" );
            break;
        }
        */
        return "VARIANT";
    }

    private String GetBoundString(IDiaSymbol bound)
    {
        SymTagEnum tag = (SymTagEnum) bound.symTag;
        LocationType kind = (LocationType) bound.locationType;

        System.Diagnostics.Debugger.Break();
        if (tag == SymTagEnum.SymTagData && kind == LocationType.LocIsConstant)
        {
            return GetVariantString(bound.value);
        }

        return bound.name;
    }
    
    public String GetTypeString()
    {
        return GetTypeString(m_symbol);
    }

    private String GetTypeString(IDiaSymbol s)
    {
        SymTagEnum tag = (SymTagEnum) s.symTag;

        if (tag == SymTagEnum.SymTagData || tag == SymTagEnum.SymTagTypedef)
        {
            s = s.type;
            tag = (SymTagEnum) s.symTag;
        }

        StringBuilder str = new StringBuilder();

        if (s.name != null)
        {
            str.Append(s.name);
        }
        else if (tag == SymTagEnum.SymTagPointerType)
        {
            str.Append(GetTypeString(s.type));
            str.Append("*");
        }
        else if (tag == SymTagEnum.SymTagBaseType)
        {
            BasicType bt = (BasicType) s.baseType;
            str.AppendFormat("(base type={0}, len={1:d})", bt.ToString(), s.length);
        }
        else if (tag == SymTagEnum.SymTagArrayType)
        {
            str.Append(GetTypeString(s.type));

            bool succ = true;
            int i;

            try
            {
                UInt32 rank = s.rank;

                IDiaEnumSymbols e;
                s.findChildren(SymTagEnum.SymTagDimension, null, (UInt32) NameSearchOptions.nsNone, out e);

                for (i = 0; i < e.count; i++)
                {
                    IDiaSymbol ds;
                    UInt32 celt;
                    e.Next(1, out ds, out celt);

                    str.Append("[" + GetBoundString(ds.lowerBound) + ".." + GetBoundString(ds.upperBound) + "]");
                }
            }
            catch (Exception)
            {
                succ = false;
            }

            if (succ == false)
            {
                try
                {               
                    succ = true;
                    IDiaEnumSymbols e;
                    s.findChildren(SymTagEnum.SymTagCustomType, null, (UInt32) NameSearchOptions.nsNone, out e);

                    for (i = 0; i < e.count; i++)
                    {
                        IDiaSymbol ds;
                        UInt32 celt;
                        e.Next(1, out ds, out celt);

                        str.Append("[" + GetTypeString(ds) + "]");
                    }
                }
                catch (Exception)
                {
                    succ = false;
                }
            }

            if (succ == false)
            {
                try
                {
                    succ = true;
                    str.AppendFormat("[{0:d}]", s.length/s.type.length );
                }
                catch (Exception)
                {
                    succ = false;
                }
            }
        }
        else if (tag == SymTagEnum.SymTagFunctionType)
        {
            str.Append("Function Type");
        }
        else if (tag == SymTagEnum.SymTagCustomType)
        {
            throw new Exception("NYI");
            /*
            str.Append("Custom Type: ");
            try
            {
                str.Append(s.guid.ToString());
            }
            catch (Exception e)
            {
                try
                {
                    str.AppendFormat("{0:x}:{0:x}", s.oemId, s.oemSymbolId);
                }
                catch (Exception)
                {
                }
            }
            DWORD len = 0;
            if ( s.get_types( 0, &len, NULL ) == S_OK && len > 0 ) {
                IDiaSymbol** psyms = new IDiaSymbol*[ len ];
                s.get_types( len, &len, psyms );
                for ( DWORD i = 0; i < len; ++i ) {
                    printf( " <" );
                    printType( psyms[i] );
                    printf( ">" );
                    psyms[i]->Release();
                }
                delete [] psyms;
            }
            len = 0;
            if ( s.get_dataBytes( 0, &len, NULL ) == S_OK && len > 0 ) {
                BYTE* pdata = new BYTE[ len ];
                s.get_dataBytes( len, &len, pdata );
                printf( "<data" );
                for ( DWORD i = 0; i < len; ++i ) {
                    printf( " %02x", pdata[i] );
                }
                printf( " data>" );
                delete [] pdata;
            }
            */
        } else {
            str.Append( "No Type.");
        }

        return str.ToString();
    }

    public string Name
    {
        get
        { return m_symbol.name; }
    }

    public UInt64 Address
    {
        get
        { return m_symbol.virtualAddress; }
    }

    public UInt32 Offset
    {
        get
        { return (UInt32) m_symbol.offset; }
    }

    public UInt64 Size
    {
        get
        {
            IDiaSymbol symbol = m_symbol;
            UInt64 size = symbol.length;
            if (size != 0)
                return size;

            SymTagEnum tag = (SymTagEnum) symbol.symTag;

            if (tag == SymTagEnum.SymTagData || tag == SymTagEnum.SymTagTypedef)
            {
                symbol = symbol.type;
                tag = (SymTagEnum) symbol.symTag;

                size = symbol.length;
                if (size != 0)
                    return size;
            }

            if (tag == SymTagEnum.SymTagPointerType)
            {
                // @TODO: find a way of determining native word size
                return 4;
            }
            else
            {
                throw new Exception("Unknown length.");
            }
        }
    }

    public IDiaSymbol IDiaSymbol
    {
        get
        { return m_symbol; }
    }
}

public class DiaDataSymbol : DiaSymbol
{
    public DiaDataSymbol(IDiaSymbol symbol) : base(symbol)
    {
        if ((SymTagEnum)symbol.symTag != SymTagEnum.SymTagData)
            throw new Exception("Not a data symbol.");
    }

    public DataKind DataKind
    {
        get
        { return ((DataKind) m_symbol.dataKind); }
    }

    public bool IsGlobal
    {
        get
        { return (DataKind == DataKind.DataIsGlobal); }
    }

    public bool IsFileStatic
    {
        get
        { return (DataKind == DataKind.DataIsFileStatic); }
    }

    public bool IsMember
    {
        get
        { return (DataKind == DataKind.DataIsMember); }
    }

    public bool IsStaticMember
    {
        get
        { return (DataKind == DataKind.DataIsStaticMember); }
    }
}

} // Namespace Dia.Util