From c959a66fb59d46a978d484b9faa65a9d58415e05 Mon Sep 17 00:00:00 2001 From: Mark Jansen Date: Tue, 27 Jun 2017 20:27:31 +0000 Subject: [PATCH] [SHIMDBG] Add option to dump file properties. svn path=/trunk/; revision=75218 --- .../applications/devutils/shimdbg/shimdbg.c | 130 +++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/rosapps/applications/devutils/shimdbg/shimdbg.c b/rosapps/applications/devutils/shimdbg/shimdbg.c index d58b06da37d..43fe873610d 100644 --- a/rosapps/applications/devutils/shimdbg/shimdbg.c +++ b/rosapps/applications/devutils/shimdbg/shimdbg.c @@ -66,7 +66,7 @@ void CallApphelpWithImage(char* filename, int MapIt, int IsOpt(char* argv, const char* check) { if( argv && (argv[0] == '-' || argv[0] == '/') ) { - return !stricmp(argv + 1, check); + return !_strnicmp(argv + 1, check, strlen(check)); } return 0; } @@ -86,6 +86,129 @@ int HandleImageArg(int argc, char* argv[], int* pn, char MapItChar, return 1; } +typedef WORD TAG; +typedef UINT64 QWORD; + +#define TAG_TYPE_MASK 0xF000 +#define TAG_TYPE_DWORD 0x4000 +#define TAG_TYPE_QWORD 0x5000 +#define TAG_TYPE_STRINGREF 0x6000 + +#define ATTRIBUTE_AVAILABLE 0x1 +#define ATTRIBUTE_FAILED 0x2 + +typedef struct tagATTRINFO +{ + TAG type; + DWORD flags; /* ATTRIBUTE_AVAILABLE, ATTRIBUTE_FAILED */ + union + { + QWORD qwattr; + DWORD dwattr; + WCHAR *lpattr; + }; +} ATTRINFO, *PATTRINFO; + +static PVOID hdll; +static LPCWSTR (WINAPI *pSdbTagToString)(TAG); +static BOOL (WINAPI *pSdbGetFileAttributes)(LPCWSTR, PATTRINFO *, LPDWORD); +static BOOL (WINAPI *pSdbFreeFileAttributes)(PATTRINFO); + +static BOOL InitApphelp() +{ + if (!hdll) + { + static UNICODE_STRING DllName = RTL_CONSTANT_STRING(L"apphelp.dll"); + static ANSI_STRING SdbTagToString = RTL_CONSTANT_STRING("SdbTagToString"); + static ANSI_STRING SdbGetFileAttributes = RTL_CONSTANT_STRING("SdbGetFileAttributes"); + static ANSI_STRING SdbFreeFileAttributes = RTL_CONSTANT_STRING("SdbFreeFileAttributes"); + if (!NT_SUCCESS(LdrLoadDll(NULL, NULL, &DllName, &hdll))) + { + printf("Unable to load apphelp.dll\n"); + return FALSE; + } + if (!NT_SUCCESS(LdrGetProcedureAddress(hdll, &SdbTagToString, 0, (PVOID)&pSdbTagToString)) || + !NT_SUCCESS(LdrGetProcedureAddress(hdll, &SdbGetFileAttributes, 0, (PVOID)&pSdbGetFileAttributes)) || + !NT_SUCCESS(LdrGetProcedureAddress(hdll, &SdbFreeFileAttributes, 0, (PVOID)&pSdbFreeFileAttributes))) + { + LdrUnloadDll(hdll); + hdll = NULL; + printf("Unable to resolve functions\n"); + return FALSE; + } + } + return TRUE; +} + + +int HandleDumpAttributes(int argc, char* argv[], int* pn, const char* opt) +{ + UNICODE_STRING FileName; + PATTRINFO attr; + DWORD num_attr, n; + int argn = *pn; + const char* arg; + + if (!InitApphelp()) + return 1; + + if (strlen(argv[argn]) > (strlen(opt)+1)) + { + arg = argv[argn] + strlen(opt); + } + else if (argn+1 >= argc) + { + printf("Error: no image name specified\n"); + return 1; + } + else + { + arg = argv[argn+1]; + (*pn) += 1; + } + + RtlCreateUnicodeStringFromAsciiz(&FileName, arg); + + if (pSdbGetFileAttributes(FileName.Buffer, &attr, &num_attr)) + { + printf("Dumping attributes for %s\n", arg); + for (n = 0; n < num_attr; ++n) + { + TAG tagType; + LPCWSTR tagName; + if (attr[n].flags != ATTRIBUTE_AVAILABLE) + continue; + + tagName = pSdbTagToString(attr[n].type); + + tagType = attr[n].type & TAG_TYPE_MASK; + switch (tagType) + { + case TAG_TYPE_DWORD: + printf("<%ls>0x%lx\n", tagName, attr[n].dwattr, tagName, attr[n].dwattr); + break; + case TAG_TYPE_STRINGREF: + printf("<%ls>0x%ls\n", tagName, attr[n].lpattr, tagName); + break; + case TAG_TYPE_QWORD: + printf("<%ls>0x%I64x\n", tagName, attr[n].qwattr, tagName, attr[n].qwattr); + break; + default: + printf("