VirtualBox

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

Last change on this file since 41561 was 40938, checked in by vboxsync, 13 years ago

runtime: backed out r77481,r77482,r77483,r77484,r77485

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