[SHIMDBG] Output both to the console and the debug output + fix a copypasta error.
authorMark Jansen <mark.jansen@reactos.org>
Sun, 2 Jul 2017 11:56:01 +0000 (11:56 +0000)
committerMark Jansen <mark.jansen@reactos.org>
Sun, 2 Jul 2017 11:56:01 +0000 (11:56 +0000)
svn path=/trunk/; revision=75264

rosapps/applications/devutils/shimdbg/shimdbg.c

index 43fe873..3bceef8 100644 (file)
 #include <ctype.h>
 #include <ntndk.h>
 
+void __stdcall OutputDebugStringA(PCSTR);
+
+void xprintf(const char *fmt, ...)
+{
+    va_list ap;
+    int length;
+    char *buf;
+
+    va_start(ap, fmt);
+    length = _vscprintf(fmt, ap);
+    buf = malloc(length + 1);
+    vsprintf(buf, fmt, ap);
+    buf[length] = '\0';
+    va_end(ap);
+
+    fputs(buf, stdout);
+    OutputDebugStringA(buf);
+}
+
+
 void CallApphelp(APPHELPCACHESERVICECLASS Service,
                 PAPPHELP_CACHE_SERVICE_LOOKUP CacheEntry)
 {
     NTSTATUS Status = NtApphelpCacheControl(Service, CacheEntry);
-    printf("NtApphelpCacheControl returned 0x%x\n", (unsigned int)Status);
+    xprintf("NtApphelpCacheControl returned 0x%x\n", (unsigned int)Status);
 }
 
 HANDLE MapFile(char* filename, UNICODE_STRING* PathName, int MapIt)
@@ -35,7 +55,7 @@ HANDLE MapFile(char* filename, UNICODE_STRING* PathName, int MapIt)
                     FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
         if (!NT_SUCCESS(Status))
         {
-            printf("Failed opening the file, using a NULL handle\n");
+            xprintf("Failed opening the file, using a NULL handle\n");
             FileHandle = NULL;
         }
     }
@@ -50,7 +70,7 @@ void CallApphelpWithImage(char* filename, int MapIt,
 
     HANDLE FileHandle = MapFile(filename, &PathName, MapIt);
 
-    printf("Calling %s %s mapping\n", ServiceName, (MapIt ? "with" : "without"));
+    xprintf("Calling %s %s mapping\n", ServiceName, (MapIt ? "with" : "without"));
 
     RtlInitUnicodeString(&CacheEntry.ImageName, PathName.Buffer);
     CacheEntry.ImageHandle = FileHandle ? FileHandle : (HANDLE)-1;
@@ -82,7 +102,7 @@ int HandleImageArg(int argc, char* argv[], int* pn, char MapItChar,
         (*pn) += 1;
         return 0;
     }
-    printf("Error: no image name specified\n");
+    xprintf("Error: no image name specified\n");
     return 1;
 }
 
@@ -124,7 +144,7 @@ static BOOL InitApphelp()
         static ANSI_STRING SdbFreeFileAttributes = RTL_CONSTANT_STRING("SdbFreeFileAttributes");
         if (!NT_SUCCESS(LdrLoadDll(NULL, NULL, &DllName, &hdll)))
         {
-            printf("Unable to load apphelp.dll\n");
+            xprintf("Unable to load apphelp.dll\n");
             return FALSE;
         }
         if (!NT_SUCCESS(LdrGetProcedureAddress(hdll, &SdbTagToString, 0, (PVOID)&pSdbTagToString)) ||
@@ -133,7 +153,7 @@ static BOOL InitApphelp()
         {
             LdrUnloadDll(hdll);
             hdll = NULL;
-            printf("Unable to resolve functions\n");
+            xprintf("Unable to resolve functions\n");
             return FALSE;
         }
     }
@@ -158,7 +178,7 @@ int HandleDumpAttributes(int argc, char* argv[], int* pn, const char* opt)
     }
     else if (argn+1 >= argc)
     {
-        printf("Error: no image name specified\n");
+        xprintf("Error: no image name specified\n");
         return 1;
     }
     else
@@ -171,7 +191,7 @@ int HandleDumpAttributes(int argc, char* argv[], int* pn, const char* opt)
 
     if (pSdbGetFileAttributes(FileName.Buffer, &attr, &num_attr))
     {
-        printf("Dumping attributes for %s\n", arg);
+        xprintf("Dumping attributes for %s\n", arg);
         for (n = 0; n < num_attr; ++n)
         {
             TAG tagType;
@@ -185,24 +205,24 @@ int HandleDumpAttributes(int argc, char* argv[], int* pn, const char* opt)
             switch (tagType)
             {
             case TAG_TYPE_DWORD:
-                printf("<%ls>0x%lx</%ls><!-- %ld -->\n", tagName, attr[n].dwattr, tagName, attr[n].dwattr);
+                xprintf("<%ls>0x%lx</%ls>\n", tagName, attr[n].dwattr, tagName);
                 break;
             case TAG_TYPE_STRINGREF:
-                printf("<%ls>0x%ls</%ls>\n", tagName, attr[n].lpattr, tagName);
+                xprintf("<%ls>%ls</%ls>\n", tagName, attr[n].lpattr, tagName);
                 break;
             case TAG_TYPE_QWORD:
-                printf("<%ls>0x%I64x</%ls><!-- %I64d -->\n", tagName, attr[n].qwattr, tagName, attr[n].qwattr);
+                xprintf("<%ls>0x%I64x</%ls>\n", tagName, attr[n].qwattr, tagName);
                 break;
             default:
-                printf("<!-- Unknown tag type: 0x%x (from 0x%x)\n", tagType, attr[n].type);
+                xprintf("<!-- Unknown tag type: 0x%x (from 0x%x)\n", tagType, attr[n].type);
                 break;
             }
         }
-        printf("Done\n");
+        xprintf("Done\n");
     }
     else
     {
-        printf("Unable to get attributes from %s\n", arg);
+        xprintf("Unable to get attributes from %s\n", arg);
     }
 
 
@@ -224,21 +244,21 @@ void hexdump(unsigned char *p, unsigned int len)
 
     while (offset < len)
     {
-        printf("%04x ", offset);
+        xprintf("%04x ", offset);
         thisline = len - offset;
         if (thisline > 16)
             thisline = 16;
 
         for (i = 0; i < thisline; i++)
-            printf("%02x ", line[i]);
+            xprintf("%02x ", line[i]);
 
         for (; i < 16; i++)
-            printf("   ");
+            xprintf("   ");
 
         for (i = 0; i < thisline; i++)
-            printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
+            xprintf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
 
-        printf("\n");
+        xprintf("\n");
         offset += thisline;
         line += thisline;
     }
@@ -252,7 +272,7 @@ void DumpRegistryData(int IncludeDump)
     PKEY_VALUE_PARTIAL_INFORMATION KeyValueInformation = &KeyValueObject;
     ULONG KeyInfoSize, ResultSize;
 
-    printf("Dumping AppCompatCache registry key\n");
+    xprintf("Dumping AppCompatCache registry key\n");
 
     Status = NtOpenKey(&KeyHandle, KEY_QUERY_VALUE, &AppCompatKeyAttributes);
 
@@ -278,11 +298,11 @@ void DumpRegistryData(int IncludeDump)
         if (IncludeDump)
             hexdump(KeyValueInformation->Data, KeyValueInformation->DataLength);
         crc = RtlComputeCrc32(0, KeyValueInformation->Data, KeyValueInformation->DataLength);
-        printf("Len: %lu, Crc: 0x%lx\n", KeyValueInformation->DataLength, crc);
+        xprintf("Len: %lu, Crc: 0x%lx\n", KeyValueInformation->DataLength, crc);
     }
     else
     {
-        printf("Failed reading AppCompatCache from registry (0x%lx)\n", Status);
+        xprintf("Failed reading AppCompatCache from registry (0x%lx)\n", Status);
     }
 
     if (KeyValueInformation != &KeyValueObject)
@@ -303,7 +323,7 @@ int main(int argc, char* argv[])
         char* arg = argv[n];
         if (IsOpt(arg, "d"))
         {
-            printf("Calling ApphelpCacheServiceDump\n");
+            xprintf("Calling ApphelpCacheServiceDump\n");
             CallApphelp(ApphelpCacheServiceDump, NULL);
             unhandled = 0;
         }
@@ -314,19 +334,19 @@ int main(int argc, char* argv[])
         }
         else if (IsOpt(arg, "f"))
         {
-            printf("Calling ApphelpCacheServiceFlush\n");
+            xprintf("Calling ApphelpCacheServiceFlush\n");
             CallApphelp(ApphelpCacheServiceFlush, NULL);
             unhandled = 0;
         }
         else if (IsOpt(arg, "z"))
         {
-            printf("Calling ApphelpDBGReadRegistry\n");
+            xprintf("Calling ApphelpDBGReadRegistry\n");
             CallApphelp(ApphelpDBGReadRegistry, NULL);
             unhandled = 0;
         }
         else if (IsOpt(arg, "x"))
         {
-            printf("Calling ApphelpDBGWriteRegistry\n");
+            xprintf("Calling ApphelpDBGWriteRegistry\n");
             CallApphelp(ApphelpDBGWriteRegistry, NULL);
             unhandled = 0;
         }
@@ -360,21 +380,21 @@ int main(int argc, char* argv[])
     }
     if (unhandled || argc == 1)
     {
-        printf("Usage: %s [-d|-z|-x|-h|-H|-f|-[l|L] <image>|-[u|U] <image>|-[r|R] <image>|-k]\n", argv[0]);
-        printf("           -d: Dump shim cache over debug output\n");
-        printf("           -z: DEBUG Read shim cache from registry\n");
-        printf("           -x: DEBUG Write shim cache to registry\n");
-        printf("           -h: Hexdump shim registry key\n");
-        printf("           -H: Crc + Length from shim registry key only\n");
-        printf("           -f: Flush (clear) the shim cache\n");
-        printf("           -l: Lookup <image> in the shim cache\n");
-        printf("           -L: Lookup <image> in the shim cache without mapping it\n");
-        printf("           -u: Update (insert) <image> in the shim cache\n");
-        printf("           -U: Update (insert) <image> in the shim cache without mapping it\n");
-        printf("           -r: Remove <image> from the shim cache\n");
-        printf("           -R: Remove <image> from the shim cache without mapping it\n");
-        printf("           -a: Dump file attributes as used in the appcompat database\n");
-        printf("           -k: Keep the console open\n");
+        xprintf("Usage: %s [-d|-z|-x|-h|-H|-f|-[l|L] <image>|-[u|U] <image>|-[r|R] <image>|-k]\n", argv[0]);
+        xprintf("           -d: Dump shim cache over debug output\n");
+        xprintf("           -z: DEBUG Read shim cache from registry\n");
+        xprintf("           -x: DEBUG Write shim cache to registry\n");
+        xprintf("           -h: Hexdump shim registry key\n");
+        xprintf("           -H: Crc + Length from shim registry key only\n");
+        xprintf("           -f: Flush (clear) the shim cache\n");
+        xprintf("           -l: Lookup <image> in the shim cache\n");
+        xprintf("           -L: Lookup <image> in the shim cache without mapping it\n");
+        xprintf("           -u: Update (insert) <image> in the shim cache\n");
+        xprintf("           -U: Update (insert) <image> in the shim cache without mapping it\n");
+        xprintf("           -r: Remove <image> from the shim cache\n");
+        xprintf("           -R: Remove <image> from the shim cache without mapping it\n");
+        xprintf("           -a: Dump file attributes as used in the appcompat database\n");
+        xprintf("           -k: Keep the console open\n");
     }
     if (keepopen)
     {