1 | /* $Id: DBGFR3Trace.cpp 58126 2015-10-08 20:59:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGF - Debugger Facility, Tracing.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2015 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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DBGF
|
---|
23 | #include <VBox/vmm/dbgftrace.h>
|
---|
24 | #include <VBox/vmm/cfgm.h>
|
---|
25 | #include <VBox/vmm/mm.h>
|
---|
26 | #include <VBox/vmm/pdmapi.h>
|
---|
27 | #include "DBGFInternal.h"
|
---|
28 | #include <VBox/vmm/vm.h>
|
---|
29 | #include "VMMTracing.h"
|
---|
30 |
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <VBox/param.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/ctype.h>
|
---|
37 | #include <iprt/trace.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Internal Functions *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | static DECLCALLBACK(void) dbgfR3TraceInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | /**
|
---|
50 | * VMM trace point group translation table.
|
---|
51 | */
|
---|
52 | static const struct
|
---|
53 | {
|
---|
54 | /** The group name. */
|
---|
55 | const char *pszName;
|
---|
56 | /** The name length. */
|
---|
57 | uint32_t cchName;
|
---|
58 | /** The mask. */
|
---|
59 | uint32_t fMask;
|
---|
60 | } g_aVmmTpGroups[] =
|
---|
61 | {
|
---|
62 | { RT_STR_TUPLE("em"), VMMTPGROUP_EM },
|
---|
63 | { RT_STR_TUPLE("hm"), VMMTPGROUP_HM },
|
---|
64 | { RT_STR_TUPLE("tm"), VMMTPGROUP_TM },
|
---|
65 | };
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Initializes the tracing.
|
---|
70 | *
|
---|
71 | * @returns VBox status code
|
---|
72 | * @param pVM The cross context VM structure.
|
---|
73 | * @param cbEntry The trace entry size.
|
---|
74 | * @param cEntries The number of entries.
|
---|
75 | */
|
---|
76 | static int dbgfR3TraceEnable(PVM pVM, uint32_t cbEntry, uint32_t cEntries)
|
---|
77 | {
|
---|
78 | /*
|
---|
79 | * Don't enable it twice.
|
---|
80 | */
|
---|
81 | if (pVM->hTraceBufR3 != NIL_RTTRACEBUF)
|
---|
82 | return VERR_ALREADY_EXISTS;
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * Resolve default parameter values.
|
---|
86 | */
|
---|
87 | int rc;
|
---|
88 | if (!cbEntry)
|
---|
89 | {
|
---|
90 | rc = CFGMR3QueryU32Def(CFGMR3GetChild(CFGMR3GetRoot(pVM), "DBGF"), "TraceBufEntrySize", &cbEntry, 128);
|
---|
91 | AssertRCReturn(rc, rc);
|
---|
92 | }
|
---|
93 | if (!cEntries)
|
---|
94 | {
|
---|
95 | rc = CFGMR3QueryU32Def(CFGMR3GetChild(CFGMR3GetRoot(pVM), "DBGF"), "TraceBufEntries", &cEntries, 4096);
|
---|
96 | AssertRCReturn(rc, rc);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Figure the required size.
|
---|
101 | */
|
---|
102 | RTTRACEBUF hTraceBuf;
|
---|
103 | size_t cbBlock = 0;
|
---|
104 | rc = RTTraceBufCarve(&hTraceBuf, cEntries, cbEntry, 0 /*fFlags*/, NULL, &cbBlock);
|
---|
105 | if (rc != VERR_BUFFER_OVERFLOW)
|
---|
106 | {
|
---|
107 | AssertReturn(!RT_SUCCESS_NP(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Allocate a hyper heap block and carve a trace buffer out of it.
|
---|
113 | *
|
---|
114 | * Note! We ASSUME that the returned trace buffer handle has the same value
|
---|
115 | * as the heap block.
|
---|
116 | */
|
---|
117 | cbBlock = RT_ALIGN_Z(cbBlock, PAGE_SIZE);
|
---|
118 | void *pvBlock;
|
---|
119 | rc = MMR3HyperAllocOnceNoRel(pVM, cbBlock, PAGE_SIZE, MM_TAG_DBGF, &pvBlock);
|
---|
120 | if (RT_FAILURE(rc))
|
---|
121 | return rc;
|
---|
122 |
|
---|
123 | rc = RTTraceBufCarve(&hTraceBuf, cEntries, cbEntry, 0 /*fFlags*/, pvBlock, &cbBlock);
|
---|
124 | AssertRCReturn(rc, rc);
|
---|
125 | AssertRelease(hTraceBuf == (RTTRACEBUF)pvBlock && (void *)hTraceBuf == pvBlock);
|
---|
126 |
|
---|
127 | pVM->hTraceBufR3 = hTraceBuf;
|
---|
128 | pVM->hTraceBufR0 = MMHyperCCToR0(pVM, hTraceBuf);
|
---|
129 | pVM->hTraceBufRC = MMHyperCCToRC(pVM, hTraceBuf);
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Initializes the tracing.
|
---|
136 | *
|
---|
137 | * @returns VBox status code
|
---|
138 | * @param pVM The cross context VM structure.
|
---|
139 | */
|
---|
140 | int dbgfR3TraceInit(PVM pVM)
|
---|
141 | {
|
---|
142 | /*
|
---|
143 | * Initialize the trace buffer handles.
|
---|
144 | */
|
---|
145 | Assert(NIL_RTTRACEBUF == (RTTRACEBUF)NULL);
|
---|
146 | pVM->hTraceBufR3 = NIL_RTTRACEBUF;
|
---|
147 | pVM->hTraceBufRC = NIL_RTRCPTR;
|
---|
148 | pVM->hTraceBufR0 = NIL_RTR0PTR;
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Check the config and enable tracing if requested.
|
---|
152 | */
|
---|
153 | PCFGMNODE pDbgfNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "DBGF");
|
---|
154 | #if defined(DEBUG) || defined(RTTRACE_ENABLED)
|
---|
155 | bool const fDefault = false;
|
---|
156 | const char * const pszConfigDefault = "";
|
---|
157 | #else
|
---|
158 | bool const fDefault = false;
|
---|
159 | const char * const pszConfigDefault = "";
|
---|
160 | #endif
|
---|
161 | bool fTracingEnabled;
|
---|
162 | int rc = CFGMR3QueryBoolDef(pDbgfNode, "TracingEnabled", &fTracingEnabled, fDefault);
|
---|
163 | AssertRCReturn(rc, rc);
|
---|
164 | if (fTracingEnabled)
|
---|
165 | {
|
---|
166 | rc = dbgfR3TraceEnable(pVM, 0, 0);
|
---|
167 | if (RT_SUCCESS(rc))
|
---|
168 | {
|
---|
169 | if (pDbgfNode)
|
---|
170 | {
|
---|
171 | char *pszTracingConfig;
|
---|
172 | rc = CFGMR3QueryStringAllocDef(pDbgfNode, "TracingConfig", &pszTracingConfig, pszConfigDefault);
|
---|
173 | if (RT_SUCCESS(rc))
|
---|
174 | {
|
---|
175 | rc = DBGFR3TraceConfig(pVM, pszTracingConfig);
|
---|
176 | if (RT_FAILURE(rc))
|
---|
177 | rc = VMSetError(pVM, rc, RT_SRC_POS, "TracingConfig=\"%s\" -> %Rrc", pszTracingConfig, rc);
|
---|
178 | MMR3HeapFree(pszTracingConfig);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | else
|
---|
182 | {
|
---|
183 | rc = DBGFR3TraceConfig(pVM, pszConfigDefault);
|
---|
184 | if (RT_FAILURE(rc))
|
---|
185 | rc = VMSetError(pVM, rc, RT_SRC_POS, "TracingConfig=\"%s\" (default) -> %Rrc", pszConfigDefault, rc);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Register a debug info item that will dump the trace buffer content.
|
---|
192 | */
|
---|
193 | if (RT_SUCCESS(rc))
|
---|
194 | rc = DBGFR3InfoRegisterInternal(pVM, "tracebuf", "Display the trace buffer content. No arguments.", dbgfR3TraceInfo);
|
---|
195 |
|
---|
196 | return rc;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Terminates the tracing.
|
---|
202 | *
|
---|
203 | * @param pVM The cross context VM structure.
|
---|
204 | */
|
---|
205 | void dbgfR3TraceTerm(PVM pVM)
|
---|
206 | {
|
---|
207 | /* nothing to do */
|
---|
208 | NOREF(pVM);
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Relocates the trace buffer handle in RC.
|
---|
214 | *
|
---|
215 | * @param pVM The cross context VM structure.
|
---|
216 | */
|
---|
217 | void dbgfR3TraceRelocate(PVM pVM)
|
---|
218 | {
|
---|
219 | if (pVM->hTraceBufR3 != NIL_RTTRACEBUF)
|
---|
220 | pVM->hTraceBufRC = MMHyperCCToRC(pVM, pVM->hTraceBufR3);
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Change the traceing configuration of the VM.
|
---|
226 | *
|
---|
227 | * @returns VBox status code.
|
---|
228 | * @retval VINF_SUCCESS
|
---|
229 | * @retval VERR_NOT_FOUND if any of the trace point groups mentioned in the
|
---|
230 | * config string cannot be found. (Or if the string cannot be made
|
---|
231 | * sense of.) No change made.
|
---|
232 | * @retval VERR_INVALID_VM_HANDLE
|
---|
233 | * @retval VERR_INVALID_POINTER
|
---|
234 | *
|
---|
235 | * @param pVM The cross context VM structure.
|
---|
236 | * @param pszConfig The configuration change specification.
|
---|
237 | *
|
---|
238 | * Trace point group names, optionally prefixed by a '-' to
|
---|
239 | * indicate that the group is being disabled. A special
|
---|
240 | * group 'all' can be used to enable or disable all trace
|
---|
241 | * points.
|
---|
242 | *
|
---|
243 | * Drivers, devices and USB devices each have their own
|
---|
244 | * trace point group which can be accessed by prefixing
|
---|
245 | * their official PDM name by 'drv', 'dev' or 'usb'
|
---|
246 | * respectively.
|
---|
247 | */
|
---|
248 | VMMDECL(int) DBGFR3TraceConfig(PVM pVM, const char *pszConfig)
|
---|
249 | {
|
---|
250 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
251 | AssertPtrReturn(pszConfig, VERR_INVALID_POINTER);
|
---|
252 | if (pVM->hTraceBufR3 == NIL_RTTRACEBUF)
|
---|
253 | return VERR_DBGF_NO_TRACE_BUFFER;
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * We do this in two passes, the first pass just validates the input string
|
---|
257 | * and the second applies the changes.
|
---|
258 | */
|
---|
259 | for (uint32_t uPass = 0; uPass < 1; uPass++)
|
---|
260 | {
|
---|
261 | char ch;
|
---|
262 | while ((ch = *pszConfig) != '\0')
|
---|
263 | {
|
---|
264 | if (RT_C_IS_SPACE(ch))
|
---|
265 | continue;
|
---|
266 |
|
---|
267 | /*
|
---|
268 | * Operation prefix.
|
---|
269 | */
|
---|
270 | bool fNo = false;
|
---|
271 | do
|
---|
272 | {
|
---|
273 | if (ch == 'n' && pszConfig[1] == 'o')
|
---|
274 | {
|
---|
275 | fNo = !fNo;
|
---|
276 | pszConfig++;
|
---|
277 | }
|
---|
278 | else if (ch == '+')
|
---|
279 | fNo = false;
|
---|
280 | else if (ch == '-' || ch == '!' || ch == '~')
|
---|
281 | fNo = !fNo;
|
---|
282 | else
|
---|
283 | break;
|
---|
284 | } while ((ch = *++pszConfig) != '\0');
|
---|
285 | if (ch == '\0')
|
---|
286 | break;
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * Extract the name.
|
---|
290 | */
|
---|
291 | const char *pszName = pszConfig;
|
---|
292 | while ( ch != '\0'
|
---|
293 | && !RT_C_IS_SPACE(ch)
|
---|
294 | && !RT_C_IS_PUNCT(ch))
|
---|
295 | ch = *++pszConfig;
|
---|
296 | size_t const cchName = pszConfig - pszName;
|
---|
297 |
|
---|
298 | /*
|
---|
299 | * 'all' - special group that enables or disables all trace points.
|
---|
300 | */
|
---|
301 | if (cchName == 3 && !strncmp(pszName, "all", 3))
|
---|
302 | {
|
---|
303 | if (uPass != 0)
|
---|
304 | {
|
---|
305 | uint32_t iCpu = pVM->cCpus;
|
---|
306 | if (!fNo)
|
---|
307 | while (iCpu-- > 0)
|
---|
308 | pVM->aCpus[iCpu].fTraceGroups = UINT32_MAX;
|
---|
309 | else
|
---|
310 | while (iCpu-- > 0)
|
---|
311 | pVM->aCpus[iCpu].fTraceGroups = 0;
|
---|
312 | PDMR3TracingConfig(pVM, NULL, 0, !fNo, uPass > 0);
|
---|
313 | }
|
---|
314 | }
|
---|
315 | else
|
---|
316 | {
|
---|
317 | /*
|
---|
318 | * A specific group, try the VMM first then PDM.
|
---|
319 | */
|
---|
320 | uint32_t i = RT_ELEMENTS(g_aVmmTpGroups);
|
---|
321 | while (i-- > 0)
|
---|
322 | if ( g_aVmmTpGroups[i].cchName == cchName
|
---|
323 | && !strncmp(g_aVmmTpGroups[i].pszName, pszName, cchName))
|
---|
324 | {
|
---|
325 | if (uPass != 0)
|
---|
326 | {
|
---|
327 | uint32_t iCpu = pVM->cCpus;
|
---|
328 | if (!fNo)
|
---|
329 | while (iCpu-- > 0)
|
---|
330 | pVM->aCpus[iCpu].fTraceGroups |= g_aVmmTpGroups[i].fMask;
|
---|
331 | else
|
---|
332 | while (iCpu-- > 0)
|
---|
333 | pVM->aCpus[iCpu].fTraceGroups &= ~g_aVmmTpGroups[i].fMask;
|
---|
334 | }
|
---|
335 | break;
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (i == UINT32_MAX)
|
---|
339 | {
|
---|
340 | int rc = PDMR3TracingConfig(pVM, pszName, cchName, !fNo, uPass > 0);
|
---|
341 | if (RT_FAILURE(rc))
|
---|
342 | return rc;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | return VINF_SUCCESS;
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Query the trace configuration specification string.
|
---|
354 | *
|
---|
355 | * @returns VBox status code.
|
---|
356 | * @retval VINF_SUCCESS
|
---|
357 | * @retval VERR_INVALID_VM_HANDLE
|
---|
358 | * @retval VERR_INVALID_POINTER
|
---|
359 | * @retval VERR_BUFFER_OVERFLOW if the buffer is too small. Buffer will be
|
---|
360 | * empty.
|
---|
361 |
|
---|
362 | * @param pVM The cross context VM structure.
|
---|
363 | * @param pszConfig Pointer to the output buffer.
|
---|
364 | * @param cbConfig The size of the output buffer.
|
---|
365 | */
|
---|
366 | VMMDECL(int) DBGFR3TraceQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
|
---|
367 | {
|
---|
368 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
369 | AssertPtrReturn(pszConfig, VERR_INVALID_POINTER);
|
---|
370 | if (cbConfig < 1)
|
---|
371 | return VERR_BUFFER_OVERFLOW;
|
---|
372 | *pszConfig = '\0';
|
---|
373 |
|
---|
374 | if (pVM->hTraceBufR3 == NIL_RTTRACEBUF)
|
---|
375 | return VERR_DBGF_NO_TRACE_BUFFER;
|
---|
376 |
|
---|
377 | int rc = VINF_SUCCESS;
|
---|
378 | uint32_t const fTraceGroups = pVM->aCpus[0].fTraceGroups;
|
---|
379 | if ( fTraceGroups == UINT32_MAX
|
---|
380 | && PDMR3TracingAreAll(pVM, true /*fEnabled*/))
|
---|
381 | rc = RTStrCopy(pszConfig, cbConfig, "all");
|
---|
382 | else if ( fTraceGroups == 0
|
---|
383 | && PDMR3TracingAreAll(pVM, false /*fEnabled*/))
|
---|
384 | rc = RTStrCopy(pszConfig, cbConfig, "-all");
|
---|
385 | else
|
---|
386 | {
|
---|
387 | char *pszDst = pszConfig;
|
---|
388 | size_t cbDst = cbConfig;
|
---|
389 | uint32_t i = RT_ELEMENTS(g_aVmmTpGroups);
|
---|
390 | while (i-- > 0)
|
---|
391 | if (g_aVmmTpGroups[i].fMask & fTraceGroups)
|
---|
392 | {
|
---|
393 | size_t cchThis = g_aVmmTpGroups[i].cchName + (pszDst != pszConfig);
|
---|
394 | if (cchThis >= cbDst)
|
---|
395 | {
|
---|
396 | rc = VERR_BUFFER_OVERFLOW;
|
---|
397 | break;
|
---|
398 | }
|
---|
399 | if (pszDst != pszConfig)
|
---|
400 | {
|
---|
401 | *pszDst = ' ';
|
---|
402 | memcpy(pszDst + 1, g_aVmmTpGroups[i].pszName, g_aVmmTpGroups[i].cchName + 1);
|
---|
403 | }
|
---|
404 | else
|
---|
405 | memcpy(pszDst, g_aVmmTpGroups[i].pszName, g_aVmmTpGroups[i].cchName + 1);
|
---|
406 | pszDst += cchThis;
|
---|
407 | cbDst -= cchThis;
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (RT_SUCCESS(rc))
|
---|
411 | rc = PDMR3TracingQueryConfig(pVM, pszDst, cbDst);
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (RT_FAILURE(rc))
|
---|
415 | *pszConfig = '\0';
|
---|
416 | return rc;
|
---|
417 | }
|
---|
418 |
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * @callback_method_impl{FNRTTRACEBUFCALLBACK}
|
---|
422 | */
|
---|
423 | static DECLCALLBACK(int)
|
---|
424 | dbgfR3TraceInfoDumpEntry(RTTRACEBUF hTraceBuf, uint32_t iEntry, uint64_t NanoTS, RTCPUID idCpu, const char *pszMsg, void *pvUser)
|
---|
425 | {
|
---|
426 | PCDBGFINFOHLP pHlp = (PCDBGFINFOHLP)pvUser;
|
---|
427 | pHlp->pfnPrintf(pHlp, "#%04u/%'llu/%02x: %s\n", iEntry, NanoTS, idCpu, pszMsg);
|
---|
428 | NOREF(hTraceBuf);
|
---|
429 | return VINF_SUCCESS;
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * @callback_method_impl{FNDBGFHANDLERINT, Info handler for displaying the trace buffer content.}
|
---|
435 | */
|
---|
436 | static DECLCALLBACK(void) dbgfR3TraceInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
437 | {
|
---|
438 | RTTRACEBUF hTraceBuf = pVM->hTraceBufR3;
|
---|
439 | if (hTraceBuf == NIL_RTTRACEBUF)
|
---|
440 | pHlp->pfnPrintf(pHlp, "Tracing is disable\n");
|
---|
441 | else
|
---|
442 | {
|
---|
443 | pHlp->pfnPrintf(pHlp, "Trace buffer %p - %u entries of %u bytes\n",
|
---|
444 | hTraceBuf, RTTraceBufGetEntryCount(hTraceBuf), RTTraceBufGetEntrySize(hTraceBuf));
|
---|
445 | RTTraceBufEnumEntries(hTraceBuf, dbgfR3TraceInfoDumpEntry, (void *)pHlp);
|
---|
446 | }
|
---|
447 | NOREF(pszArgs);
|
---|
448 | }
|
---|
449 |
|
---|