1 | /* $Id: ntBldSymDb.cpp 57978 2015-09-30 19:39:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTDirCreateUniqueNumbered, generic implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <Windows.h>
|
---|
32 | #include <Dbghelp.h>
|
---|
33 |
|
---|
34 | #include <iprt/alloca.h>
|
---|
35 | #include <iprt/dir.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/getopt.h>
|
---|
38 | #include <iprt/initterm.h>
|
---|
39 | #include <iprt/list.h>
|
---|
40 | #include <iprt/mem.h>
|
---|
41 | #include <iprt/message.h>
|
---|
42 | #include <iprt/path.h>
|
---|
43 | #include <iprt/stream.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 |
|
---|
47 | #include "r0drv/nt/symdb.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /** A structure member we're interested in. */
|
---|
54 | typedef struct MYMEMBER
|
---|
55 | {
|
---|
56 | /** The member name. */
|
---|
57 | const char * const pszName;
|
---|
58 | /** Reserved. */
|
---|
59 | uint32_t const fFlags;
|
---|
60 | /** The offset of the member. UINT32_MAX if not found. */
|
---|
61 | uint32_t off;
|
---|
62 | /** The size of the member. */
|
---|
63 | uint32_t cb;
|
---|
64 | /** Alternative names, optional.
|
---|
65 | * This is a string of zero terminated strings, ending with an zero length
|
---|
66 | * string (or double '\\0' if you like). */
|
---|
67 | const char * const pszzAltNames;
|
---|
68 | } MYMEMBER;
|
---|
69 | /** Pointer to a member we're interested. */
|
---|
70 | typedef MYMEMBER *PMYMEMBER;
|
---|
71 |
|
---|
72 | /** Members we're interested in. */
|
---|
73 | typedef struct MYSTRUCT
|
---|
74 | {
|
---|
75 | /** The structure name. */
|
---|
76 | const char * const pszName;
|
---|
77 | /** Array of members we're interested in. */
|
---|
78 | MYMEMBER *paMembers;
|
---|
79 | /** The number of members we're interested in. */
|
---|
80 | uint32_t const cMembers;
|
---|
81 | /** Reserved. */
|
---|
82 | uint32_t const fFlags;
|
---|
83 | } MYSTRUCT;
|
---|
84 |
|
---|
85 | /** Architecture. */
|
---|
86 | typedef enum MYARCH
|
---|
87 | {
|
---|
88 | MYARCH_X86,
|
---|
89 | MYARCH_AMD64,
|
---|
90 | MYARCH_DETECT
|
---|
91 | } MYARCH;
|
---|
92 |
|
---|
93 | /** Set of structures for one kernel. */
|
---|
94 | typedef struct MYSET
|
---|
95 | {
|
---|
96 | /** The list entry. */
|
---|
97 | RTLISTNODE ListEntry;
|
---|
98 | /** The source PDB. */
|
---|
99 | char *pszPdb;
|
---|
100 | /** The OS version we've harvested structs for */
|
---|
101 | RTNTSDBOSVER OsVerInfo;
|
---|
102 | /** The architecture. */
|
---|
103 | MYARCH enmArch;
|
---|
104 | /** The structures and their member. */
|
---|
105 | MYSTRUCT aStructs[1];
|
---|
106 | } MYSET;
|
---|
107 | /** Pointer a set of structures for one kernel. */
|
---|
108 | typedef MYSET *PMYSET;
|
---|
109 |
|
---|
110 |
|
---|
111 | /*********************************************************************************************************************************
|
---|
112 | * Global Variables *
|
---|
113 | *********************************************************************************************************************************/
|
---|
114 | /** Verbosity level (-v, --verbose). */
|
---|
115 | static uint32_t g_iOptVerbose = 1;
|
---|
116 | /** Set if we should force ahead despite errors. */
|
---|
117 | static bool g_fOptForce = false;
|
---|
118 |
|
---|
119 | /** The members of the KPRCB structure that we're interested in. */
|
---|
120 | static MYMEMBER g_aKprcbMembers[] =
|
---|
121 | {
|
---|
122 | { "QuantumEnd", 0, UINT32_MAX, UINT32_MAX, NULL },
|
---|
123 | { "DpcQueueDepth", 0, UINT32_MAX, UINT32_MAX, "DpcData[0].DpcQueueDepth\0" },
|
---|
124 | { "VendorString", 0, UINT32_MAX, UINT32_MAX, NULL },
|
---|
125 | };
|
---|
126 |
|
---|
127 | /** The structures we're interested in. */
|
---|
128 | static MYSTRUCT g_aStructs[] =
|
---|
129 | {
|
---|
130 | { "_KPRCB", &g_aKprcbMembers[0], RT_ELEMENTS(g_aKprcbMembers), 0 },
|
---|
131 | };
|
---|
132 |
|
---|
133 | /** List of data we've found. This is sorted by version info. */
|
---|
134 | static RTLISTANCHOR g_SetList;
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * For debug/verbose output.
|
---|
142 | *
|
---|
143 | * @param pszFormat The format string.
|
---|
144 | * @param ... The arguments referenced in the format string.
|
---|
145 | */
|
---|
146 | static void MyDbgPrintf(const char *pszFormat, ...)
|
---|
147 | {
|
---|
148 | if (g_iOptVerbose > 1)
|
---|
149 | {
|
---|
150 | va_list va;
|
---|
151 | va_start(va, pszFormat);
|
---|
152 | RTPrintf("debug: ");
|
---|
153 | RTPrintfV(pszFormat, va);
|
---|
154 | va_end(va);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Returns the name we wish to use in the C code.
|
---|
161 | * @returns Structure name.
|
---|
162 | * @param pStruct The structure descriptor.
|
---|
163 | */
|
---|
164 | static const char *figureCStructName(MYSTRUCT const *pStruct)
|
---|
165 | {
|
---|
166 | const char *psz = pStruct->pszName;
|
---|
167 | while (*psz == '_')
|
---|
168 | psz++;
|
---|
169 | return psz;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Returns the name we wish to use in the C code.
|
---|
175 | * @returns Member name.
|
---|
176 | * @param pMember The member descriptor.
|
---|
177 | */
|
---|
178 | static const char *figureCMemberName(MYMEMBER const *pMember)
|
---|
179 | {
|
---|
180 | return pMember->pszName;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Creates a MYSET with copies of all the data and inserts it into the
|
---|
186 | * g_SetList in a orderly fashion.
|
---|
187 | *
|
---|
188 | * @param pOut The output stream.
|
---|
189 | */
|
---|
190 | static void generateHeader(PRTSTREAM pOut)
|
---|
191 | {
|
---|
192 | RTStrmPrintf(pOut,
|
---|
193 | "/* $" "I" "d" ": $ */\n" /* avoid it being expanded */
|
---|
194 | "/** @file\n"
|
---|
195 | " * IPRT - NT kernel type helpers - Autogenerated, do NOT edit.\n"
|
---|
196 | " */\n"
|
---|
197 | "\n"
|
---|
198 | "/*\n"
|
---|
199 | " * Copyright (C) 2013-2015 Oracle Corporation \n"
|
---|
200 | " *\n"
|
---|
201 | " * This file is part of VirtualBox Open Source Edition (OSE), as\n"
|
---|
202 | " * available from http://www.virtualbox.org. This file is free software;\n"
|
---|
203 | " * you can redistribute it and/or modify it under the terms of the GNU\n"
|
---|
204 | " * General Public License (GPL) as published by the Free Software\n"
|
---|
205 | " * Foundation, in version 2 as it comes in the \"COPYING\" file of the\n"
|
---|
206 | " * VirtualBox OSE distribution. VirtualBox OSE is distributed in the\n"
|
---|
207 | " * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.\n"
|
---|
208 | " *\n"
|
---|
209 | " * The contents of this file may alternatively be used under the terms\n"
|
---|
210 | " * of the Common Development and Distribution License Version 1.0\n"
|
---|
211 | " * (CDDL) only, as it comes in the \"COPYING.CDDL\" file of the\n"
|
---|
212 | " * VirtualBox OSE distribution, in which case the provisions of the\n"
|
---|
213 | " * CDDL are applicable instead of those of the GPL.\n"
|
---|
214 | " *\n"
|
---|
215 | " * You may elect to license modified versions of this file under the\n"
|
---|
216 | " * terms and conditions of either the GPL or the CDDL or both.\n"
|
---|
217 | " */\n"
|
---|
218 | "\n"
|
---|
219 | "\n"
|
---|
220 | "#ifndef ___r0drv_nt_symdbdata_h\n"
|
---|
221 | "#define ___r0drv_nt_symdbdata_h\n"
|
---|
222 | "\n"
|
---|
223 | "#include \"r0drv/nt/symdb.h\"\n"
|
---|
224 | "\n"
|
---|
225 | );
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Generate types.
|
---|
229 | */
|
---|
230 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aStructs); i++)
|
---|
231 | {
|
---|
232 | const char *pszStructName = figureCStructName(&g_aStructs[i]);
|
---|
233 |
|
---|
234 | RTStrmPrintf(pOut,
|
---|
235 | "typedef struct RTNTSDBTYPE_%s\n"
|
---|
236 | "{\n",
|
---|
237 | pszStructName);
|
---|
238 | PMYMEMBER paMembers = g_aStructs[i].paMembers;
|
---|
239 | for (uint32_t j = 0; j < g_aStructs->cMembers; j++)
|
---|
240 | {
|
---|
241 | const char *pszMemName = figureCMemberName(&paMembers[j]);
|
---|
242 | RTStrmPrintf(pOut,
|
---|
243 | " uint32_t off%s;\n"
|
---|
244 | " uint32_t cb%s;\n",
|
---|
245 | pszMemName, pszMemName);
|
---|
246 | }
|
---|
247 |
|
---|
248 | RTStrmPrintf(pOut,
|
---|
249 | "} RTNTSDBTYPE_%s;\n"
|
---|
250 | "\n",
|
---|
251 | pszStructName);
|
---|
252 | }
|
---|
253 |
|
---|
254 | RTStrmPrintf(pOut,
|
---|
255 | "\n"
|
---|
256 | "typedef struct RTNTSDBSET\n"
|
---|
257 | "{\n"
|
---|
258 | " RTNTSDBOSVER%-20s OsVerInfo;\n", "");
|
---|
259 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aStructs); i++)
|
---|
260 | {
|
---|
261 | const char *pszStructName = figureCStructName(&g_aStructs[i]);
|
---|
262 | RTStrmPrintf(pOut, " RTNTSDBTYPE_%-20s %s;\n", pszStructName, pszStructName);
|
---|
263 | }
|
---|
264 | RTStrmPrintf(pOut,
|
---|
265 | "} RTNTSDBSET;\n"
|
---|
266 | "typedef RTNTSDBSET const *PCRTNTSDBSET;\n"
|
---|
267 | "\n");
|
---|
268 |
|
---|
269 | /*
|
---|
270 | * Output the data.
|
---|
271 | */
|
---|
272 | RTStrmPrintf(pOut,
|
---|
273 | "\n"
|
---|
274 | "#ifndef RTNTSDB_NO_DATA\n"
|
---|
275 | "const RTNTSDBSET g_artNtSdbSets[] = \n"
|
---|
276 | "{\n");
|
---|
277 | PMYSET pSet;
|
---|
278 | RTListForEach(&g_SetList, pSet, MYSET, ListEntry)
|
---|
279 | {
|
---|
280 | const char *pszArch = pSet->enmArch == MYARCH_AMD64 ? "AMD64" : "X86";
|
---|
281 | RTStrmPrintf(pOut,
|
---|
282 | "# ifdef RT_ARCH_%s\n"
|
---|
283 | " { /* Source: %s */\n"
|
---|
284 | " /*.OsVerInfo = */\n"
|
---|
285 | " {\n"
|
---|
286 | " /* .uMajorVer = */ %u,\n"
|
---|
287 | " /* .uMinorVer = */ %u,\n"
|
---|
288 | " /* .fChecked = */ %s,\n"
|
---|
289 | " /* .fSmp = */ %s,\n"
|
---|
290 | " /* .uCsdNo = */ %u,\n"
|
---|
291 | " /* .uBuildNo = */ %u,\n"
|
---|
292 | " },\n",
|
---|
293 | pszArch,
|
---|
294 | pSet->pszPdb,
|
---|
295 | pSet->OsVerInfo.uMajorVer,
|
---|
296 | pSet->OsVerInfo.uMinorVer,
|
---|
297 | pSet->OsVerInfo.fChecked ? "true" : "false",
|
---|
298 | pSet->OsVerInfo.fSmp ? "true" : "false",
|
---|
299 | pSet->OsVerInfo.uCsdNo,
|
---|
300 | pSet->OsVerInfo.uBuildNo);
|
---|
301 | for (uint32_t i = 0; i < RT_ELEMENTS(pSet->aStructs); i++)
|
---|
302 | {
|
---|
303 | const char *pszStructName = figureCStructName(&pSet->aStructs[i]);
|
---|
304 | RTStrmPrintf(pOut,
|
---|
305 | " /* .%s = */\n"
|
---|
306 | " {\n", pszStructName);
|
---|
307 | PMYMEMBER paMembers = pSet->aStructs[i].paMembers;
|
---|
308 | for (uint32_t j = 0; j < pSet->aStructs[i].cMembers; j++)
|
---|
309 | {
|
---|
310 | const char *pszMemName = figureCMemberName(&paMembers[j]);
|
---|
311 | RTStrmPrintf(pOut,
|
---|
312 | " /* .off%-25s = */ %#06x,\n"
|
---|
313 | " /* .cb%-26s = */ %#06x,\n",
|
---|
314 | pszMemName, paMembers[j].off,
|
---|
315 | pszMemName, paMembers[j].cb);
|
---|
316 | }
|
---|
317 | RTStrmPrintf(pOut,
|
---|
318 | " },\n");
|
---|
319 | }
|
---|
320 | RTStrmPrintf(pOut,
|
---|
321 | " },\n"
|
---|
322 | "# endif\n"
|
---|
323 | );
|
---|
324 | }
|
---|
325 |
|
---|
326 | RTStrmPrintf(pOut,
|
---|
327 | "};\n"
|
---|
328 | "#endif /* !RTNTSDB_NO_DATA */\n"
|
---|
329 | "\n");
|
---|
330 |
|
---|
331 | RTStrmPrintf(pOut, "\n#endif\n\n");
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Creates a MYSET with copies of all the data and inserts it into the
|
---|
337 | * g_SetList in a orderly fashion.
|
---|
338 | *
|
---|
339 | * @returns Fully complained exit code.
|
---|
340 | * @param pOsVerInfo The OS version info.
|
---|
341 | * @param enmArch The NT architecture of the incoming PDB.
|
---|
342 | * @param pszPdb The PDB file name.
|
---|
343 | */
|
---|
344 | static RTEXITCODE saveStructures(PRTNTSDBOSVER pOsVerInfo, MYARCH enmArch, const char *pszPdb)
|
---|
345 | {
|
---|
346 | /*
|
---|
347 | * Allocate one big chunk, figure it's size once.
|
---|
348 | */
|
---|
349 | static size_t s_cbNeeded = 0;
|
---|
350 | if (s_cbNeeded == 0)
|
---|
351 | {
|
---|
352 | s_cbNeeded = RT_OFFSETOF(MYSET, aStructs[RT_ELEMENTS(g_aStructs)]);
|
---|
353 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aStructs); i++)
|
---|
354 | s_cbNeeded += sizeof(MYMEMBER) * g_aStructs[i].cMembers;
|
---|
355 | }
|
---|
356 |
|
---|
357 | size_t cbPdb = strlen(pszPdb) + 1;
|
---|
358 | PMYSET pSet = (PMYSET)RTMemAlloc(s_cbNeeded + cbPdb);
|
---|
359 | if (!pSet)
|
---|
360 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Out of memory!\n");
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * Copy over the data.
|
---|
364 | */
|
---|
365 | pSet->enmArch = enmArch;
|
---|
366 | memcpy(&pSet->OsVerInfo, pOsVerInfo, sizeof(pSet->OsVerInfo));
|
---|
367 | memcpy(&pSet->aStructs[0], g_aStructs, sizeof(g_aStructs));
|
---|
368 |
|
---|
369 | PMYMEMBER pDst = (PMYMEMBER)&pSet->aStructs[RT_ELEMENTS(g_aStructs)];
|
---|
370 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aStructs); i++)
|
---|
371 | {
|
---|
372 | pSet->aStructs[i].paMembers = pDst;
|
---|
373 | memcpy(pDst, g_aStructs[i].paMembers, g_aStructs[i].cMembers * sizeof(*pDst));
|
---|
374 | pDst += g_aStructs[i].cMembers;
|
---|
375 | }
|
---|
376 |
|
---|
377 | pSet->pszPdb = (char *)pDst;
|
---|
378 | memcpy(pDst, pszPdb, cbPdb);
|
---|
379 |
|
---|
380 | /*
|
---|
381 | * Link it.
|
---|
382 | */
|
---|
383 | PMYSET pInsertBefore;
|
---|
384 | RTListForEach(&g_SetList, pInsertBefore, MYSET, ListEntry)
|
---|
385 | {
|
---|
386 | int iDiff = rtNtOsVerInfoCompare(&pInsertBefore->OsVerInfo, &pSet->OsVerInfo);
|
---|
387 | if (iDiff >= 0)
|
---|
388 | {
|
---|
389 | if (iDiff > 0 || pInsertBefore->enmArch > pSet->enmArch)
|
---|
390 | {
|
---|
391 | RTListNodeInsertBefore(&pInsertBefore->ListEntry, &pSet->ListEntry);
|
---|
392 | return RTEXITCODE_SUCCESS;
|
---|
393 | }
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | RTListAppend(&g_SetList, &pSet->ListEntry);
|
---|
398 | return RTEXITCODE_SUCCESS;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Checks that we found everything.
|
---|
404 | *
|
---|
405 | * @returns Fully complained exit code.
|
---|
406 | */
|
---|
407 | static RTEXITCODE checkThatWeFoundEverything(void)
|
---|
408 | {
|
---|
409 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
410 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aStructs); i++)
|
---|
411 | {
|
---|
412 | PMYMEMBER paMembers = g_aStructs[i].paMembers;
|
---|
413 | uint32_t j = g_aStructs[i].cMembers;
|
---|
414 | while (j-- > 0)
|
---|
415 | {
|
---|
416 | if (paMembers[j].off == UINT32_MAX)
|
---|
417 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, " Missing %s::%s\n", g_aStructs[i].pszName, paMembers[j].pszName);
|
---|
418 | }
|
---|
419 | }
|
---|
420 | return rcExit;
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Matches the member against what we're looking for.
|
---|
426 | *
|
---|
427 | * @returns Number of hits.
|
---|
428 | * @param cWantedMembers The number members in paWantedMembers.
|
---|
429 | * @param paWantedMembers The members we're looking for.
|
---|
430 | * @param pszPrefix The member name prefix.
|
---|
431 | * @param pszMember The member name.
|
---|
432 | * @param offMember The member offset.
|
---|
433 | * @param cbMember The member size.
|
---|
434 | */
|
---|
435 | static uint32_t matchUpStructMembers(unsigned cWantedMembers, PMYMEMBER paWantedMembers,
|
---|
436 | const char *pszPrefix, const char *pszMember,
|
---|
437 | uint32_t offMember, uint32_t cbMember)
|
---|
438 | {
|
---|
439 | size_t cchPrefix = strlen(pszPrefix);
|
---|
440 | uint32_t cHits = 0;
|
---|
441 | uint32_t iMember = cWantedMembers;
|
---|
442 | while (iMember-- > 0)
|
---|
443 | {
|
---|
444 | if ( !strncmp(pszPrefix, paWantedMembers[iMember].pszName, cchPrefix)
|
---|
445 | && !strcmp(pszMember, paWantedMembers[iMember].pszName + cchPrefix))
|
---|
446 | {
|
---|
447 | paWantedMembers[iMember].off = offMember;
|
---|
448 | paWantedMembers[iMember].cb = cbMember;
|
---|
449 | cHits++;
|
---|
450 | }
|
---|
451 | else if (paWantedMembers[iMember].pszzAltNames)
|
---|
452 | {
|
---|
453 | char const *pszCur = paWantedMembers[iMember].pszzAltNames;
|
---|
454 | while (*pszCur)
|
---|
455 | {
|
---|
456 | size_t cchCur = strlen(pszCur);
|
---|
457 | if ( !strncmp(pszPrefix, pszCur, cchPrefix)
|
---|
458 | && !strcmp(pszMember, pszCur + cchPrefix))
|
---|
459 | {
|
---|
460 | paWantedMembers[iMember].off = offMember;
|
---|
461 | paWantedMembers[iMember].cb = cbMember;
|
---|
462 | cHits++;
|
---|
463 | break;
|
---|
464 | }
|
---|
465 | pszCur += cchCur + 1;
|
---|
466 | }
|
---|
467 | }
|
---|
468 | }
|
---|
469 | return cHits;
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Resets the writable structure members prior to processing a PDB.
|
---|
475 | *
|
---|
476 | * While processing the PDB, will fill in the sizes and offsets of what we find.
|
---|
477 | * Afterwards we'll use look for reset values to see that every structure and
|
---|
478 | * member was located successfully.
|
---|
479 | */
|
---|
480 | static void resetMyStructs(void)
|
---|
481 | {
|
---|
482 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aStructs); i++)
|
---|
483 | {
|
---|
484 | PMYMEMBER paMembers = g_aStructs[i].paMembers;
|
---|
485 | uint32_t j = g_aStructs[i].cMembers;
|
---|
486 | while (j-- > 0)
|
---|
487 | {
|
---|
488 | paMembers[j].off = UINT32_MAX;
|
---|
489 | paMembers[j].cb = UINT32_MAX;
|
---|
490 | }
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 | /**
|
---|
496 | * Find members in the specified structure type (@a idxType).
|
---|
497 | *
|
---|
498 | * @returns Fully bitched exit code.
|
---|
499 | * @param hFake Fake process handle.
|
---|
500 | * @param uModAddr The module address.
|
---|
501 | * @param idxType The type index of the structure which members we're
|
---|
502 | * going to process.
|
---|
503 | * @param cWantedMembers The number of wanted members.
|
---|
504 | * @param paWantedMembers The wanted members. This will be modified.
|
---|
505 | * @param offDisp Displacement when calculating member offsets.
|
---|
506 | * @param pszStructNm The top level structure name.
|
---|
507 | * @param pszPrefix The member name prefix.
|
---|
508 | * @param pszLogTag The log tag.
|
---|
509 | */
|
---|
510 | static RTEXITCODE findMembers(HANDLE hFake, uint64_t uModAddr, uint32_t idxType,
|
---|
511 | uint32_t cWantedMembers, PMYMEMBER paWantedMembers,
|
---|
512 | uint32_t offDisp, const char *pszStructNm, const char *pszPrefix, const char *pszLogTag)
|
---|
513 | {
|
---|
514 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
515 |
|
---|
516 | DWORD cChildren = 0;
|
---|
517 | if (!SymGetTypeInfo(hFake, uModAddr, idxType, TI_GET_CHILDRENCOUNT, &cChildren))
|
---|
518 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: TI_GET_CHILDRENCOUNT failed on _KPRCB: %u\n", pszLogTag, GetLastError());
|
---|
519 |
|
---|
520 | MyDbgPrintf(" %s: cChildren=%u (%#x)\n", pszStructNm, cChildren);
|
---|
521 | TI_FINDCHILDREN_PARAMS *pChildren;
|
---|
522 | pChildren = (TI_FINDCHILDREN_PARAMS *)alloca(RT_OFFSETOF(TI_FINDCHILDREN_PARAMS, ChildId[cChildren]));
|
---|
523 | pChildren->Start = 0;
|
---|
524 | pChildren->Count = cChildren;
|
---|
525 | if (!SymGetTypeInfo(hFake, uModAddr, idxType, TI_FINDCHILDREN, pChildren))
|
---|
526 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: TI_FINDCHILDREN failed on _KPRCB: %u\n", pszLogTag, GetLastError());
|
---|
527 |
|
---|
528 | for (uint32_t i = 0; i < cChildren; i++)
|
---|
529 | {
|
---|
530 | //MyDbgPrintf(" %s: child#%u: TypeIndex=%u\n", pszStructNm, i, pChildren->ChildId[i]);
|
---|
531 | IMAGEHLP_SYMBOL_TYPE_INFO enmErr;
|
---|
532 | PWCHAR pwszMember = NULL;
|
---|
533 | uint32_t idxRefType = 0;
|
---|
534 | uint32_t offMember = 0;
|
---|
535 | uint64_t cbMember = 0;
|
---|
536 | uint32_t cMemberChildren = 0;
|
---|
537 | if ( SymGetTypeInfo(hFake, uModAddr, pChildren->ChildId[i], enmErr = TI_GET_SYMNAME, &pwszMember)
|
---|
538 | && SymGetTypeInfo(hFake, uModAddr, pChildren->ChildId[i], enmErr = TI_GET_OFFSET, &offMember)
|
---|
539 | && SymGetTypeInfo(hFake, uModAddr, pChildren->ChildId[i], enmErr = TI_GET_TYPE, &idxRefType)
|
---|
540 | && SymGetTypeInfo(hFake, uModAddr, idxRefType, enmErr = TI_GET_LENGTH, &cbMember)
|
---|
541 | && SymGetTypeInfo(hFake, uModAddr, idxRefType, enmErr = TI_GET_CHILDRENCOUNT, &cMemberChildren)
|
---|
542 | )
|
---|
543 | {
|
---|
544 | offMember += offDisp;
|
---|
545 |
|
---|
546 | char *pszMember;
|
---|
547 | int rc = RTUtf16ToUtf8(pwszMember, &pszMember);
|
---|
548 | if (RT_SUCCESS(rc))
|
---|
549 | {
|
---|
550 | matchUpStructMembers(cWantedMembers, paWantedMembers, pszPrefix, pszMember, offMember, cbMember);
|
---|
551 |
|
---|
552 | /*
|
---|
553 | * Gather more info and do some debug printing. We'll use some
|
---|
554 | * of this info below when recursing into sub-structures
|
---|
555 | * and arrays.
|
---|
556 | */
|
---|
557 | uint32_t fNested = 0; SymGetTypeInfo(hFake, uModAddr, idxRefType, TI_GET_NESTED, &fNested);
|
---|
558 | uint32_t uDataKind = 0; SymGetTypeInfo(hFake, uModAddr, idxRefType, TI_GET_DATAKIND, &uDataKind);
|
---|
559 | uint32_t uBaseType = 0; SymGetTypeInfo(hFake, uModAddr, idxRefType, TI_GET_BASETYPE, &uBaseType);
|
---|
560 | uint32_t uMembTag = 0; SymGetTypeInfo(hFake, uModAddr, pChildren->ChildId[i], TI_GET_SYMTAG, &uMembTag);
|
---|
561 | uint32_t uBaseTag = 0; SymGetTypeInfo(hFake, uModAddr, idxRefType, TI_GET_SYMTAG, &uBaseTag);
|
---|
562 | uint32_t cElements = 0; SymGetTypeInfo(hFake, uModAddr, idxRefType, TI_GET_COUNT, &cElements);
|
---|
563 | uint32_t idxArrayType = 0; SymGetTypeInfo(hFake, uModAddr, idxRefType, TI_GET_ARRAYINDEXTYPEID, &idxArrayType);
|
---|
564 | MyDbgPrintf(" %#06x LB %#06llx %c%c %2d %2d %2d %2d %2d %4d %s::%s%s\n",
|
---|
565 | offMember, cbMember,
|
---|
566 | cMemberChildren > 0 ? 'c' : '-',
|
---|
567 | fNested != 0 ? 'n' : '-',
|
---|
568 | uDataKind,
|
---|
569 | uBaseType,
|
---|
570 | uMembTag,
|
---|
571 | uBaseTag,
|
---|
572 | cElements,
|
---|
573 | idxArrayType,
|
---|
574 | pszStructNm,
|
---|
575 | pszPrefix,
|
---|
576 | pszMember);
|
---|
577 |
|
---|
578 | /*
|
---|
579 | * Recurse into children.
|
---|
580 | */
|
---|
581 | if (cMemberChildren > 0)
|
---|
582 | {
|
---|
583 | size_t cbNeeded = strlen(pszMember) + strlen(pszPrefix) + sizeof(".");
|
---|
584 | char *pszSubPrefix = (char *)RTMemTmpAlloc(cbNeeded);
|
---|
585 | if (pszSubPrefix)
|
---|
586 | {
|
---|
587 | strcat(strcat(strcpy(pszSubPrefix, pszPrefix), pszMember), ".");
|
---|
588 | RTEXITCODE rcExit2 = findMembers(hFake, uModAddr, idxRefType, cWantedMembers,
|
---|
589 | paWantedMembers, offMember,
|
---|
590 | pszStructNm,
|
---|
591 | pszSubPrefix,
|
---|
592 | pszLogTag);
|
---|
593 | if (rcExit2 != RTEXITCODE_SUCCESS)
|
---|
594 | rcExit = rcExit2;
|
---|
595 | RTMemTmpFree(pszSubPrefix);
|
---|
596 | }
|
---|
597 | else
|
---|
598 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "out of memory\n");
|
---|
599 | }
|
---|
600 | /*
|
---|
601 | * Recurse into arrays too.
|
---|
602 | */
|
---|
603 | else if (cElements > 0 && idxArrayType > 0)
|
---|
604 | {
|
---|
605 | BOOL fRc;
|
---|
606 | uint32_t idxElementRefType = 0;
|
---|
607 | fRc = SymGetTypeInfo(hFake, uModAddr, idxRefType, TI_GET_TYPE, &idxElementRefType); Assert(fRc);
|
---|
608 | uint64_t cbElement = cbMember / cElements;
|
---|
609 | fRc = SymGetTypeInfo(hFake, uModAddr, idxElementRefType, TI_GET_LENGTH, &cbElement); Assert(fRc);
|
---|
610 | MyDbgPrintf("idxArrayType=%u idxElementRefType=%u cbElement=%u\n", idxArrayType, idxElementRefType, cbElement);
|
---|
611 |
|
---|
612 | size_t cbNeeded = strlen(pszMember) + strlen(pszPrefix) + sizeof("[xxxxxxxxxxxxxxxx].");
|
---|
613 | char *pszSubPrefix = (char *)RTMemTmpAlloc(cbNeeded);
|
---|
614 | if (pszSubPrefix)
|
---|
615 | {
|
---|
616 | for (uint32_t iElement = 0; iElement < cElements; iElement++)
|
---|
617 | {
|
---|
618 | RTStrPrintf(pszSubPrefix, cbNeeded, "%s%s[%u].", pszPrefix, pszMember, iElement);
|
---|
619 | RTEXITCODE rcExit2 = findMembers(hFake, uModAddr, idxElementRefType, cWantedMembers,
|
---|
620 | paWantedMembers,
|
---|
621 | offMember + iElement * cbElement,
|
---|
622 | pszStructNm,
|
---|
623 | pszSubPrefix,
|
---|
624 | pszLogTag);
|
---|
625 | if (rcExit2 != RTEXITCODE_SUCCESS)
|
---|
626 | rcExit = rcExit2;
|
---|
627 | }
|
---|
628 | RTMemTmpFree(pszSubPrefix);
|
---|
629 | }
|
---|
630 | else
|
---|
631 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "out of memory\n");
|
---|
632 | }
|
---|
633 |
|
---|
634 | RTStrFree(pszMember);
|
---|
635 | }
|
---|
636 | else
|
---|
637 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: RTUtf16ToUtf8 failed on %s child#%u: %Rrc\n",
|
---|
638 | pszLogTag, pszStructNm, i, rc);
|
---|
639 | }
|
---|
640 | /* TI_GET_OFFSET fails on bitfields, so just ignore+skip those. */
|
---|
641 | else if (enmErr != TI_GET_OFFSET || GetLastError() != ERROR_INVALID_FUNCTION)
|
---|
642 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: SymGetTypeInfo(,,,%d,) failed on %s child#%u: %u\n",
|
---|
643 | pszLogTag, enmErr, pszStructNm, i, GetLastError());
|
---|
644 | LocalFree(pwszMember);
|
---|
645 | } /* For each child. */
|
---|
646 |
|
---|
647 | return rcExit;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Lookup up structures and members in the given module.
|
---|
653 | *
|
---|
654 | * @returns Fully bitched exit code.
|
---|
655 | * @param hFake Fake process handle.
|
---|
656 | * @param uModAddr The module address.
|
---|
657 | * @param pszLogTag The log tag.
|
---|
658 | * @param pszPdb The full PDB path.
|
---|
659 | * @param pOsVerInfo The OS version info for altering the error handling
|
---|
660 | * for older OSes.
|
---|
661 | */
|
---|
662 | static RTEXITCODE findStructures(HANDLE hFake, uint64_t uModAddr, const char *pszLogTag, const char *pszPdb,
|
---|
663 | PCRTNTSDBOSVER pOsVerInfo)
|
---|
664 | {
|
---|
665 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
666 | PSYMBOL_INFO pSymInfo = (PSYMBOL_INFO)alloca(sizeof(*pSymInfo));
|
---|
667 | for (uint32_t iStruct = 0; iStruct < RT_ELEMENTS(g_aStructs); iStruct++)
|
---|
668 | {
|
---|
669 | pSymInfo->SizeOfStruct = sizeof(*pSymInfo);
|
---|
670 | pSymInfo->MaxNameLen = 0;
|
---|
671 | if (!SymGetTypeFromName(hFake, uModAddr, g_aStructs[iStruct].pszName, pSymInfo))
|
---|
672 | {
|
---|
673 | if (!(pOsVerInfo->uMajorVer == 5 && pOsVerInfo->uMinorVer == 0) /* w2k */)
|
---|
674 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Failed to find _KPRCB: %u\n", pszPdb, GetLastError());
|
---|
675 | RTMsgInfo("%s: Skipping - failed to find _KPRCB: %u\n", pszPdb, GetLastError());
|
---|
676 | return RTEXITCODE_SKIPPED;
|
---|
677 | }
|
---|
678 |
|
---|
679 | MyDbgPrintf(" %s: TypeIndex=%u\n", g_aStructs[iStruct].pszName, pSymInfo->TypeIndex);
|
---|
680 | MyDbgPrintf(" %s: Size=%u (%#x)\n", g_aStructs[iStruct].pszName, pSymInfo->Size, pSymInfo->Size);
|
---|
681 |
|
---|
682 | rcExit = findMembers(hFake, uModAddr, pSymInfo->TypeIndex,
|
---|
683 | g_aStructs[iStruct].cMembers, g_aStructs[iStruct].paMembers, 0 /* offDisp */,
|
---|
684 | g_aStructs[iStruct].pszName, "", pszLogTag);
|
---|
685 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
686 | return rcExit;
|
---|
687 | } /* for each struct we want */
|
---|
688 | return rcExit;
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | static bool strIEndsWith(const char *pszString, const char *pszSuffix)
|
---|
693 | {
|
---|
694 | size_t cchString = strlen(pszString);
|
---|
695 | size_t cchSuffix = strlen(pszSuffix);
|
---|
696 | if (cchString < cchSuffix)
|
---|
697 | return false;
|
---|
698 | return RTStrICmp(pszString + cchString - cchSuffix, pszSuffix) == 0;
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Use various hysterics to figure out the OS version details from the PDB path.
|
---|
704 | *
|
---|
705 | * This ASSUMES quite a bunch of things:
|
---|
706 | * -# Working on unpacked symbol packages. This does not work for
|
---|
707 | * windbg symbol stores/caches.
|
---|
708 | * -# The symbol package has been unpacked into a directory with the same
|
---|
709 | * name as the symbol package (sans suffixes).
|
---|
710 | *
|
---|
711 | * @returns Fully complained exit code.
|
---|
712 | * @param pszPdb The path to the PDB.
|
---|
713 | * @param pVerInfo Where to return the version info.
|
---|
714 | * @param penmArch Where to return the architecture.
|
---|
715 | */
|
---|
716 | static RTEXITCODE FigurePdbVersionInfo(const char *pszPdb, PRTNTSDBOSVER pVerInfo, MYARCH *penmArch)
|
---|
717 | {
|
---|
718 | /*
|
---|
719 | * Split the path.
|
---|
720 | */
|
---|
721 | union
|
---|
722 | {
|
---|
723 | RTPATHSPLIT Split;
|
---|
724 | uint8_t abPad[RTPATH_MAX + 1024];
|
---|
725 | } u;
|
---|
726 | int rc = RTPathSplit(pszPdb, &u.Split, sizeof(u), 0);
|
---|
727 | if (RT_FAILURE(rc))
|
---|
728 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathSplit failed on '%s': %Rrc", pszPdb, rc);
|
---|
729 | if (!(u.Split.fProps & RTPATH_PROP_FILENAME))
|
---|
730 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPATH_PROP_FILENAME not set for: '%s'", pszPdb);
|
---|
731 | const char *pszFilename = u.Split.apszComps[u.Split.cComps - 1];
|
---|
732 |
|
---|
733 | /*
|
---|
734 | * SMP or UNI kernel?
|
---|
735 | */
|
---|
736 | if ( !RTStrICmp(pszFilename, "ntkrnlmp.pdb")
|
---|
737 | || !RTStrICmp(pszFilename, "ntkrpamp.pdb")
|
---|
738 | )
|
---|
739 | pVerInfo->fSmp = true;
|
---|
740 | else if ( !RTStrICmp(pszFilename, "ntoskrnl.pdb")
|
---|
741 | || !RTStrICmp(pszFilename, "ntkrnlpa.pdb")
|
---|
742 | )
|
---|
743 | pVerInfo->fSmp = false;
|
---|
744 | else
|
---|
745 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Doesn't recognize the filename '%s'...", pszFilename);
|
---|
746 |
|
---|
747 | /*
|
---|
748 | * Look for symbol pack names in the path. This is stuff like:
|
---|
749 | * - WindowsVista.6002.090410-1830.x86fre
|
---|
750 | * - WindowsVista.6002.090410-1830.amd64chk
|
---|
751 | * - Windows_Win7.7600.16385.090713-1255.X64CHK
|
---|
752 | * - Windows_Win7SP1.7601.17514.101119-1850.AMD64FRE
|
---|
753 | * - Windows_Win8.9200.16384.120725-1247.X86CHK
|
---|
754 | * - en_windows_8_1_symbols_debug_checked_x64_2712568
|
---|
755 | */
|
---|
756 | bool fFound = false;
|
---|
757 | uint32_t i = u.Split.cComps - 1;
|
---|
758 | while (i-- > 0)
|
---|
759 | {
|
---|
760 | static struct
|
---|
761 | {
|
---|
762 | const char *pszPrefix;
|
---|
763 | size_t cchPrefix;
|
---|
764 | uint8_t uMajorVer;
|
---|
765 | uint8_t uMinorVer;
|
---|
766 | uint8_t uCsdNo;
|
---|
767 | uint32_t uBuildNo; /**< UINT32_MAX means the number immediately after the prefix. */
|
---|
768 | } const s_aSymPacks[] =
|
---|
769 | {
|
---|
770 | { RT_STR_TUPLE("w2kSP1SYM"), 5, 0, 1, 2195 },
|
---|
771 | { RT_STR_TUPLE("w2ksp2srp1"), 5, 0, 2, 2195 },
|
---|
772 | { RT_STR_TUPLE("w2ksp2sym"), 5, 0, 2, 2195 },
|
---|
773 | { RT_STR_TUPLE("w2ksp3sym"), 5, 0, 3, 2195 },
|
---|
774 | { RT_STR_TUPLE("w2ksp4sym"), 5, 0, 4, 2195 },
|
---|
775 | { RT_STR_TUPLE("Windows2000-KB891861"), 5, 0, 4, 2195 },
|
---|
776 | { RT_STR_TUPLE("windowsxp"), 5, 1, 0, 2600 },
|
---|
777 | { RT_STR_TUPLE("xpsp1sym"), 5, 1, 1, 2600 },
|
---|
778 | { RT_STR_TUPLE("WindowsXP-KB835935-SP2-"), 5, 1, 2, 2600 },
|
---|
779 | { RT_STR_TUPLE("WindowsXP-KB936929-SP3-"), 5, 1, 3, 2600 },
|
---|
780 | { RT_STR_TUPLE("Windows2003."), 5, 2, 0, 3790 },
|
---|
781 | { RT_STR_TUPLE("Windows2003_sp1."), 5, 2, 1, 3790 },
|
---|
782 | { RT_STR_TUPLE("WindowsServer2003-KB933548-v1"), 5, 2, 1, 3790 },
|
---|
783 | { RT_STR_TUPLE("WindowsVista.6000."), 6, 0, 0, 6000 },
|
---|
784 | { RT_STR_TUPLE("Windows_Longhorn.6001."), 6, 0, 1, 6001 }, /* incl w2k8 */
|
---|
785 | { RT_STR_TUPLE("WindowsVista.6002."), 6, 0, 2, 6002 }, /* incl w2k8 */
|
---|
786 | { RT_STR_TUPLE("Windows_Winmain.7000"), 6, 1, 0, 7000 }, /* Beta */
|
---|
787 | { RT_STR_TUPLE("Windows_Winmain.7100"), 6, 1, 0, 7100 }, /* RC */
|
---|
788 | { RT_STR_TUPLE("Windows_Win7.7600"), 6, 1, 0, 7600 }, /* RC */
|
---|
789 | { RT_STR_TUPLE("Windows_Win7SP1.7601"), 6, 1, 1, 7601 }, /* RC */
|
---|
790 | { RT_STR_TUPLE("Windows_Winmain.8102"), 6, 2, 0, 8102 }, /* preview */
|
---|
791 | { RT_STR_TUPLE("Windows_Winmain.8250"), 6, 2, 0, 8250 }, /* beta */
|
---|
792 | { RT_STR_TUPLE("Windows_Winmain.8400"), 6, 2, 0, 8400 }, /* RC */
|
---|
793 | { RT_STR_TUPLE("Windows_Win8.9200"), 6, 2, 0, 9200 }, /* RTM */
|
---|
794 | { RT_STR_TUPLE("en_windows_8_1"), 6, 3, 0, 9600 }, /* RTM */
|
---|
795 | { RT_STR_TUPLE("en_windows_10_symbols_"), 10, 0, 0,10240 }, /* RTM */
|
---|
796 | };
|
---|
797 |
|
---|
798 | const char *pszComp = u.Split.apszComps[i];
|
---|
799 | uint32_t iSymPack = RT_ELEMENTS(s_aSymPacks);
|
---|
800 | while (iSymPack-- > 0)
|
---|
801 | if (!RTStrNICmp(pszComp, s_aSymPacks[iSymPack].pszPrefix, s_aSymPacks[iSymPack].cchPrefix))
|
---|
802 | break;
|
---|
803 | if (iSymPack >= RT_ELEMENTS(s_aSymPacks))
|
---|
804 | continue;
|
---|
805 |
|
---|
806 | pVerInfo->uMajorVer = s_aSymPacks[iSymPack].uMajorVer;
|
---|
807 | pVerInfo->uMinorVer = s_aSymPacks[iSymPack].uMinorVer;
|
---|
808 | pVerInfo->uCsdNo = s_aSymPacks[iSymPack].uCsdNo;
|
---|
809 | pVerInfo->fChecked = false;
|
---|
810 | pVerInfo->uBuildNo = s_aSymPacks[iSymPack].uBuildNo;
|
---|
811 |
|
---|
812 | /* Parse build number if necessary. */
|
---|
813 | if (s_aSymPacks[iSymPack].uBuildNo == UINT32_MAX)
|
---|
814 | {
|
---|
815 | char *pszNext;
|
---|
816 | rc = RTStrToUInt32Ex(pszComp + s_aSymPacks[iSymPack].cchPrefix, &pszNext, 10, &pVerInfo->uBuildNo);
|
---|
817 | if (RT_FAILURE(rc))
|
---|
818 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to decode build number in '%s': %Rrc", pszComp, rc);
|
---|
819 | if (*pszNext != '.' && *pszNext != '_' && *pszNext != '-')
|
---|
820 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to decode build number in '%s': '%c'", pszComp, *pszNext);
|
---|
821 | }
|
---|
822 |
|
---|
823 | /* Look for build arch and checked/free. */
|
---|
824 | if ( RTStrIStr(pszComp, ".x86.chk.")
|
---|
825 | || RTStrIStr(pszComp, ".x86chk.")
|
---|
826 | || RTStrIStr(pszComp, "_x86_chk_")
|
---|
827 | || RTStrIStr(pszComp, "_x86chk_")
|
---|
828 | || RTStrIStr(pszComp, "-x86-DEBUG")
|
---|
829 | || (RTStrIStr(pszComp, "-x86-") && RTStrIStr(pszComp, "-DEBUG"))
|
---|
830 | || RTStrIStr(pszComp, "_debug_checked_x86")
|
---|
831 | )
|
---|
832 | {
|
---|
833 | pVerInfo->fChecked = true;
|
---|
834 | *penmArch = MYARCH_X86;
|
---|
835 | }
|
---|
836 | else if ( RTStrIStr(pszComp, ".amd64.chk.")
|
---|
837 | || RTStrIStr(pszComp, ".amd64chk.")
|
---|
838 | || RTStrIStr(pszComp, ".x64.chk.")
|
---|
839 | || RTStrIStr(pszComp, ".x64chk.")
|
---|
840 | || RTStrIStr(pszComp, "_debug_checked_x64")
|
---|
841 | )
|
---|
842 | {
|
---|
843 | pVerInfo->fChecked = true;
|
---|
844 | *penmArch = MYARCH_AMD64;
|
---|
845 | }
|
---|
846 | else if ( RTStrIStr(pszComp, ".amd64.fre.")
|
---|
847 | || RTStrIStr(pszComp, ".amd64fre.")
|
---|
848 | || RTStrIStr(pszComp, ".x64.fre.")
|
---|
849 | || RTStrIStr(pszComp, ".x64fre.")
|
---|
850 | )
|
---|
851 | {
|
---|
852 | pVerInfo->fChecked = false;
|
---|
853 | *penmArch = MYARCH_AMD64;
|
---|
854 | }
|
---|
855 | else if ( RTStrIStr(pszComp, "DEBUG")
|
---|
856 | || RTStrIStr(pszComp, "_chk")
|
---|
857 | )
|
---|
858 | {
|
---|
859 | pVerInfo->fChecked = true;
|
---|
860 | *penmArch = MYARCH_X86;
|
---|
861 | }
|
---|
862 | else if (RTStrIStr(pszComp, "_x64"))
|
---|
863 | {
|
---|
864 | pVerInfo->fChecked = false;
|
---|
865 | *penmArch = MYARCH_AMD64;
|
---|
866 | }
|
---|
867 | else
|
---|
868 | {
|
---|
869 | pVerInfo->fChecked = false;
|
---|
870 | *penmArch = MYARCH_X86;
|
---|
871 | }
|
---|
872 | return RTEXITCODE_SUCCESS;
|
---|
873 | }
|
---|
874 |
|
---|
875 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Giving up on '%s'...\n", pszPdb);
|
---|
876 | }
|
---|
877 |
|
---|
878 |
|
---|
879 | /**
|
---|
880 | * Process one PDB.
|
---|
881 | *
|
---|
882 | * @returns Fully bitched exit code.
|
---|
883 | * @param pszPdb The path to the PDB.
|
---|
884 | */
|
---|
885 | static RTEXITCODE processPdb(const char *pszPdb)
|
---|
886 | {
|
---|
887 | /*
|
---|
888 | * We need the size later on, so get that now and present proper IPRT error
|
---|
889 | * info if the file is missing or inaccessible.
|
---|
890 | */
|
---|
891 | RTFSOBJINFO ObjInfo;
|
---|
892 | int rc = RTPathQueryInfoEx(pszPdb, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
|
---|
893 | if (RT_FAILURE(rc))
|
---|
894 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathQueryInfo fail on '%s': %Rrc\n", pszPdb, rc);
|
---|
895 |
|
---|
896 | /*
|
---|
897 | * Figure the windows version details for the given PDB.
|
---|
898 | */
|
---|
899 | MYARCH enmArch;
|
---|
900 | RTNTSDBOSVER OsVerInfo;
|
---|
901 | RTEXITCODE rcExit = FigurePdbVersionInfo(pszPdb, &OsVerInfo, &enmArch);
|
---|
902 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
903 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to figure the OS version info for '%s'.\n'", pszPdb);
|
---|
904 |
|
---|
905 | /*
|
---|
906 | * Create a fake handle and open the PDB.
|
---|
907 | */
|
---|
908 | static uintptr_t s_iHandle = 0;
|
---|
909 | HANDLE hFake = (HANDLE)++s_iHandle;
|
---|
910 | if (!SymInitialize(hFake, NULL, FALSE))
|
---|
911 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "SymInitialied failed: %u\n", GetLastError());
|
---|
912 |
|
---|
913 | uint64_t uModAddr = UINT64_C(0x1000000);
|
---|
914 | uModAddr = SymLoadModuleEx(hFake, NULL /*hFile*/, pszPdb, NULL /*pszModuleName*/,
|
---|
915 | uModAddr, ObjInfo.cbObject, NULL /*pData*/, 0 /*fFlags*/);
|
---|
916 | if (uModAddr != 0)
|
---|
917 | {
|
---|
918 | MyDbgPrintf("*** uModAddr=%#llx \"%s\" ***\n", uModAddr, pszPdb);
|
---|
919 |
|
---|
920 | char szLogTag[32];
|
---|
921 | RTStrCopy(szLogTag, sizeof(szLogTag), RTPathFilename(pszPdb));
|
---|
922 |
|
---|
923 | /*
|
---|
924 | * Find the structures.
|
---|
925 | */
|
---|
926 | rcExit = findStructures(hFake, uModAddr, szLogTag, pszPdb, &OsVerInfo);
|
---|
927 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
928 | rcExit = checkThatWeFoundEverything();
|
---|
929 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
930 | {
|
---|
931 | /*
|
---|
932 | * Save the details for later when we produce the header.
|
---|
933 | */
|
---|
934 | rcExit = saveStructures(&OsVerInfo, enmArch, pszPdb);
|
---|
935 | }
|
---|
936 | }
|
---|
937 | else
|
---|
938 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymLoadModuleEx failed: %u\n", GetLastError());
|
---|
939 |
|
---|
940 | if (!SymCleanup(hFake))
|
---|
941 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymCleanup failed: %u\n", GetLastError());
|
---|
942 |
|
---|
943 | if (rcExit == RTEXITCODE_SKIPPED)
|
---|
944 | rcExit = RTEXITCODE_SUCCESS;
|
---|
945 | return rcExit;
|
---|
946 | }
|
---|
947 |
|
---|
948 |
|
---|
949 | /** The size of the directory entry buffer we're using. */
|
---|
950 | #define MY_DIRENTRY_BUF_SIZE (sizeof(RTDIRENTRYEX) + RTPATH_MAX)
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * Checks if the name is of interest to us.
|
---|
954 | *
|
---|
955 | * @returns true/false.
|
---|
956 | * @param pszName The name.
|
---|
957 | * @param cchName The length of the name.
|
---|
958 | */
|
---|
959 | static bool isInterestingName(const char *pszName, size_t cchName)
|
---|
960 | {
|
---|
961 | static struct { const char *psz; size_t cch; } const s_aNames[] =
|
---|
962 | {
|
---|
963 | RT_STR_TUPLE("ntoskrnl.pdb"),
|
---|
964 | RT_STR_TUPLE("ntkrnlmp.pdb"),
|
---|
965 | RT_STR_TUPLE("ntkrnlpa.pdb"),
|
---|
966 | RT_STR_TUPLE("ntkrpamp.pdb"),
|
---|
967 | };
|
---|
968 |
|
---|
969 | if ( cchName == s_aNames[0].cch
|
---|
970 | && (pszName[0] == 'n' || pszName[0] == 'N')
|
---|
971 | && (pszName[1] == 't' || pszName[1] == 'T')
|
---|
972 | )
|
---|
973 | {
|
---|
974 | int i = RT_ELEMENTS(s_aNames);
|
---|
975 | while (i-- > 0)
|
---|
976 | if ( s_aNames[i].cch == cchName
|
---|
977 | && !RTStrICmp(s_aNames[i].psz, pszName))
|
---|
978 | return true;
|
---|
979 | }
|
---|
980 | return false;
|
---|
981 | }
|
---|
982 |
|
---|
983 |
|
---|
984 | /**
|
---|
985 | * Recursively processes relevant files in the specified directory.
|
---|
986 | *
|
---|
987 | * @returns Fully complained exit code.
|
---|
988 | * @param pszDir Pointer to the directory buffer.
|
---|
989 | * @param cchDir The length of pszDir in pszDir.
|
---|
990 | * @param pDirEntry Pointer to the directory buffer.
|
---|
991 | * @param iLogDepth The logging depth.
|
---|
992 | */
|
---|
993 | static RTEXITCODE processDirSub(char *pszDir, size_t cchDir, PRTDIRENTRYEX pDirEntry, int iLogDepth)
|
---|
994 | {
|
---|
995 | Assert(cchDir > 0); Assert(pszDir[cchDir] == '\0');
|
---|
996 |
|
---|
997 | /* Make sure we've got some room in the path, to save us extra work further down. */
|
---|
998 | if (cchDir + 3 >= RTPATH_MAX)
|
---|
999 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Path too long: '%s'\n", pszDir);
|
---|
1000 |
|
---|
1001 | /* Open directory. */
|
---|
1002 | PRTDIR pDir;
|
---|
1003 | int rc = RTDirOpen(&pDir, pszDir);
|
---|
1004 | if (RT_FAILURE(rc))
|
---|
1005 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTDirOpen failed on '%s': %Rrc\n", pszDir, rc);
|
---|
1006 |
|
---|
1007 | /* Ensure we've got a trailing slash (there is space for it see above). */
|
---|
1008 | if (!RTPATH_IS_SEP(pszDir[cchDir - 1]))
|
---|
1009 | {
|
---|
1010 | pszDir[cchDir++] = RTPATH_SLASH;
|
---|
1011 | pszDir[cchDir] = '\0';
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | /*
|
---|
1015 | * Process the files and subdirs.
|
---|
1016 | */
|
---|
1017 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
1018 | for (;;)
|
---|
1019 | {
|
---|
1020 | /* Get the next directory. */
|
---|
1021 | size_t cbDirEntry = MY_DIRENTRY_BUF_SIZE;
|
---|
1022 | rc = RTDirReadEx(pDir, pDirEntry, &cbDirEntry, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
|
---|
1023 | if (RT_FAILURE(rc))
|
---|
1024 | break;
|
---|
1025 |
|
---|
1026 | /* Skip the dot and dot-dot links. */
|
---|
1027 | if ( (pDirEntry->cbName == 1 && pDirEntry->szName[0] == '.')
|
---|
1028 | || (pDirEntry->cbName == 2 && pDirEntry->szName[0] == '.' && pDirEntry->szName[1] == '.'))
|
---|
1029 | continue;
|
---|
1030 |
|
---|
1031 | /* Check length. */
|
---|
1032 | if (pDirEntry->cbName + cchDir + 3 >= RTPATH_MAX)
|
---|
1033 | {
|
---|
1034 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Path too long: '%s' in '%.*s'\n", pDirEntry->szName, cchDir, pszDir);
|
---|
1035 | break;
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | if (RTFS_IS_FILE(pDirEntry->Info.Attr.fMode))
|
---|
1039 | {
|
---|
1040 | /*
|
---|
1041 | * Process debug info files of interest.
|
---|
1042 | */
|
---|
1043 | if (isInterestingName(pDirEntry->szName, pDirEntry->cbName))
|
---|
1044 | {
|
---|
1045 | memcpy(&pszDir[cchDir], pDirEntry->szName, pDirEntry->cbName + 1);
|
---|
1046 | RTEXITCODE rcExit2 = processPdb(pszDir);
|
---|
1047 | if (rcExit2 != RTEXITCODE_SUCCESS)
|
---|
1048 | rcExit = rcExit2;
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 | else if (RTFS_IS_DIRECTORY(pDirEntry->Info.Attr.fMode))
|
---|
1052 | {
|
---|
1053 | /*
|
---|
1054 | * Recurse into the subdirectory. In order to speed up Win7+
|
---|
1055 | * symbol pack traversals, we skip directories with ".pdb" suffixes
|
---|
1056 | * unless they match any of the .pdb files we're looking for.
|
---|
1057 | *
|
---|
1058 | * Note! When we get back pDirEntry will be invalid.
|
---|
1059 | */
|
---|
1060 | if ( pDirEntry->cbName <= 4
|
---|
1061 | || RTStrICmp(&pDirEntry->szName[pDirEntry->cbName - 4], ".pdb")
|
---|
1062 | || isInterestingName(pDirEntry->szName, pDirEntry->cbName))
|
---|
1063 | {
|
---|
1064 | memcpy(&pszDir[cchDir], pDirEntry->szName, pDirEntry->cbName + 1);
|
---|
1065 | if (iLogDepth > 0)
|
---|
1066 | RTMsgInfo("%s%s ...\n", pszDir, RTPATH_SLASH_STR);
|
---|
1067 | RTEXITCODE rcExit2 = processDirSub(pszDir, cchDir + pDirEntry->cbName, pDirEntry, iLogDepth - 1);
|
---|
1068 | if (rcExit2 != RTEXITCODE_SUCCESS)
|
---|
1069 | rcExit = rcExit2;
|
---|
1070 | }
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 | if (rc != VERR_NO_MORE_FILES)
|
---|
1074 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTDirReadEx failed: %Rrc\npszDir=%.*s", rc, cchDir, pszDir);
|
---|
1075 |
|
---|
1076 | rc = RTDirClose(pDir);
|
---|
1077 | if (RT_FAILURE(rc))
|
---|
1078 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTDirClose failed: %Rrc\npszDir=%.*s", rc, cchDir, pszDir);
|
---|
1079 | return rcExit;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 |
|
---|
1083 | /**
|
---|
1084 | * Recursively processes relevant files in the specified directory.
|
---|
1085 | *
|
---|
1086 | * @returns Fully complained exit code.
|
---|
1087 | * @param pszDir The directory to search.
|
---|
1088 | */
|
---|
1089 | static RTEXITCODE processDir(const char *pszDir)
|
---|
1090 | {
|
---|
1091 | char szPath[RTPATH_MAX];
|
---|
1092 | int rc = RTPathAbs(pszDir, szPath, sizeof(szPath));
|
---|
1093 | if (RT_FAILURE(rc))
|
---|
1094 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathAbs failed on '%s': %Rrc\n", pszDir, rc);
|
---|
1095 |
|
---|
1096 | union
|
---|
1097 | {
|
---|
1098 | uint8_t abPadding[MY_DIRENTRY_BUF_SIZE];
|
---|
1099 | RTDIRENTRYEX DirEntry;
|
---|
1100 | } uBuf;
|
---|
1101 | return processDirSub(szPath, strlen(szPath), &uBuf.DirEntry, g_iOptVerbose);
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 |
|
---|
1105 | int main(int argc, char **argv)
|
---|
1106 | {
|
---|
1107 | int rc = RTR3InitExe(argc, &argv, 0 /*fFlags*/);
|
---|
1108 | if (RT_FAILURE(rc))
|
---|
1109 | return RTMsgInitFailure(rc);
|
---|
1110 |
|
---|
1111 | RTListInit(&g_SetList);
|
---|
1112 |
|
---|
1113 | /*
|
---|
1114 | * Parse options.
|
---|
1115 | */
|
---|
1116 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1117 | {
|
---|
1118 | { "--force", 'f', RTGETOPT_REQ_NOTHING },
|
---|
1119 | { "--output", 'o', RTGETOPT_REQ_STRING },
|
---|
1120 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
1121 | { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
|
---|
1122 | };
|
---|
1123 |
|
---|
1124 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
1125 | const char *pszOutput = "-";
|
---|
1126 |
|
---|
1127 | int ch;
|
---|
1128 | RTGETOPTUNION ValueUnion;
|
---|
1129 | RTGETOPTSTATE GetState;
|
---|
1130 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
|
---|
1131 | RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1132 | while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
|
---|
1133 | {
|
---|
1134 | switch (ch)
|
---|
1135 | {
|
---|
1136 | case 'f':
|
---|
1137 | g_fOptForce = true;
|
---|
1138 | break;
|
---|
1139 |
|
---|
1140 | case 'v':
|
---|
1141 | g_iOptVerbose++;
|
---|
1142 | break;
|
---|
1143 |
|
---|
1144 | case 'q':
|
---|
1145 | g_iOptVerbose++;
|
---|
1146 | break;
|
---|
1147 |
|
---|
1148 | case 'o':
|
---|
1149 | pszOutput = ValueUnion.psz;
|
---|
1150 | break;
|
---|
1151 |
|
---|
1152 | case 'V':
|
---|
1153 | RTPrintf("$Revision: 57978 $");
|
---|
1154 | break;
|
---|
1155 |
|
---|
1156 | case 'h':
|
---|
1157 | RTPrintf("usage: %s [-v|--verbose] [-q|--quiet] [-f|--force] [-o|--output <file.h>] <dir1|pdb1> [...]\n"
|
---|
1158 | " or: %s [-V|--version]\n"
|
---|
1159 | " or: %s [-h|--help]\n",
|
---|
1160 | argv[0], argv[0], argv[0]);
|
---|
1161 | return RTEXITCODE_SUCCESS;
|
---|
1162 |
|
---|
1163 | case VINF_GETOPT_NOT_OPTION:
|
---|
1164 | {
|
---|
1165 | RTEXITCODE rcExit2;
|
---|
1166 | if (RTFileExists(ValueUnion.psz))
|
---|
1167 | rcExit2 = processPdb(ValueUnion.psz);
|
---|
1168 | else
|
---|
1169 | rcExit2 = processDir(ValueUnion.psz);
|
---|
1170 | if (rcExit2 != RTEXITCODE_SUCCESS)
|
---|
1171 | {
|
---|
1172 | if (!g_fOptForce)
|
---|
1173 | return rcExit2;
|
---|
1174 | rcExit = rcExit2;
|
---|
1175 | }
|
---|
1176 | break;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | default:
|
---|
1180 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1181 | }
|
---|
1182 | }
|
---|
1183 | if (RTListIsEmpty(&g_SetList))
|
---|
1184 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "No usable debug files found.\n");
|
---|
1185 |
|
---|
1186 | /*
|
---|
1187 | * Generate the output.
|
---|
1188 | */
|
---|
1189 | PRTSTREAM pOut = g_pStdOut;
|
---|
1190 | if (strcmp(pszOutput, "-"))
|
---|
1191 | {
|
---|
1192 | rc = RTStrmOpen(pszOutput, "w", &pOut);
|
---|
1193 | if (RT_FAILURE(rc))
|
---|
1194 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening '%s' for writing: %Rrc\n", pszOutput, rc);
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | generateHeader(pOut);
|
---|
1198 |
|
---|
1199 | if (pOut != g_pStdOut)
|
---|
1200 | rc = RTStrmClose(pOut);
|
---|
1201 | else
|
---|
1202 | rc = RTStrmFlush(pOut);
|
---|
1203 | if (RT_FAILURE(rc))
|
---|
1204 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %s '%s': %Rrc\n", pszOutput,
|
---|
1205 | pOut != g_pStdOut ? "closing" : "flushing", rc);
|
---|
1206 | return rcExit;
|
---|
1207 | }
|
---|