Merging r37048, r37051, r37052, r37055 from the-real-msvc branch
[reactos.git] / reactos / lib / rtl / debug.c
index dbcfc1e..a17367f 100644 (file)
@@ -22,8 +22,6 @@ DebugPrint(IN PANSI_STRING DebugString,
            IN ULONG ComponentId,
            IN ULONG Level)
 {
-    return STATUS_SUCCESS;
-
     /* Call the INT2D Service */
     return DebugService(BREAKPOINT_PRINT,
                         DebugString->Buffer,
@@ -56,51 +54,46 @@ vDbgPrintExWithPrefixInternal(IN LPCSTR Prefix,
                               IN va_list ap,
                               IN BOOLEAN HandleBreakpoint)
 {
-    NTSTATUS Status;
+    NTSTATUS Status = STATUS_SUCCESS;
     ANSI_STRING DebugString;
     CHAR Buffer[512];
-    PCHAR pBuffer = Buffer;
-    ULONG pBufferSize = sizeof(Buffer);
-    ULONG Length;
+    ULONG Length, PrefixLength;
     EXCEPTION_RECORD ExceptionRecord;
 
     /* Check if we should print it or not */
-    if (ComponentId != -1 && !NtQueryDebugFilterState(ComponentId, Level))
+    if ((ComponentId != -1) && !(NtQueryDebugFilterState(ComponentId, Level)))
     {
         /* This message is masked */
-        return STATUS_SUCCESS;
+        return Status;
     }
 
     /* For user mode, don't recursively DbgPrint */
-    if (RtlpSetInDbgPrint(TRUE)) return STATUS_SUCCESS;
+    if (RtlpSetInDbgPrint(TRUE)) return Status;
 
-    /* Initialize the length to 8 */
-    DebugString.Length = 0;
-
-    /* Handle the prefix */
-    if (Prefix && *Prefix)
+    /* Guard against incorrect pointers */
+    _SEH_TRY
     {
-        /* Get the length */
-        DebugString.Length = strlen(Prefix);
-
-        /* Normalize it */
-        if(DebugString.Length > sizeof(Buffer))
-        {
-            DebugString.Length = sizeof(Buffer);
-        }
+        /* Get the length and normalize it */
+        PrefixLength = strlen(Prefix);
+        if (PrefixLength > sizeof(Buffer)) PrefixLength = sizeof(Buffer);
 
         /* Copy it */
-        strncpy(Buffer, Prefix, DebugString.Length);
+        strncpy(Buffer, Prefix, PrefixLength);
 
-        /* Set the pointer and update the size */
-        pBuffer = &Buffer[DebugString.Length];
-        pBufferSize -= DebugString.Length;
+        /* Do the printf */
+        Length = _vsnprintf(Buffer + PrefixLength,
+                            sizeof(Buffer) - PrefixLength,
+                            Format,
+                            ap);
     }
-
-    /* Setup the ANSI String */
-    DebugString.Buffer = Buffer;
-    DebugString.MaximumLength = sizeof(Buffer);
-    Length = _vsnprintf(pBuffer, pBufferSize, Format, ap);
+    _SEH_HANDLE
+    {
+        /* Fail */
+        Length = PrefixLength = 0;
+        Status = _SEH_GetExceptionCode();
+    }
+    _SEH_END;
+    if (!NT_SUCCESS(Status)) return Status;
 
     /* Check if we went past the buffer */
     if (Length == -1)
@@ -111,9 +104,15 @@ vDbgPrintExWithPrefixInternal(IN LPCSTR Prefix,
         /* Put maximum */
         Length = sizeof(Buffer);
     }
+    else
+    {
+        /* Add the prefix */
+        Length += PrefixLength;
+    }
 
-    /* Update length */
-    DebugString.Length += Length;
+    /* Build the string */
+    DebugString.Length = Length;
+    DebugString.Buffer = Buffer;
 
     /* First, let the debugger know as well */
     if (RtlpCheckForActiveDebugger(FALSE))
@@ -144,7 +143,7 @@ vDbgPrintExWithPrefixInternal(IN LPCSTR Prefix,
         if (Status == STATUS_BREAKPOINT)
         {
             /* Breakpoint */
-            DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
+            //DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
             Status = STATUS_SUCCESS;
         }
     }
@@ -187,7 +186,7 @@ vDbgPrintEx(IN ULONG ComponentId,
             IN va_list ap)
 {
     /* Call the internal routine that also handles ControlC */
-    return vDbgPrintExWithPrefixInternal(NULL,
+    return vDbgPrintExWithPrefixInternal("",
                                          ComponentId,
                                          Level,
                                          Format,
@@ -203,17 +202,19 @@ __cdecl
 DbgPrint(PCCH Format,
          ...)
 {
+       ULONG n;
     va_list ap;
 
     /* Call the internal routine that also handles ControlC */
     va_start(ap, Format);
-    return vDbgPrintExWithPrefixInternal(NULL,
+    n = vDbgPrintExWithPrefixInternal("",
                                          -1,
                                          DPFLTR_ERROR_LEVEL,
                                          Format,
                                          ap,
                                          TRUE);
     va_end(ap);
+       return n;
 }
 
 /*
@@ -226,17 +227,19 @@ DbgPrintEx(IN ULONG ComponentId,
            IN PCCH Format,
            ...)
 {
+       ULONG n;
     va_list ap;
 
     /* Call the internal routine that also handles ControlC */
     va_start(ap, Format);
-    return vDbgPrintExWithPrefixInternal(NULL,
+    n = vDbgPrintExWithPrefixInternal("",
                                          ComponentId,
                                          Level,
                                          Format,
                                          ap,
                                          TRUE);
     va_end(ap);
+       return n;
 }
 
 /*
@@ -247,16 +250,19 @@ __cdecl
 DbgPrintReturnControlC(PCH Format,
                        ...)
 {
+       ULONG n;
     va_list ap;
 
     /* Call the internal routine that also handles ControlC */
     va_start(ap, Format);
-    return vDbgPrintExWithPrefixInternal(NULL,
+    n = vDbgPrintExWithPrefixInternal("",
                                          -1,
                                          DPFLTR_ERROR_LEVEL,
                                          Format,
                                          ap,
                                          FALSE);
+    va_end(ap);
+       return n;
 }
 
 /*
@@ -272,7 +278,7 @@ DbgPrompt(IN PCCH Prompt,
     STRING Input;
 
     /* Setup the input string */
-    Input.MaximumLength = MaximumResponseLength;
+    Input.MaximumLength = (USHORT)MaximumResponseLength;
     Input.Buffer = Response;
 
     /* Setup the output string */
@@ -286,7 +292,7 @@ DbgPrompt(IN PCCH Prompt,
 /*
  * @implemented
  */
-BOOLEAN
+NTSTATUS
 NTAPI
 DbgQueryDebugFilterState(IN ULONG ComponentId,
                          IN ULONG Level)
@@ -322,10 +328,10 @@ DbgLoadImageSymbols(IN PANSI_STRING Name,
 
     /* Setup the symbol data */
     SymbolInfo.BaseOfDll = Base;
-    SymbolInfo.ProcessId = ProcessId;
+    SymbolInfo.ProcessId = (ULONG)ProcessId;
 
     /* Get NT Headers */
-    NtHeader = NULL; //RtlImageNtHeader(Base);
+    NtHeader = RtlImageNtHeader(Base);
     if (NtHeader)
     {
         /* Get the rest of the data */
@@ -344,8 +350,8 @@ DbgLoadImageSymbols(IN PANSI_STRING Name,
 }
 
 /*
-* @implemented
-*/
+ * @implemented
+ */
 VOID
 NTAPI
 DbgUnLoadImageSymbols(IN PANSI_STRING Name,
@@ -356,7 +362,7 @@ DbgUnLoadImageSymbols(IN PANSI_STRING Name,
 
     /* Setup the symbol data */
     SymbolInfo.BaseOfDll = Base;
-    SymbolInfo.ProcessId = ProcessId;
+    SymbolInfo.ProcessId = (ULONG)ProcessId;
     SymbolInfo.CheckSum = SymbolInfo.SizeOfImage = 0;
 
     /* Load the symbols */