summaryrefslogtreecommitdiffstats
path: root/private/sdktools/imagehlp/upddbg.c
blob: 77885773940875a2c086bba72492fe712a6aa341 (plain) (blame)
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
/*++

Copyright (c) 1995  Microsoft Corporation

Module Name:

    upddbg.c

Abstract:

    This tool updates debug files to match corresponding binary checksum,
    base address and timestamp

Author:

    Matthew Hoehnen (matthoe) 08-Jun-1995

Revision History:

--*/

#include <private.h>


BOOL                            fUpdate;
LPSTR                           CurrentImageName;
LOADED_IMAGE                    CurrentImage;
CHAR                            DebugFilePath[_MAX_PATH];
CHAR                            SymbolPathBuffer[MAX_PATH*10];
LPSTR                           SymbolPath;
DWORD                           dw;
LPSTR                           FilePart;
CHAR                            Buffer[MAX_PATH];
PIMAGE_LOAD_CONFIG_DIRECTORY    ConfigInfo;
CHAR                            c;
LPSTR                           p;
BOOL                            DbgHeaderModified;
ULONG                           CheckSum;
HANDLE                          hDbgFile;
ULONG                           cb;
IMAGE_SEPARATE_DEBUG_HEADER     DbgHeader;


VOID
DisplayUsage(
    VOID
    )
{
    fputs("usage: UPDDBG [switches] image-names... \n"
          "              [-?] display this message\n"
          "              [-u] update image\n"
          "              [-s] path to symbol files\n", stderr
          );
}



int _CRTAPI1
main(
    int argc,
    char *argv[],
    char *envp[]
    )
{
    if (argc <= 1) {
        DisplayUsage();
        return 1;
    }

    _tzset();

    while (--argc) {
        p = *++argv;
        if (*p == '/' || *p == '-') {
            while (c = *++p)
            switch (tolower( c )) {
                case '?':
                    DisplayUsage();
                    return 0;

                case 'u':
                    fUpdate = TRUE;
                    break;

                case 's':
                    argc--, argv++;
                    SymbolPath = *argv;
                    break;

                default:
                    fprintf( stderr, "UPDDBG: Invalid switch - /%c\n", c );
                    DisplayUsage();
                    return 1;
            }
        }
    }

    if (!SymbolPath) {
        if (GetEnvironmentVariable( "_nt_symbol_path", SymbolPathBuffer, sizeof(SymbolPathBuffer)-1 )) {
            SymbolPath = SymbolPathBuffer;
        }
    }

    if (!SymbolPath) {
        fprintf( stderr, "UPDDBG: uknown symbol file path\n" );
        return 1;
    }

    CurrentImageName = p;

    //
    // Map and load the current image
    //

    if (!MapAndLoad( CurrentImageName, NULL, &CurrentImage, FALSE, TRUE )) {
        fprintf( stderr, "UPDDBG: failure mapping and loading %s\n", CurrentImageName );
        return 1;
    }

    CurrentImageName = CurrentImage.ModuleName;

    FlushViewOfFile( CurrentImage.MappedAddress, 0 );

    if (!fUpdate) {
        hDbgFile = FindDebugInfoFile( CurrentImageName, SymbolPath, DebugFilePath );
        if (hDbgFile == INVALID_HANDLE_VALUE || hDbgFile == NULL) {
            fprintf( stderr, "UPDDBG: could not locate DBG file %s\n", CurrentImageName );
            return 1;
        }

        if (!ReadFile( hDbgFile, &DbgHeader, sizeof(IMAGE_SEPARATE_DEBUG_HEADER), &cb, NULL )) {
            fprintf( stderr, "UPDDBG: could not read DBG file %s\n", CurrentImageName );
            return 1;
        }

        printf( "*\n" );
        if (CurrentImage.FileHeader->OptionalHeader.CheckSum != DbgHeader.CheckSum) {
            printf( "*************************************\n" );
            printf( "* WARNING: checksums do not match   *\n" );
            printf( "*************************************\n" );
            printf( "*\n" );
        }

        _strlwr( CurrentImageName );
        _strlwr( DebugFilePath );

        printf( "Image    0x%08x %s\n", CurrentImage.FileHeader->OptionalHeader.CheckSum, CurrentImageName );
        printf( "DBG File 0x%08x %s\n", DbgHeader.CheckSum, DebugFilePath );

        return 0;
    }

    if (!CurrentImage.FileHeader->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) {
        fprintf( stderr, "UPDDBG: symbols have not been split %s\n", CurrentImageName );
        return 1;
    }

    if ( UpdateDebugInfoFileEx( CurrentImageName,
                                SymbolPath,
                                DebugFilePath,
                                CurrentImage.FileHeader,
                                CurrentImage.FileHeader->OptionalHeader.CheckSum
                                ) ) {
        if (GetLastError() == ERROR_INVALID_DATA) {
            printf( "UPDDBG: Warning - Old checksum did not match for %s\n", DebugFilePath );
        }
        printf( "Updated symbols for %s\n", DebugFilePath );
    } else {
        printf( "Unable to update symbols: %s\n", DebugFilePath );
    }

    return 0;
}