:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / ntdll / rtl / trace.c
1 /*
2  *  ReactOS kernel
3  *  Copyright (C) 2000 David Welch <welch@cwcom.net>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /* $Id$
20  *
21  * PROJECT:           ReactOS kernel
22  * PURPOSE:           Tracing library calls
23  * FILE:              lib/ntdll/rtl/trace.c
24  * PROGRAMER:         David Welch <welch@cwcom.net>
25  */
26
27 /* INCLUDES *****************************************************************/
28
29 #include <ddk/ntddk.h>
30 #include <ntdll/trace.h>
31 #include <string.h>
32 #include <stdarg.h>
33
34 #include <ntdll.h>
35
36 /* GLOBALS *******************************************************************/
37
38 static NTDLL_TRACE_TABLE TraceTable;
39 static BOOLEAN TraceTableValid = FALSE;
40
41 /* FUNCTIONS *****************************************************************/
42
43 NTSTATUS
44 RtlpInitTrace(VOID)
45 {
46   HANDLE SectionHandle;
47   UNICODE_STRING SectionName;
48   OBJECT_ATTRIBUTES ObjectAttributes;
49   CHAR Buffer[4096];
50   NTSTATUS Status;
51   PROCESS_BASIC_INFORMATION Pbi;
52   ULONG ReturnedSize;
53   PVOID BaseAddress;
54   LARGE_INTEGER Offset;
55   ULONG ViewSize;
56
57   Status = NtQueryInformationProcess(NtCurrentProcess(),
58                                      ProcessBasicInformation,
59                                      (PVOID)&Pbi,
60                                      sizeof(Pbi),
61                                      &ReturnedSize);
62   if (!NT_SUCCESS(Status))
63     {
64       return(Status);
65     }
66
67   sprintf(Buffer, "\\??\\TraceSection%d", Pbi.UniqueProcessId);
68
69   InitializeObjectAttributes(&ObjectAttributes,
70                              &SectionName,
71                              0,
72                              NULL,
73                              NULL);
74   Status = NtOpenSection(&SectionHandle,
75                          SECTION_MAP_READ,
76                          &ObjectAttributes);
77   if (!NT_SUCCESS(Status))
78     {
79       return(Status);
80     }
81
82   BaseAddress = 0;
83   Offset.QuadPart = 0;
84   ViewSize = 0;
85   Status = NtMapViewOfSection(SectionHandle,
86                               NtCurrentProcess(),
87                               &BaseAddress,
88                               0,
89                               sizeof(NTDLL_TRACE_TABLE),
90                               &Offset,
91                               &ViewSize,
92                               ViewUnmap,
93                               0,
94                               PAGE_READONLY);
95   if (!NT_SUCCESS(Status))
96     {
97       NtClose(SectionHandle);
98       return(Status);
99     }
100   NtClose(SectionHandle);
101
102   memcpy(&TraceTable, BaseAddress, sizeof(TraceTable));
103   TraceTableValid = TRUE;
104
105   Status = NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
106
107   return(STATUS_SUCCESS);
108 }
109
110 VOID
111 RtlPrintTrace(ULONG Flag, PCH Format, ...)
112 {
113   va_list ap;
114   CHAR FString[4096];
115
116   if (!TraceTableValid)
117     {
118       return;
119     }
120   if (TraceTable.Flags[Flag / BITS_IN_CHAR] & (1 << (Flag % BITS_IN_CHAR)))
121     {
122       va_start(ap, Format);
123       vsprintf(FString, Format, ap);
124       DbgPrint(FString);
125       va_end(ap);
126     }
127 }