VirtualBox

source: vbox/trunk/src/VBox/Runtime/VBox/log-vbox.cpp@ 20374

Last change on this file since 20374 was 19315, checked in by vboxsync, 16 years ago

Logging: introducing NAT and NET log groups

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.9 KB
Line 
1/* $Id: log-vbox.cpp 19315 2009-05-04 03:29:02Z vboxsync $ */
2/** @file
3 * VirtualBox Runtime - Logging configuration.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/** @page pg_rtlog Runtime - Logging
32 *
33 * VBox uses the IPRT logging system which supports group level flags and multiple
34 * destinations. The GC logging is making it even more interesting since GC logging will
35 * have to be buffered and written when back in host context.
36 *
37 * [more later]
38 *
39 *
40 * @section sec_logging_destination The Destination Specifier.
41 *
42 * The {logger-env-base}_DEST environment variable can be used to specify where
43 * the log output goes. The following specifiers are recognized:
44 *
45 * - file=\<filename\>
46 * This sets the logger output filename to \<filename\>. Not formatting
47 * or anything is supported. Each logger specifies a default name if
48 * file logging should be enabled by default.
49 *
50 * - nofile
51 * This disables the file output.
52 *
53 * - stdout
54 * Enables logger output to stdout.
55 *
56 * - nostdout
57 * Disables logger output to stdout.
58 *
59 * - stderr
60 * Enables logger output to stderr.
61 *
62 * - nostderr
63 * Disables logger output to stderr.
64 *
65 * - debugger
66 * Enables logger output to native debugger. (Win32/64 only)
67 *
68 * - nodebugger
69 * Disables logger output to native debugger. (Win32/64 only)
70 *
71 * - user
72 * Enables logger output to special backdoor if in guest r0.
73 *
74 * - nodebugger
75 * Disables logger output to special user stream.
76 *
77 *
78 *
79 * @section sec_logging_destination The Group Specifier.
80 *
81 * The {logger-env-base} environment variable can be used to specify which
82 * logger groups to enable and which to disable. By default all groups are
83 * disabled. For your convenience this specifier is case in-sensitive (ASCII).
84 *
85 * The specifier is evaluated from left to right.
86 *
87 * [more later]
88 *
89 * The groups settings can be reprogrammed during execution using the
90 * RTLogGroupSettings() command and a group specifier.
91 *
92 *
93 *
94 * @section sec_logging_default The Default Logger
95 *
96 * The default logger uses VBOX_LOG_DEST as destination specifier. File output is
97 * enabled by default and goes to a file "./VBox-\<pid\>.log".
98 *
99 * The default logger have all groups turned off by default to force the developer
100 * to be careful with what log information to collect - logging everything is
101 * generally NOT a good idea.
102 *
103 * The log groups of the default logger can be found in the LOGGROUP in enum. The
104 * VBOX_LOG environment variable and the .log debugger command can be used to
105 * configure the groups.
106 *
107 * Each group have flags in addition to the enable/disable flag. These flags can
108 * be appended to the group name using dot separators. The flags correspond to
109 * RTLOGGRPFLAGS and have a short and a long version:
110 *
111 * - e - Enabled: Whether the group is enabled at all.
112 * - l - Level2: Level-2 logging.
113 * - f - Flow: Execution flow logging (entry messages)
114 * - s - Sander: Special Sander logging messages.
115 * - b - Bird: Special Bird logging messages.
116 *
117 * @todo Update this section...
118 *
119 * Example:
120 *
121 * VBOX_LOG=+all+pgm.e.s.b.z.l-qemu
122 *
123 * Space and ';' separators are allowed:
124 *
125 * VBOX_LOG=+all +pgm.e.s.b.z.l ; - qemu
126 *
127 */
128
129
130/*******************************************************************************
131* Header Files *
132*******************************************************************************/
133#ifdef IN_RING3
134# if defined(RT_OS_WINDOWS)
135# include <Windows.h>
136# elif defined(RT_OS_LINUX)
137# include <unistd.h>
138# elif defined(RT_OS_SOLARIS)
139# define _STRUCTURED_PROC 1
140# undef _FILE_OFFSET_BITS /* procfs doesn't like this */
141# include <sys/procfs.h>
142# include <unistd.h>
143# elif defined(RT_OS_L4)
144# include <l4/vboxserver/vboxserver.h>
145# elif defined(RT_OS_OS2)
146# include <stdlib.h>
147# endif
148#endif
149
150#include <VBox/log.h>
151#include <iprt/asm.h>
152#include <iprt/err.h>
153#include <iprt/time.h>
154#ifdef IN_RING3
155# include <iprt/param.h>
156# include <iprt/assert.h>
157# include <iprt/path.h>
158# include <iprt/process.h>
159# include <iprt/string.h>
160# include <stdio.h>
161#endif
162
163
164/*******************************************************************************
165* Global Variables *
166*******************************************************************************/
167/** The default logger. */
168static PRTLOGGER g_pLogger = NULL;
169/** The default logger groups.
170 * This must match LOGGROUP! */
171static const char *g_apszGroups[] =
172VBOX_LOGGROUP_NAMES;
173
174
175/**
176 * Creates the default logger instance for a VBox process.
177 *
178 * @returns Pointer to the logger instance.
179 */
180RTDECL(PRTLOGGER) RTLogDefaultInit(void)
181{
182 /*
183 * Initialize the default logger instance.
184 * Take care to do this once and not recursivly.
185 */
186 static volatile uint32_t fInitializing = 0;
187 PRTLOGGER pLogger;
188 int rc;
189
190 if (g_pLogger || !ASMAtomicCmpXchgU32(&fInitializing, 1, 0))
191 return g_pLogger;
192
193#ifdef IN_RING3
194 /*
195 * Assert the group definitions.
196 */
197#define ASSERT_LOG_GROUP(grp) ASSERT_LOG_GROUP2(LOG_GROUP_##grp, #grp)
198#define ASSERT_LOG_GROUP2(def, str) \
199 do { if (strcmp(g_apszGroups[def], str)) {printf("%s='%s' expects '%s'\n", #def, g_apszGroups[def], str); RTAssertDoPanic(); } } while (0)
200 ASSERT_LOG_GROUP(DEFAULT);
201 ASSERT_LOG_GROUP(CFGM);
202 ASSERT_LOG_GROUP(CPUM);
203 ASSERT_LOG_GROUP(CSAM);
204 ASSERT_LOG_GROUP(DBGC);
205 ASSERT_LOG_GROUP(DBGF);
206 ASSERT_LOG_GROUP(DBGF_INFO);
207 ASSERT_LOG_GROUP(DEV);
208 ASSERT_LOG_GROUP(DEV_ACPI);
209 ASSERT_LOG_GROUP(DEV_APIC);
210 ASSERT_LOG_GROUP(DEV_AUDIO);
211 ASSERT_LOG_GROUP(DEV_FDC);
212 ASSERT_LOG_GROUP(DEV_HPET);
213 ASSERT_LOG_GROUP(DEV_IDE);
214 ASSERT_LOG_GROUP(DEV_KBD);
215 ASSERT_LOG_GROUP(DEV_LPC);
216 ASSERT_LOG_GROUP(DEV_NE2000);
217 ASSERT_LOG_GROUP(DEV_PC);
218 ASSERT_LOG_GROUP(DEV_PC_ARCH);
219 ASSERT_LOG_GROUP(DEV_PC_BIOS);
220 ASSERT_LOG_GROUP(DEV_PCI);
221 ASSERT_LOG_GROUP(DEV_PCNET);
222 ASSERT_LOG_GROUP(DEV_PIC);
223 ASSERT_LOG_GROUP(DEV_PIT);
224 ASSERT_LOG_GROUP(DEV_RTC);
225 ASSERT_LOG_GROUP(DEV_SERIAL);
226 ASSERT_LOG_GROUP(DEV_SMC);
227 ASSERT_LOG_GROUP(DEV_USB);
228 ASSERT_LOG_GROUP(DEV_VGA);
229 ASSERT_LOG_GROUP(DEV_VMM);
230 ASSERT_LOG_GROUP(DEV_VMM_STDERR);
231 ASSERT_LOG_GROUP(DIS);
232 ASSERT_LOG_GROUP(DRV);
233 ASSERT_LOG_GROUP(DRV_ACPI);
234 ASSERT_LOG_GROUP(DRV_BLOCK);
235 ASSERT_LOG_GROUP(DRV_FLOPPY);
236 ASSERT_LOG_GROUP(DRV_HOST_DVD);
237 ASSERT_LOG_GROUP(DRV_HOST_FLOPPY);
238 ASSERT_LOG_GROUP(DRV_ISCSI);
239 ASSERT_LOG_GROUP(DRV_ISCSI_TRANSPORT_TCP);
240 ASSERT_LOG_GROUP(DRV_ISO);
241 ASSERT_LOG_GROUP(DRV_KBD_QUEUE);
242 ASSERT_LOG_GROUP(DRV_MOUSE_QUEUE);
243 ASSERT_LOG_GROUP(DRV_NAT);
244 ASSERT_LOG_GROUP(DRV_RAW_IMAGE);
245 ASSERT_LOG_GROUP(DRV_TUN);
246 ASSERT_LOG_GROUP(DRV_USBPROXY);
247 ASSERT_LOG_GROUP(DRV_VBOXHDD);
248 ASSERT_LOG_GROUP(DRV_VSWITCH);
249 ASSERT_LOG_GROUP(DRV_VUSB);
250 ASSERT_LOG_GROUP(EM);
251 ASSERT_LOG_GROUP(GUI);
252 ASSERT_LOG_GROUP(HGCM);
253 ASSERT_LOG_GROUP(HWACCM);
254 ASSERT_LOG_GROUP(IOM);
255 ASSERT_LOG_GROUP(MAIN);
256 ASSERT_LOG_GROUP(MM);
257 ASSERT_LOG_GROUP(MM_HEAP);
258 ASSERT_LOG_GROUP(MM_HYPER);
259 ASSERT_LOG_GROUP(MM_HYPER_HEAP);
260 ASSERT_LOG_GROUP(MM_PHYS);
261 ASSERT_LOG_GROUP(MM_POOL);
262 ASSERT_LOG_GROUP(NAT_SERVICE);
263 ASSERT_LOG_GROUP(NET_SERVICE);
264 ASSERT_LOG_GROUP(PATM);
265 ASSERT_LOG_GROUP(PDM);
266 ASSERT_LOG_GROUP(PDM_DEVICE);
267 ASSERT_LOG_GROUP(PDM_DRIVER);
268 ASSERT_LOG_GROUP(PDM_LDR);
269 ASSERT_LOG_GROUP(PDM_QUEUE);
270 ASSERT_LOG_GROUP(PGM);
271 ASSERT_LOG_GROUP(PGM_POOL);
272 ASSERT_LOG_GROUP(REM);
273 ASSERT_LOG_GROUP(REM_DISAS);
274 ASSERT_LOG_GROUP(REM_HANDLER);
275 ASSERT_LOG_GROUP(REM_IOPORT);
276 ASSERT_LOG_GROUP(REM_MMIO);
277 ASSERT_LOG_GROUP(REM_PRINTF);
278 ASSERT_LOG_GROUP(REM_RUN);
279 ASSERT_LOG_GROUP(SELM);
280 ASSERT_LOG_GROUP(SSM);
281 ASSERT_LOG_GROUP(STAM);
282 ASSERT_LOG_GROUP(SUP);
283 ASSERT_LOG_GROUP(TM);
284 ASSERT_LOG_GROUP(TRPM);
285 ASSERT_LOG_GROUP(VM);
286 ASSERT_LOG_GROUP(VMM);
287 ASSERT_LOG_GROUP(VRDP);
288#undef ASSERT_LOG_GROUP
289#undef ASSERT_LOG_GROUP2
290#endif /* IN_RING3 */
291
292 /*
293 * Create the default logging instance.
294 */
295#ifdef IN_RING3
296# ifndef IN_GUEST_R3
297 char szExecName[RTPATH_MAX];
298 if (!RTProcGetExecutableName(szExecName, sizeof(szExecName)))
299 strcpy(szExecName, "VBox");
300 RTTIMESPEC TimeSpec;
301 RTTIME Time;
302 RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
303 rc = RTLogCreate(&pLogger, 0, NULL, "VBOX_LOG", RT_ELEMENTS(g_apszGroups), &g_apszGroups[0], RTLOGDEST_FILE,
304 "./%04d-%02d-%02d-%02d-%02d-%02d.%03d-%s-%d.log",
305 Time.i32Year, Time.u8Month, Time.u8MonthDay, Time.u8Hour, Time.u8Minute, Time.u8Second, Time.u32Nanosecond / 10000000,
306 RTPathFilename(szExecName), RTProcSelf());
307 if (RT_SUCCESS(rc))
308 {
309 /*
310 * Write a log header.
311 */
312 char szBuf[RTPATH_MAX];
313 RTTimeSpecToString(&TimeSpec, szBuf, sizeof(szBuf));
314 RTLogLoggerEx(pLogger, 0, ~0U, "Log created: %s\n", szBuf);
315 RTLogLoggerEx(pLogger, 0, ~0U, "Executable: %s\n", szExecName);
316
317 /* executable and arguments - tricky and all platform specific. */
318# if defined(RT_OS_WINDOWS)
319 RTLogLoggerEx(pLogger, 0, ~0U, "Commandline: %ls\n", GetCommandLineW());
320
321# elif defined(RT_OS_SOLARIS)
322 psinfo_t psi;
323 char szArgFileBuf[80];
324 RTStrPrintf(szArgFileBuf, sizeof(szArgFileBuf), "/proc/%ld/psinfo", (long)getpid());
325 FILE* pFile = fopen(szArgFileBuf, "rb");
326 if (pFile)
327 {
328 if (fread(&psi, sizeof(psi), 1, pFile) == 1)
329 {
330# if 0 /* 100% safe:*/
331 RTLogLoggerEx(pLogger, 0, ~0U, "Args: %s\n", psi.pr_psargs);
332# else /* probably safe: */
333 const char * const *argv = (const char * const *)psi.pr_argv;
334 for (int iArg = 0; iArg < psi.pr_argc; iArg++)
335 RTLogLoggerEx(pLogger, 0, ~0U, "Arg[%d]: %s\n", iArg, argv[iArg]);
336# endif
337
338 }
339 fclose(pFile);
340 }
341
342# elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
343# ifdef RT_OS_LINUX
344 FILE *pFile = fopen("/proc/self/cmdline", "r");
345# else /* RT_OS_FREEBSD: */
346 FILE *pFile = fopen("/proc/curproc/cmdline", "r");
347# endif
348 if (pFile)
349 {
350 /* braindead */
351 unsigned iArg = 0;
352 char ch;
353 bool fNew = true;
354 while (!feof(pFile) && (ch = fgetc(pFile)) != EOF)
355 {
356 if (fNew)
357 {
358 RTLogLoggerEx(pLogger, 0, ~0U, "Arg[%u]: ", iArg++);
359 fNew = false;
360 }
361 if (ch)
362 RTLogLoggerEx(pLogger, 0, ~0U, "%c", ch);
363 else
364 {
365 RTLogLoggerEx(pLogger, 0, ~0U, "\n");
366 fNew = true;
367 }
368 }
369 if (!fNew)
370 RTLogLoggerEx(pLogger, 0, ~0U, "\n");
371 fclose(pFile);
372 }
373
374# elif defined(RT_OS_L4) || defined(RT_OS_OS2) || defined(RT_OS_DARWIN)
375 /* commandline? */
376# else
377# error needs porting.
378# endif
379 }
380
381# else /* IN_GUEST_R3 */
382 /* The user destination is backdoor logging. */
383 rc = RTLogCreate(&pLogger, 0, NULL, "VBOX_LOG",
384 RT_ELEMENTS(g_apszGroups), &g_apszGroups[0],
385 RTLOGDEST_USER, "VBox.log");
386# endif /* IN_GUEST_R3 */
387
388#else /* IN_RING0 */
389 rc = RTLogCreate(&pLogger, 0, NULL, "VBOX_LOG", RT_ELEMENTS(g_apszGroups), &g_apszGroups[0],
390# ifdef LOG_TO_BACKDOOR /** @todo look at guest ring 0 logging */
391 RTLOGDEST_USER,
392# else
393 RTLOGDEST_FILE,
394# endif
395 "VBox-ring0.log");
396 if (RT_SUCCESS(rc))
397 {
398 /*
399 * This is where you set your ring-0 logging preferences.
400 *
401 * On platforms which don't differ between debugger and kernel
402 * log printing, STDOUT is gonna be a stub and the DEBUGGER
403 * destination is the one doing all the work. On platforms
404 * that do differ (like Darwin), STDOUT is the kernel log.
405 */
406# if 0 /*defined(DEBUG_bird) && !defined(IN_GUEST)*/
407 //RTLogGroupSettings(pLogger, "all=~0 -default.l6.l5.l4.l3");
408 RTLogGroupSettings(pLogger, "all=0 pgm*=~0 gmm=~0");
409 RTLogFlags(pLogger, "enabled unbuffered pid tid");
410 pLogger->fDestFlags |= RTLOGDEST_DEBUGGER | RTLOGDEST_STDOUT;
411# endif
412# if defined(DEBUG_sandervl) && !defined(IN_GUEST)
413 RTLogGroupSettings(pLogger, "+all");
414 RTLogFlags(pLogger, "enabled unbuffered");
415 pLogger->fDestFlags |= RTLOGDEST_DEBUGGER;
416# endif
417# if defined(DEBUG_ramshankar) /* Guest ring-0 as well */
418 RTLogGroupSettings(pLogger, "+all.e.l.f");
419 RTLogFlags(pLogger, "enabled unbuffered");
420 pLogger->fDestFlags |= RTLOGDEST_DEBUGGER;
421# endif
422# if defined(DEBUG_aleksey) /* Guest ring-0 as well */
423 RTLogGroupSettings(pLogger, "+net_adp_drv.e.l.f+net_flt_drv.e.l.l2.l3.l4.f");
424 RTLogFlags(pLogger, "enabled unbuffered");
425 pLogger->fDestFlags |= RTLOGDEST_DEBUGGER | RTLOGDEST_STDOUT;
426# endif
427# if 0 /* defined(DEBUG_misha) */ /* Guest ring-0 as well */
428 RTLogGroupSettings(pLogger, "+net_flt_drv.e.l.f.l2+srv_intnet.e.l.f");
429 RTLogFlags(pLogger, "enabled unbuffered");
430 pLogger->fDestFlags |= RTLOGDEST_DEBUGGER;
431# endif
432# if 0 /* vboxdrv logging - ATTENTION: this is what we're refering to guys! Change to '# if 1'. */
433 RTLogGroupSettings(pLogger, "all=~0 -default.l6.l5.l4.l3");
434 RTLogFlags(pLogger, "enabled unbuffered tid");
435 pLogger->fDestFlags |= RTLOGDEST_DEBUGGER | RTLOGDEST_STDOUT;
436# endif
437 }
438#endif /* IN_RING0 */
439 return g_pLogger = RT_SUCCESS(rc) ? pLogger : NULL;
440}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette