VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGFR3FlowTrace.cpp@ 87466

Last change on this file since 87466 was 87135, checked in by vboxsync, 4 years ago

VMM/DBGFR3FlowTrace: First commit of a new DBGF framework to collect guest state information during execution, allowing visualization of guest execution flow, bugref:8650 [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 67.9 KB
Line 
1/* $Id: DBGFR3FlowTrace.cpp 87135 2020-12-29 12:32:49Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Guest Execution Flow Tracing.
4 */
5
6/*
7 * Copyright (C) 2016 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/** @page pg_dbgf_cfg DBGFR3FlowTrace - Flow Trace Interface
20 *
21 * @todo
22 */
23
24
25/*********************************************************************************************************************************
26* Header Files *
27*********************************************************************************************************************************/
28#define LOG_GROUP LOG_GROUP_DBGF
29#include <VBox/vmm/dbgfflowtrace.h>
30#include "DBGFInternal.h"
31#include <VBox/vmm/mm.h>
32#include <VBox/vmm/uvm.h>
33#include <VBox/vmm/vm.h>
34#include <VBox/err.h>
35#include <VBox/log.h>
36
37#include <iprt/assert.h>
38#include <iprt/semaphore.h>
39#include <iprt/list.h>
40#include <iprt/time.h>
41
42
43/*********************************************************************************************************************************
44* Defined Constants And Macros *
45*********************************************************************************************************************************/
46
47
48
49/*********************************************************************************************************************************
50* Structures and Typedefs *
51*********************************************************************************************************************************/
52
53/** Pointer to the internal trace module instance data. */
54typedef struct DBGFFLOWTRACEMODINT *PDBGFFLOWTRACEMODINT;
55/** Pointer to a trace module probe location. */
56typedef struct DBGFFLOWTRACEMODPROBELOC *PDBGFFLOWTRACEMODPROBELOC;
57
58/**
59 * Internal probe instance data.
60 */
61typedef struct DBGFFLOWTRACEPROBEINT
62{
63 /** External and internal references hold. */
64 volatile uint32_t cRefs;
65 /** Trace modules referencing this probe. */
66 volatile uint32_t cRefsMod;
67 /** The user mode VM handle. */
68 PUVM pUVM;
69 /** Description of this probe. */
70 char *pszDescr;
71 /** Overall memory consumed for this probe for each invocation. */
72 size_t cbProbe;
73 /** Number of entries for this probe. */
74 uint32_t cEntries;
75 /** Maximum number of entries the array can hold. */
76 uint32_t cEntriesMax;
77 /** Pointer to the probe entry array. */
78 PDBGFFLOWTRACEPROBEENTRY paEntries;
79} DBGFFLOWTRACEPROBEINT;
80/** Pointer to the internal probe instance data. */
81typedef DBGFFLOWTRACEPROBEINT *PDBGFFLOWTRACEPROBEINT;
82/** Pointer to a constant internal probe instance data. */
83typedef const DBGFFLOWTRACEPROBEINT *PCDBGFFLOWTRACEPROBEINT;
84
85/**
86 * Record collected for one probe hit.
87 */
88typedef struct DBGFFLOWTRACERECORDINT
89{
90 /** Data list node. */
91 RTLISTNODE NdRecord;
92 /** The probe instance the record was created for. */
93 PDBGFFLOWTRACEPROBEINT pProbe;
94 /** The common probe instance data was collected for. */
95 PDBGFFLOWTRACEPROBEINT pProbeCmn;
96 /** Address of the probe location. */
97 DBGFADDRESS AddrProbe;
98 /** Reference counter. */
99 volatile uint32_t cRefs;
100 /** CPU ID this data was collected on. */
101 VMCPUID idCpu;
102 /** Sequence number for this data. */
103 uint64_t u64SeqNo;
104 /** Timestamp in nanoseconds when the data was collected. */
105 uint64_t u64TsCollected;
106 /** Pointer to the values for the common probe if available. */
107 PDBGFFLOWTRACEPROBEVAL paValCmn;
108 /** The probe values collected - size defined
109 * by the number of entries in the probe. */
110 DBGFFLOWTRACEPROBEVAL aVal[1];
111} DBGFFLOWTRACERECORDINT;
112/** Pointer to one collected probe data. */
113typedef DBGFFLOWTRACERECORDINT *PDBGFFLOWTRACERECORDINT;
114
115/**
116 * Trace module state.
117 */
118typedef enum DBGFFLOWTRACEMODSTATE
119{
120 /** Invalid state. */
121 DBGFFLOWTRACEMODSTATE_INVALID = 0,
122 /** The module was created. */
123 DBGFFLOWTRACEMODSTATE_CREATED,
124 /** The module is active, no probes can be added. */
125 DBGFFLOWTRACEMODSTATE_ENABLED,
126 /** The VM is destroyed but there are still references to the module,
127 * functionality is limited (query records only). */
128 DBGFFLOWTRACEMODSTATE_VM_DESTROYED,
129 /** The trace module is destroyed. */
130 DBGFFLOWTRACEMODSTATE_DESTROYED,
131 /** 32bit hack. */
132 DBGFFLOWTRACEMODSTATE_32BIT_HACK = 0x7fffffff
133} DBGFFLOWTRACEMODSTATE;
134
135/**
136 * Internal trace module instance data.
137 */
138typedef struct DBGFFLOWTRACEMODINT
139{
140 /** References hold for this trace module. */
141 volatile uint32_t cRefs;
142 /** The user mode VM handle. */
143 PUVM pUVM;
144 /** CPU ID the module is for. */
145 VMCPUID idCpu;
146 /** The DBGF owner handle. */
147 DBGFBPOWNER hBpOwner;
148 /** State of the trace module. */
149 volatile DBGFFLOWTRACEMODSTATE enmState;
150 /** Next free sequence number. */
151 volatile uint64_t u64SeqNoNext;
152 /** Optional ocmmon probe describing data to collect. */
153 PDBGFFLOWTRACEPROBEINT pProbeCmn;
154 /** Flags whether to record only a limited amount of data as indicated
155 * by cHitsLeft. */
156 bool fLimit;
157 /** Number of hits left until the module is disabled automatically. */
158 volatile uint32_t cHitsLeft;
159 /** Number of records to keep before evicting the oldest one. */
160 uint32_t cRecordsMax;
161 /** Number of records collected in this module. */
162 volatile uint32_t cRecords;
163 /** Number of probes in this trace module. */
164 uint32_t cProbes;
165 /** List of probes active for this module - DBGFFLOWTRACEMODPROBELOC. */
166 RTLISTANCHOR LstProbes;
167 /** List of collected data for this module. */
168 RTLISTANCHOR LstRecords;
169 /** Semaphore protecting access to the probe and record list. */
170 RTSEMFASTMUTEX hMtx;
171} DBGFFLOWTRACEMODINT;
172/** Pointer to a const internal trace module instance data. */
173typedef const DBGFFLOWTRACEMODINT *PCDBGFFLOWTRACEMODINT;
174
175/**
176 * Trace module probe location data.
177 */
178typedef struct DBGFFLOWTRACEMODPROBELOC
179{
180 /** List node for the list of probes. */
181 RTLISTNODE NdProbes;
182 /** The owning trace module. */
183 PDBGFFLOWTRACEMODINT pTraceMod;
184 /** The probe instance. */
185 PDBGFFLOWTRACEPROBEINT pProbe;
186 /** Address of the probe location. */
187 DBGFADDRESS AddrProbe;
188 /** The DBGF breakpoint handle. */
189 DBGFBP hBp;
190 /** Flags controlling the collection behavior for the probe. */
191 uint32_t fFlags;
192} DBGFFLOWTRACEMODPROBELOC;
193
194
195/**
196 * Flow trace report state.
197 */
198typedef struct DBGFFLOWTRACEREPORTINT
199{
200 /** The user mode VM handle. */
201 PUVM pUVM;
202 /** Reference count. */
203 volatile uint32_t cRefs;
204 /** Number of records. */
205 uint32_t cRecords;
206 /** Array with handle of records - variable in size. */
207 PDBGFFLOWTRACERECORDINT apRec[1];
208} DBGFFLOWTRACEMODREPORTINT;
209/** Pointer to the internal flow trace report state. */
210typedef DBGFFLOWTRACEREPORTINT *PDBGFFLOWTRACEREPORTINT;
211
212
213/*********************************************************************************************************************************
214* Internal Functions *
215*********************************************************************************************************************************/
216
217/**
218 * Creates a new trace record.
219 *
220 * @returns Pointer to the create flow trace record or NULL if out of memory.
221 * @param pProbeLoc The probe location to allocate the record for.
222 * @param idCpu The CPU ID this record was created for.
223 * @param ppbBuf Where to store the pointer to the data buffer for this probe.
224 * @param ppbBufCmn Where to store the pointer to the data buffer for the common probe
225 * if available.
226 */
227static PDBGFFLOWTRACERECORDINT dbgfR3FlowTraceRecordCreate(PDBGFFLOWTRACEMODPROBELOC pProbeLoc, VMCPUID idCpu,
228 uint8_t **ppbBuf, uint8_t **ppbBufCmn)
229{
230 PDBGFFLOWTRACEMODINT pTraceMod = pProbeLoc->pTraceMod;
231 PCDBGFFLOWTRACEPROBEINT pProbe = pProbeLoc->pProbe;
232 PCDBGFFLOWTRACEPROBEINT pProbeCmn = pTraceMod->pProbeCmn;
233 size_t cbProbeBuf = pProbe->cbProbe;
234 if (pProbeCmn)
235 cbProbeBuf += pProbeCmn->cbProbe;
236
237 *ppbBuf = NULL;
238 *ppbBufCmn = NULL;
239
240 PDBGFFLOWTRACERECORDINT pRecord = (PDBGFFLOWTRACERECORDINT)MMR3HeapAllocZU(pTraceMod->pUVM, MM_TAG_DBGF_FLOWTRACE,
241 sizeof(DBGFFLOWTRACERECORDINT) + cbProbeBuf);
242 if (RT_LIKELY(pRecord))
243 {
244 DBGFR3FlowTraceProbeRetain(pProbeLoc->pProbe);
245 if (pProbeLoc->pTraceMod->pProbeCmn)
246 DBGFR3FlowTraceProbeRetain(pProbeLoc->pTraceMod->pProbeCmn);
247
248 pRecord->pProbe = pProbeLoc->pProbe;
249 pRecord->pProbeCmn = pProbeLoc->pTraceMod->pProbeCmn;
250 pRecord->AddrProbe = pProbeLoc->AddrProbe;
251 pRecord->cRefs = 1;
252 pRecord->idCpu = idCpu;
253 pRecord->u64SeqNo = ASMAtomicIncU64(&pTraceMod->u64SeqNoNext);
254 pRecord->u64TsCollected = RTTimeNanoTS();
255 pRecord->paValCmn = NULL;
256
257 *ppbBuf = (uint8_t *)&pRecord->aVal[pProbe->cEntries];
258
259 if (pProbeCmn)
260 {
261 size_t offValCmn = pProbe->cbProbe - pProbe->cEntries * sizeof(DBGFFLOWTRACEPROBEVAL);
262 pRecord->paValCmn = (PDBGFFLOWTRACEPROBEVAL)(*ppbBuf + offValCmn);
263 *ppbBufCmn = (uint8_t *)&pRecord->paValCmn[pProbeCmn->cEntries];
264 }
265 }
266
267 return pRecord;
268}
269
270
271/**
272 * Destroys the given record.
273 *
274 * @returns nothing.
275 * @param pRecord The record to destroy.
276 */
277static void dbgfR3FlowTraceRecordDestroy(PDBGFFLOWTRACERECORDINT pRecord)
278{
279 DBGFR3FlowTraceProbeRelease(pRecord->pProbe);
280 pRecord->pProbe = NULL;
281 MMR3HeapFree(pRecord);
282}
283
284
285/**
286 * Creates a new flow trace report which can hold the given amount o records.
287 *
288 * @returns Pointer to the newly created report state or NULL if out of memory.
289 * @param pUVM The usermode VM handle.
290 * @param cRecords Number of records the report shoudld be able to hold.
291 */
292static PDBGFFLOWTRACEREPORTINT dbgfR3FlowTraceReportCreate(PUVM pUVM, uint32_t cRecords)
293{
294 PDBGFFLOWTRACEREPORTINT pReport = (PDBGFFLOWTRACEREPORTINT)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_FLOWTRACE,
295 RT_UOFFSETOF_DYN(DBGFFLOWTRACEREPORTINT, apRec[cRecords]));
296 if (RT_LIKELY(pReport))
297 {
298 pReport->pUVM = pUVM;
299 pReport->cRefs = 1;
300 pReport->cRecords = cRecords;
301 }
302
303 return pReport;
304}
305
306
307/**
308 * Destroys the given report releasing all references hold to the containing records.
309 *
310 * @returns nothing.
311 * @param pReport The report to destroy.
312 */
313static void dbgfR3FlowTraceReportDestroy(PDBGFFLOWTRACEREPORTINT pReport)
314{
315 for (uint32_t i = 0; i < pReport->cRecords; i++)
316 DBGFR3FlowTraceRecordRelease(pReport->apRec[i]);
317 MMR3HeapFree(pReport);
318}
319
320
321/**
322 * Queries the given register and returns the value as a guest pointer.
323 *
324 * @returns VBox status code.
325 * @param pUVM The usermode VM handle.
326 * @param idCpu VM CPU identifier.
327 * @param pszReg The register name to query.
328 * @param pGCPtr Where to store the register value on success.
329 */
330static int dbgfR3FlowTraceModProbeQueryRegAsGCPtr(PUVM pUVM, VMCPUID idCpu, const char *pszReg,
331 PRTGCPTR pGCPtr)
332{
333 DBGFREGVAL Val;
334 DBGFREGVALTYPE enmValType;
335 int rc = DBGFR3RegNmQuery(pUVM, idCpu, pszReg, &Val, &enmValType);
336 if (RT_SUCCESS(rc))
337 {
338 switch (enmValType)
339 {
340 case DBGFREGVALTYPE_U8:
341 *pGCPtr = Val.u8;
342 break;
343 case DBGFREGVALTYPE_U16:
344 *pGCPtr = Val.u16;
345 break;
346 case DBGFREGVALTYPE_U32:
347 *pGCPtr = Val.u32;
348 break;
349 case DBGFREGVALTYPE_U64:
350 *pGCPtr = Val.u64;
351 break;
352 case DBGFREGVALTYPE_U128:
353 case DBGFREGVALTYPE_R80:
354 case DBGFREGVALTYPE_DTR:
355 default:
356 rc = VERR_INVALID_PARAMETER;
357 }
358 }
359
360 return rc;
361}
362
363
364/**
365 * Resolves the guest address from an indirect memory probe entry.
366 *
367 * @returns VBox status code.
368 * @param pUVM The usermode VM handle.
369 * @param idCpu VM CPU identifier.
370 * @param pEntry The probe entry.
371 * @param pAddr Where to store the address on success.
372 */
373static int dbgfR3FlowTraceModProbeResolveIndirectAddr(PUVM pUVM, VMCPUID idCpu, PDBGFFLOWTRACEPROBEENTRY pEntry,
374 PDBGFADDRESS pAddr)
375{
376 Assert(pEntry->enmType == DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM);
377
378 RTGCPTR GCPtrBase = 0;
379 RTGCPTR GCPtrIndex = 0;
380 int rc = dbgfR3FlowTraceModProbeQueryRegAsGCPtr(pUVM, idCpu, pEntry->Type.IndirectMem.RegBase.pszName,
381 &GCPtrBase);
382 if ( RT_SUCCESS(rc)
383 && pEntry->Type.IndirectMem.RegIndex.pszName)
384 rc = dbgfR3FlowTraceModProbeQueryRegAsGCPtr(pUVM, idCpu, pEntry->Type.IndirectMem.RegIndex.pszName,
385 &GCPtrIndex);
386 if (RT_SUCCESS(rc))
387 {
388 RTGCPTR GCPtr = GCPtrBase + GCPtrIndex * pEntry->Type.IndirectMem.uScale;
389 DBGFR3AddrFromFlat(pUVM, pAddr, GCPtr);
390 if (pEntry->Type.IndirectMem.iOffset > 0)
391 DBGFR3AddrAdd(pAddr, pEntry->Type.IndirectMem.iOffset);
392 else if (pEntry->Type.IndirectMem.iOffset < 0)
393 DBGFR3AddrSub(pAddr, -pEntry->Type.IndirectMem.iOffset);
394 }
395
396 return rc;
397}
398
399
400/**
401 * Destroys the given flow trace module freeing all allocated resources.
402 *
403 * @returns nothing.
404 * @param pThis The flow trace module instance data.
405 */
406static void dbgfR3FlowTraceModDestroy(PDBGFFLOWTRACEMODINT pThis)
407{
408 if (ASMAtomicReadU32((volatile uint32_t *)&pThis->enmState) == DBGFFLOWTRACEMODSTATE_ENABLED)
409 {
410 int rc = DBGFR3FlowTraceModDisable(pThis);
411 AssertRC(rc);
412 }
413
414 Assert( pThis->enmState == DBGFFLOWTRACEMODSTATE_CREATED
415 || pThis->enmState == DBGFFLOWTRACEMODSTATE_VM_DESTROYED);
416
417 /* Do the cleanup under the semaphore. */
418 RTSemFastMutexRequest(pThis->hMtx);
419 if (pThis->pProbeCmn)
420 DBGFR3FlowTraceProbeRelease(pThis->pProbeCmn);
421
422 PDBGFFLOWTRACEMODPROBELOC pIt, pItNext;
423 RTListForEachSafe(&pThis->LstProbes, pIt, pItNext, DBGFFLOWTRACEMODPROBELOC, NdProbes)
424 {
425 RTListNodeRemove(&pIt->NdProbes);
426 ASMAtomicDecU32(&pIt->pProbe->cRefsMod);
427 DBGFR3FlowTraceProbeRelease(pIt->pProbe);
428 MMR3HeapFree(pIt);
429 }
430
431 PDBGFFLOWTRACERECORDINT pRecIt, pRecItNext;
432 RTListForEachSafe(&pThis->LstRecords, pRecIt, pRecItNext, DBGFFLOWTRACERECORDINT, NdRecord)
433 {
434 RTListNodeRemove(&pRecIt->NdRecord);
435 DBGFR3FlowTraceRecordRelease(pRecIt);
436 }
437
438 DBGFR3BpOwnerDestroy(pThis->pUVM, pThis->hBpOwner);
439 RTSemFastMutexRelease(pThis->hMtx);
440 RTSemFastMutexDestroy(pThis->hMtx);
441 MMR3HeapFree(pThis);
442}
443
444
445/**
446 * Checks whether the given basic block and address intersect.
447 *
448 * @returns true if they intersect, false otherwise.
449 * @param pAddr The address to check for.
450 * @param pAddrStart The start address.
451 * @param pAddrLast The last address.
452 */
453static bool dbgfR3FlowTraceAddrIntersect(PDBGFADDRESS pAddr, PDBGFADDRESS pAddrStart,
454 PDBGFADDRESS pAddrLast)
455{
456 return (pAddrStart->Sel == pAddr->Sel)
457 && (pAddrStart->off <= pAddr->off)
458 && (pAddrLast->off >= pAddr->off);
459}
460
461
462/**
463 * Matches a single value against a given filter value.
464 *
465 * @returns Flag whether the value matches against the single value.
466 * @param pVal The value to match.
467 * @param pValFilter The value filter to match against.
468 */
469static bool dbgfR3FlowTraceRecordMatchSingleValue(PCDBGFFLOWTRACEPROBEVAL pVal,
470 PCDBGFFLOWTRACEPROBEVAL pValFilter)
471{
472 if (pVal->pProbeEntry->enmType != pValFilter->pProbeEntry->enmType)
473 return false;
474
475 switch (pVal->pProbeEntry->enmType)
476 {
477 case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
478 {
479 if (pVal->Type.Reg.enmType != pValFilter->Type.Reg.enmType)
480 return false;
481
482 if (strcmp(pVal->Type.Reg.pszName, pValFilter->Type.Reg.pszName))
483 return false;
484
485 switch (pVal->Type.Reg.enmType)
486 {
487 case DBGFREGVALTYPE_U8:
488 if (pVal->Type.Reg.Val.u8 != pValFilter->Type.Reg.Val.u8)
489 return false;
490 break;
491 case DBGFREGVALTYPE_U16:
492 if (pVal->Type.Reg.Val.u16 != pValFilter->Type.Reg.Val.u16)
493 return false;
494 break;
495 case DBGFREGVALTYPE_U32:
496 if (pVal->Type.Reg.Val.u32 != pValFilter->Type.Reg.Val.u32)
497 return false;
498 break;
499 case DBGFREGVALTYPE_U64:
500 if (pVal->Type.Reg.Val.u64 != pValFilter->Type.Reg.Val.u64)
501 return false;
502 break;
503 case DBGFREGVALTYPE_U128:
504 if (memcmp(&pVal->Type.Reg.Val.u128, &pValFilter->Type.Reg.Val.u128,
505 sizeof(RTUINT128U)))
506 return false;
507 break;
508 case DBGFREGVALTYPE_R80:
509 if (memcmp(&pVal->Type.Reg.Val.r80Ex, &pValFilter->Type.Reg.Val.r80Ex,
510 sizeof(RTFLOAT80U2)))
511 return false;
512 break;
513 case DBGFREGVALTYPE_DTR:
514 if ( pVal->Type.Reg.Val.dtr.u64Base != pValFilter->Type.Reg.Val.dtr.u64Base
515 || pVal->Type.Reg.Val.dtr.u32Limit != pValFilter->Type.Reg.Val.dtr.u32Limit)
516 return false;
517 break;
518 default:
519 AssertFailed();
520 return false;
521 }
522 break;
523 }
524 case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
525 case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
526 if ( memcmp(&pVal->Type.Mem.Addr, &pValFilter->Type.Mem.Addr,
527 sizeof(DBGFADDRESS))
528 || pVal->Type.Mem.cbBuf != pValFilter->Type.Mem.cbBuf
529 || memcmp(pVal->Type.Mem.pvBuf, pValFilter->Type.Mem.pvBuf,
530 pValFilter->Type.Mem.cbBuf))
531 return false;
532 break;
533 default:
534 AssertFailed();
535 return false;
536 }
537
538 return true;
539}
540
541
542/**
543 * Matches the given values against the filter values returning a flag whether they match.
544 *
545 * @returns Flag whether the given values match the filter.
546 * @param paVal Pointer to the array of values.
547 * @param cVals Number of values in the array.
548 * @param paValFilter Pointer to the filter values.
549 * @param cValsFilter Number of entries in the filter values.
550 */
551static bool dbgfR3FlowTraceRecordMatchValues(PCDBGFFLOWTRACEPROBEVAL paVal, uint32_t cVals,
552 PCDBGFFLOWTRACEPROBEVAL paValFilter, uint32_t cValsFilter)
553{
554 bool fMatch = false;
555
556 /*
557 * The order in which the filters and values are doesn't need to match but for every filter
558 * there should be at least one entry matching.
559 */
560 while ( cValsFilter-- > 0
561 && fMatch)
562 {
563 for (uint32_t i = 0; i < cVals; i++)
564 {
565 fMatch = dbgfR3FlowTraceRecordMatchSingleValue(&paVal[i], paValFilter);
566 if (fMatch)
567 break;
568 }
569 paValFilter++;
570 }
571
572 return fMatch;
573}
574
575
576/**
577 * Checks the given record against the given filter, returning whether the filter
578 * matches.
579 *
580 * @returns Flag whether the record matches the given filter.
581 * @param pRecord The record to check.
582 * @param pFilter The filter to check against.
583 */
584static bool dbgfR3FlowTraceRecordMatchSingleFilter(PDBGFFLOWTRACERECORDINT pRecord,
585 PDBGFFLOWTRACEREPORTFILTER pFilter)
586{
587 bool fMatch = false;
588
589 switch (pFilter->enmType)
590 {
591 case DBGFFLOWTRACEREPORTFILTERTYPE_SEQ_NUM:
592 {
593 if ( pRecord->u64SeqNo >= pFilter->Type.SeqNo.u64SeqNoFirst
594 && pRecord->u64SeqNo <= pFilter->Type.SeqNo.u64SeqNoLast)
595 fMatch = true;
596 break;
597 }
598 case DBGFFLOWTRACEREPORTFILTERTYPE_TIMESTAMP:
599 {
600 if ( pRecord->u64TsCollected >= pFilter->Type.Timestamp.u64TsFirst
601 && pRecord->u64TsCollected <= pFilter->Type.Timestamp.u64TsLast)
602 fMatch = true;
603 break;
604 }
605 case DBGFFLOWTRACEREPORTFILTERTYPE_ADDR:
606 {
607 if (dbgfR3FlowTraceAddrIntersect(&pRecord->AddrProbe,
608 &pFilter->Type.Addr.AddrStart,
609 &pFilter->Type.Addr.AddrLast))
610 fMatch = true;
611 break;
612 }
613 case DBGFFLOWTRACEREPORTFILTERTYPE_VMCPU_ID:
614 {
615 if ( pRecord->idCpu >= pFilter->Type.VCpuId.idCpuStart
616 && pRecord->idCpu <= pFilter->Type.VCpuId.idCpuLast)
617 fMatch = true;
618 break;
619 }
620 case DBGFFLOWTRACEREPORTFILTERTYPE_PROBE_DATA:
621 {
622 if (pFilter->Type.ProbeData.fValCmn)
623 {
624 if (pRecord->paValCmn)
625 {
626 PCDBGFFLOWTRACEPROBEINT pProbeCmn = pRecord->pProbeCmn;
627 AssertPtr(pProbeCmn);
628
629 fMatch = dbgfR3FlowTraceRecordMatchValues(pRecord->paValCmn, pProbeCmn->cEntries,
630 pFilter->Type.ProbeData.paVal,
631 pFilter->Type.ProbeData.cVals);
632 }
633 }
634 else
635 fMatch = dbgfR3FlowTraceRecordMatchValues(&pRecord->aVal[0], pRecord->pProbe->cEntries,
636 pFilter->Type.ProbeData.paVal, pFilter->Type.ProbeData.cVals);
637 break;
638 }
639 default:
640 AssertMsgFailed(("Invalid filter type %u!\n", pFilter->enmType));
641 }
642
643 return fMatch;
644}
645
646
647/**
648 * Checks the given record against the given filters.
649 *
650 * @returns Flag whether the record matches the filters.
651 * @param pRecord The record to check.
652 * @param paFilters Array of filters to check.
653 * @param cFilters Number of filters in the array.
654 * @param enmOp How the record should match against the filters.
655 */
656static bool dbgfR3FlowTraceDoesRecordMatchFilter(PDBGFFLOWTRACERECORDINT pRecord,
657 PDBGFFLOWTRACEREPORTFILTER paFilters,
658 uint32_t cFilters, DBGFFLOWTRACEREPORTFILTEROP enmOp)
659{
660 bool fMatch = false;
661
662 if (enmOp == DBGFFLOWTRACEREPORTFILTEROP_AND)
663 {
664 fMatch = true;
665 while (cFilters-- > 0)
666 {
667 if (!dbgfR3FlowTraceRecordMatchSingleFilter(pRecord, &paFilters[cFilters]))
668 {
669 fMatch = false;
670 break;
671 }
672 }
673 }
674 else if (enmOp == DBGFFLOWTRACEREPORTFILTEROP_OR)
675 {
676 while (cFilters-- > 0)
677 {
678 if (dbgfR3FlowTraceRecordMatchSingleFilter(pRecord, &paFilters[cFilters]))
679 {
680 fMatch = true;
681 break;
682 }
683 }
684 }
685 else
686 AssertMsgFailed(("Invalid filter operation %u!\n", enmOp));
687
688 return fMatch;
689}
690
691
692/**
693 * Collects all the data specified in the given probe.
694 *
695 * @returns nothing.
696 * @param pUVM The user mode VM handle.
697 * @param idCpu The virtual CPU ID.
698 * @param pTraceMod The trace module instance.
699 * @param pAddrProbe Location of the probe, NULL if a common probe.
700 * @param pProbe The probe instance.
701 * @param pVal Pointer to the array of values to fill.
702 * @param pbBuf Poitner to the memory buffer holding additional data.
703 */
704static void dbgfR3FlowTraceModProbeCollectData(PUVM pUVM, VMCPUID idCpu,
705 PDBGFFLOWTRACEMODINT pTraceMod,
706 PCDBGFADDRESS pAddrProbe,
707 PDBGFFLOWTRACEPROBEINT pProbe,
708 PDBGFFLOWTRACEPROBEVAL pVal, uint8_t *pbBuf)
709{
710 for (uint32_t i = 0; i < pProbe->cEntries; i++)
711 {
712 int rc;
713 PDBGFFLOWTRACEPROBEENTRY pEntry = &pProbe->paEntries[i];
714
715 pVal->pProbeEntry = pEntry;
716
717 switch (pEntry->enmType)
718 {
719 case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
720 rc = DBGFR3RegNmQuery(pUVM, idCpu, pEntry->Type.Reg.pszName,
721 &pVal->Type.Reg.Val, &pVal->Type.Reg.enmType);
722 AssertRC(rc);
723 pVal->Type.Reg.pszName = pEntry->Type.Reg.pszName;
724 break;
725 case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
726 {
727 DBGFADDRESS Addr;
728 rc = dbgfR3FlowTraceModProbeResolveIndirectAddr(pUVM, idCpu, pEntry, &Addr);
729 if (RT_SUCCESS(rc))
730 {
731 pVal->Type.Mem.pvBuf = pbBuf;
732 pVal->Type.Mem.cbBuf = pEntry->Type.IndirectMem.cbMem;
733 pVal->Type.Mem.Addr = Addr;
734 rc = DBGFR3MemRead(pUVM, idCpu, &pVal->Type.Mem.Addr, pbBuf,
735 pVal->Type.Mem.cbBuf);
736 AssertRC(rc);
737 pbBuf += pVal->Type.Mem.cbBuf;
738 }
739 break;
740 }
741 case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
742 pVal->Type.Mem.pvBuf = pbBuf;
743 pVal->Type.Mem.cbBuf = pEntry->Type.ConstMem.cbMem;
744 pVal->Type.Mem.Addr = pEntry->Type.ConstMem.AddrMem;
745 rc = DBGFR3MemRead(pUVM, idCpu, &pVal->Type.Mem.Addr, pbBuf,
746 pVal->Type.Mem.cbBuf);
747 AssertRC(rc);
748 pbBuf += pVal->Type.Mem.cbBuf;
749 break;
750 case DBGFFLOWTRACEPROBEENTRYTYPE_CALLBACK:
751 rc = pEntry->Type.Callback.pfnCallback(pUVM, idCpu, pTraceMod,
752 pAddrProbe, pProbe, pEntry,
753 pEntry->Type.Callback.pvUser);
754 break;
755 default:
756 AssertFailed();
757 }
758
759 pVal++;
760 }
761}
762
763
764/**
765 * @callback_method_impl{FNDBGFBPHIT}
766 */
767static DECLCALLBACK(VBOXSTRICTRC) dbgfR3FlowTraceModProbeFiredWorker(PVM pVM, VMCPUID idCpu, void *pvUserBp, DBGFBP hBp, PCDBGFBPPUB pBpPub)
768{
769 RT_NOREF(pVM, hBp, pBpPub);
770 LogFlowFunc(("pVM=%#p idCpu=%u pvUserBp=%#p hBp=%#x pBpPub=%p\n",
771 pVM, idCpu, pvUserBp, hBp, pBpPub));
772
773 PDBGFFLOWTRACEMODPROBELOC pProbeLoc = (PDBGFFLOWTRACEMODPROBELOC)pvUserBp;
774 PDBGFFLOWTRACEPROBEINT pProbe = pProbeLoc->pProbe;
775 PDBGFFLOWTRACEMODINT pTraceMod = pProbeLoc->pTraceMod;
776 bool fDisabledModule = false;
777
778 /* Check whether the trace module is still active and we are tracing the correct VCPU. */
779 if (ASMAtomicReadU32((volatile uint32_t *)&pTraceMod->enmState) != DBGFFLOWTRACEMODSTATE_ENABLED
780 || ( idCpu != pTraceMod->idCpu
781 && pTraceMod->idCpu != VMCPUID_ANY))
782 return VINF_SUCCESS;
783
784 if ( pTraceMod->fLimit
785 && ASMAtomicReadU32(&pTraceMod->cHitsLeft))
786 {
787 uint32_t cHitsLeftNew = ASMAtomicDecU32(&pTraceMod->cHitsLeft);
788 if (cHitsLeftNew > cHitsLeftNew + 1) /* Underflow => reached the limit. */
789 {
790 ASMAtomicIncU32(&pTraceMod->cHitsLeft);
791 return VINF_SUCCESS;
792 }
793
794 if (!cHitsLeftNew)
795 {
796 /* We got the last record, disable the trace module. */
797 fDisabledModule = ASMAtomicCmpXchgU32((volatile uint32_t *)&pTraceMod->enmState, DBGFFLOWTRACEMODSTATE_CREATED,
798 DBGFFLOWTRACEMODSTATE_ENABLED);
799 }
800 }
801
802 uint8_t *pbBuf = NULL;
803 uint8_t *pbBufCmn = NULL;
804 PDBGFFLOWTRACERECORDINT pRecord = dbgfR3FlowTraceRecordCreate(pProbeLoc, idCpu, &pbBuf, &pbBufCmn);
805 if (pRecord)
806 {
807 dbgfR3FlowTraceModProbeCollectData(pTraceMod->pUVM, idCpu, pTraceMod, &pProbeLoc->AddrProbe, pProbe,
808 &pRecord->aVal[0], pbBuf);
809 if (pTraceMod->pProbeCmn)
810 dbgfR3FlowTraceModProbeCollectData(pTraceMod->pUVM, idCpu, pTraceMod, NULL, pTraceMod->pProbeCmn,
811 pRecord->paValCmn, pbBufCmn);
812
813 RTSemFastMutexRequest(pTraceMod->hMtx);
814 uint32_t cRecordsNew = ASMAtomicIncU32(&pTraceMod->cRecords);
815 RTListAppend(&pTraceMod->LstRecords, &pRecord->NdRecord);
816 if ( (cRecordsNew > pTraceMod->cRecordsMax)
817 && pTraceMod->cRecordsMax > 0)
818 {
819 /* Get the first record and destroy it. */
820 pRecord = RTListRemoveFirst(&pTraceMod->LstRecords, DBGFFLOWTRACERECORDINT, NdRecord);
821 AssertPtr(pRecord);
822 DBGFR3FlowTraceRecordRelease(pRecord);
823 ASMAtomicDecU32(&pTraceMod->cRecords);
824 }
825 RTSemFastMutexRelease(pTraceMod->hMtx);
826 }
827
828 if (fDisabledModule)
829 {
830 int rc = DBGFR3FlowTraceModDisable(pTraceMod);
831 AssertRC(rc);
832 }
833
834 return VINF_SUCCESS;
835}
836
837
838/**
839 * Worker for DBGFR3FlowTraceModEnable(), doing the work in an EMT rendezvous point to
840 * ensure no probe is hit in an inconsistent state.
841 *
842 * @returns Strict VBox status code.
843 * @param pVM The VM instance data.
844 * @param pVCpu The virtual CPU we execute on.
845 * @param pvUser Opaque user data.
846 */
847static DECLCALLBACK(VBOXSTRICTRC) dbgfR3FlowTraceModEnableWorker(PVM pVM, PVMCPU pVCpu, void *pvUser)
848{
849 RT_NOREF2(pVM, pVCpu);
850 PDBGFFLOWTRACEMODINT pThis = (PDBGFFLOWTRACEMODINT)pvUser;
851 PDBGFFLOWTRACEMODPROBELOC pProbeLoc = NULL;
852 int rc = VINF_SUCCESS;
853
854 pThis->enmState = DBGFFLOWTRACEMODSTATE_ENABLED;
855
856 RTListForEach(&pThis->LstProbes, pProbeLoc, DBGFFLOWTRACEMODPROBELOC, NdProbes)
857 {
858 rc = DBGFR3BpSetInt3Ex(pThis->pUVM, pThis->hBpOwner, pProbeLoc,
859 0 /*idSrcCpu*/, &pProbeLoc->AddrProbe,
860 0 /*iHitTrigger*/, ~0ULL /*iHitDisable*/, &pProbeLoc->hBp);
861 if (RT_FAILURE(rc))
862 break;
863 }
864
865 if (RT_FAILURE(rc))
866 pThis->enmState = DBGFFLOWTRACEMODSTATE_CREATED;
867
868 return rc;
869}
870
871
872/**
873 * Worker for DBGFR3FlowTraceModDisable(), doing the work in an EMT rendezvous point to
874 * ensure no probe is hit in an inconsistent state.
875 *
876 * @returns Struct VBox status code.
877 * @param pVM The VM instance data.
878 * @param pVCpu The virtual CPU we execute on.
879 * @param pvUser Opaque user data.
880 */
881static DECLCALLBACK(VBOXSTRICTRC) dbgfR3FlowTraceModDisableWorker(PVM pVM, PVMCPU pVCpu, void *pvUser)
882{
883 RT_NOREF2(pVM, pVCpu);
884 PDBGFFLOWTRACEMODINT pThis = (PDBGFFLOWTRACEMODINT)pvUser;
885 PDBGFFLOWTRACEMODPROBELOC pProbeLoc = NULL;
886 int rc = VINF_SUCCESS;
887
888 pThis->enmState = DBGFFLOWTRACEMODSTATE_CREATED;
889
890 RTListForEach(&pThis->LstProbes, pProbeLoc, DBGFFLOWTRACEMODPROBELOC, NdProbes)
891 {
892 rc = DBGFR3BpClear(pThis->pUVM, pProbeLoc->hBp);
893 AssertRC(rc);
894 }
895
896 return rc;
897}
898
899
900/**
901 * Checks whether both addresses are equal.
902 *
903 * @returns true if both addresses point to the same location, false otherwise.
904 * @param pAddr1 First address.
905 * @param pAddr2 Second address.
906 */
907static bool dbgfR3FlowTraceAddrEqual(PCDBGFADDRESS pAddr1, PCDBGFADDRESS pAddr2)
908{
909 return pAddr1->Sel == pAddr2->Sel
910 && pAddr1->off == pAddr2->off;
911}
912
913
914/**
915 * Returns the probe location pointer at the given address for the given trace module.
916 *
917 * @returns Pointer to the probe location or NULL if there is no probe at the given location.
918 * @param pThis The flow trace module instance data.
919 * @param pAddrProbe Address of the probe to check.
920 */
921static PDBGFFLOWTRACEMODPROBELOC dbgfR3TraceModGetProbeLocAtAddr(PDBGFFLOWTRACEMODINT pThis, PCDBGFADDRESS pAddrProbe)
922{
923 RTSemFastMutexRequest(pThis->hMtx);
924
925 PDBGFFLOWTRACEMODPROBELOC pIt;
926 RTListForEach(&pThis->LstProbes, pIt, DBGFFLOWTRACEMODPROBELOC, NdProbes)
927 {
928 if (dbgfR3FlowTraceAddrEqual(&pIt->AddrProbe, pAddrProbe))
929 {
930 RTSemFastMutexRelease(pThis->hMtx);
931 return pIt;
932 }
933 }
934 RTSemFastMutexRelease(pThis->hMtx);
935 return NULL;
936}
937
938
939/**
940 * Cleans up any allocated resources for each entry in the given probe for the given range.
941 *
942 * @returns nothing.
943 * @param pProbe The probe instance.
944 * @param idxStart Start index to clean up.
945 * @param cEntries How many entries to clean up.
946 */
947static void dbgfR3ProbeEntryCleanup(PDBGFFLOWTRACEPROBEINT pProbe, uint32_t idxStart, uint32_t cEntries)
948{
949 AssertReturnVoid(pProbe->cEntriesMax >= idxStart + cEntries);
950
951 for (uint32_t i = idxStart; i < idxStart + cEntries; i++)
952 {
953 PDBGFFLOWTRACEPROBEENTRY pEntry = &pProbe->paEntries[i];
954
955 switch (pEntry->enmType)
956 {
957 case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
958 if (pEntry->Type.Reg.pszName)
959 MMR3HeapFree((void *)pEntry->Type.Reg.pszName);
960 pEntry->Type.Reg.pszName = NULL;
961 break;
962 case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
963 pEntry->Type.ConstMem.cbMem = 0;
964 break;
965 case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
966 pEntry->Type.IndirectMem.uScale = 0;
967 pEntry->Type.IndirectMem.cbMem = 0;
968 if (pEntry->Type.IndirectMem.RegBase.pszName)
969 MMR3HeapFree((void *)pEntry->Type.IndirectMem.RegBase.pszName);
970 if (pEntry->Type.IndirectMem.RegIndex.pszName)
971 MMR3HeapFree((void *)pEntry->Type.IndirectMem.RegIndex.pszName);
972 pEntry->Type.IndirectMem.RegBase.pszName = NULL;
973 pEntry->Type.IndirectMem.RegIndex.pszName = NULL;
974 break;
975 case DBGFFLOWTRACEPROBEENTRYTYPE_CALLBACK:
976 pEntry->Type.Callback.pfnCallback = NULL;
977 pEntry->Type.Callback.pvUser = NULL;
978 break;
979 default:
980 AssertFailed();
981 }
982 }
983}
984
985
986/**
987 * Destroys the given flow trace probe freeing all allocated resources.
988 *
989 * @returns nothing.
990 * @param pProbe The flow trace probe instance data.
991 */
992static void dbgfR3FlowTraceProbeDestroy(PDBGFFLOWTRACEPROBEINT pProbe)
993{
994 dbgfR3ProbeEntryCleanup(pProbe, 0, pProbe->cEntries);
995 MMR3HeapFree(pProbe->paEntries);
996 MMR3HeapFree(pProbe);
997}
998
999
1000/**
1001 * Ensures that the given probe has the given amount of additional entries available,
1002 * increasing the size if necessary.
1003 *
1004 * @returns VBox status code.
1005 * @retval VERR_NO_MEMORY if increasing the size failed due to an out of memory condition.
1006 * @param pProbe The probe insatnce.
1007 * @param cEntriesAdd Number of additional entries required.
1008 */
1009static int dbgfR3ProbeEnsureSize(PDBGFFLOWTRACEPROBEINT pProbe, uint32_t cEntriesAdd)
1010{
1011 uint32_t cEntriesNew = pProbe->cEntries + cEntriesAdd;
1012 int rc = VINF_SUCCESS;
1013
1014 if (pProbe->cEntriesMax < cEntriesNew)
1015 {
1016 PDBGFFLOWTRACEPROBEENTRY paEntriesNew;
1017 if (!pProbe->cEntriesMax)
1018 paEntriesNew = (PDBGFFLOWTRACEPROBEENTRY)MMR3HeapAllocZU(pProbe->pUVM, MM_TAG_DBGF_FLOWTRACE,
1019 cEntriesNew * sizeof(DBGFFLOWTRACEPROBEENTRY));
1020 else
1021 paEntriesNew = (PDBGFFLOWTRACEPROBEENTRY)MMR3HeapRealloc(pProbe->paEntries,
1022 cEntriesNew * sizeof(DBGFFLOWTRACEPROBEENTRY));
1023 if (RT_LIKELY(paEntriesNew))
1024 {
1025 pProbe->paEntries = paEntriesNew;
1026 pProbe->cEntriesMax = cEntriesNew;
1027 }
1028 else
1029 rc = VERR_NO_MEMORY;
1030 }
1031
1032 return rc;
1033}
1034
1035
1036/**
1037 * Duplicates a probe registry entry.
1038 * @returns VBox status code.
1039 * @param pUVM The usermode VM handle.
1040 * @param pDst Where to copy the entry to.
1041 * @param pSrc What to copy.
1042 */
1043static int dbgfR3ProbeEntryRegDup(PUVM pUVM, PDBGFFLOWTRACEPROBEENTRYREG pDst, PCDBGFFLOWTRACEPROBEENTRYREG pSrc)
1044{
1045 int rc = VINF_SUCCESS;
1046
1047 pDst->enmType = pSrc->enmType;
1048 pDst->pszName = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_FLOWTRACE, pSrc->pszName);
1049 if (!pDst->pszName)
1050 rc = VERR_NO_MEMORY;
1051
1052 return rc;
1053}
1054
1055
1056/**
1057 * Duplicates a given probe entry in the given destination doing a deep copy (strings are duplicated).
1058 *
1059 * @returns VBox status code.
1060 * @param pUVM The usermode VM handle.
1061 * @param pDst Where to copy the entry to.
1062 * @param pSrc What to copy.
1063 */
1064static int dbgfR3ProbeEntryDup(PUVM pUVM, PDBGFFLOWTRACEPROBEENTRY pDst, PCDBGFFLOWTRACEPROBEENTRY pSrc)
1065{
1066 int rc = VINF_SUCCESS;
1067
1068 pDst->enmType = pSrc->enmType;
1069 pDst->pszDesc = NULL;
1070 if (pSrc->pszDesc)
1071 {
1072 pDst->pszDesc = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_FLOWTRACE, pSrc->pszDesc);
1073 if (!pDst->pszDesc)
1074 rc = VERR_NO_MEMORY;
1075 }
1076
1077 if (RT_SUCCESS(rc))
1078 {
1079 switch (pDst->enmType)
1080 {
1081 case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
1082 rc = dbgfR3ProbeEntryRegDup(pUVM, &pDst->Type.Reg, &pSrc->Type.Reg);
1083 break;
1084 case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
1085 pDst->Type.ConstMem.AddrMem = pSrc->Type.ConstMem.AddrMem;
1086 pDst->Type.ConstMem.cbMem = pSrc->Type.ConstMem.cbMem;
1087 break;
1088 case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
1089 pDst->Type.IndirectMem.uScale = pSrc->Type.IndirectMem.uScale;
1090 pDst->Type.IndirectMem.cbMem = pSrc->Type.IndirectMem.cbMem;
1091 pDst->Type.IndirectMem.iOffset = pSrc->Type.IndirectMem.iOffset;
1092 rc = dbgfR3ProbeEntryRegDup(pUVM, &pDst->Type.IndirectMem.RegBase, &pSrc->Type.IndirectMem.RegBase);
1093 if ( RT_SUCCESS(rc)
1094 && pDst->Type.IndirectMem.RegIndex.pszName)
1095 {
1096 rc = dbgfR3ProbeEntryRegDup(pUVM, &pDst->Type.IndirectMem.RegIndex, &pSrc->Type.IndirectMem.RegIndex);
1097 if (RT_FAILURE(rc))
1098 MMR3HeapFree((void *)pDst->Type.IndirectMem.RegBase.pszName);
1099 }
1100 break;
1101 case DBGFFLOWTRACEPROBEENTRYTYPE_CALLBACK:
1102 pDst->Type.Callback.pfnCallback = pSrc->Type.Callback.pfnCallback;
1103 pDst->Type.Callback.pvUser = pSrc->Type.Callback.pvUser;
1104 break;
1105 default:
1106 rc = VERR_INVALID_PARAMETER;
1107 }
1108 }
1109
1110 if ( RT_FAILURE(rc)
1111 && pDst->pszDesc)
1112 {
1113 MMR3HeapFree((void *)pDst->pszDesc);
1114 pDst->pszDesc = NULL;
1115 }
1116
1117 return rc;
1118}
1119
1120
1121/**
1122 * Recalculates the size occupied by the data of this probe for each invocation.
1123 *
1124 * @returns nothing.
1125 * @param pProbe The probe instance.
1126 */
1127static void dbgfR3ProbeRecalcSize(PDBGFFLOWTRACEPROBEINT pProbe)
1128{
1129 size_t cbProbe = 0;
1130
1131 for (uint32_t i = 0; i < pProbe->cEntries; i++)
1132 {
1133 PDBGFFLOWTRACEPROBEENTRY pEntry = &pProbe->paEntries[i];
1134
1135 cbProbe += sizeof(DBGFFLOWTRACEPROBEVAL);
1136
1137 switch (pEntry->enmType)
1138 {
1139 case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
1140 cbProbe += pEntry->Type.ConstMem.cbMem;
1141 break;
1142 case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
1143 cbProbe += pEntry->Type.IndirectMem.cbMem;
1144 break;
1145 case DBGFFLOWTRACEPROBEENTRYTYPE_CALLBACK:
1146 case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
1147 break;
1148 default:
1149 AssertFailed();
1150 }
1151 }
1152
1153 pProbe->cbProbe = cbProbe;
1154}
1155
1156
1157/**
1158 * Creates a new empty flow trace module.
1159 *
1160 * @returns VBox status code.
1161 * @param pUVM The usermode VM handle.
1162 * @param idCpu CPU ID the module is for, use VMCPUID_ANY for any CPU.
1163 * @param hFlowTraceProbeCommon Optional probe handle of data to capture regardless of the actual
1164 * probe.
1165 * @param phFlowTraceMod Where to store the handle to the created module on success.
1166 */
1167VMMR3DECL(int) DBGFR3FlowTraceModCreate(PUVM pUVM, VMCPUID idCpu,
1168 DBGFFLOWTRACEPROBE hFlowTraceProbeCommon,
1169 PDBGFFLOWTRACEMOD phFlowTraceMod)
1170{
1171 int rc = VINF_SUCCESS;
1172 PDBGFFLOWTRACEMODINT pThis = (PDBGFFLOWTRACEMODINT)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_FLOWTRACE,
1173 sizeof(DBGFFLOWTRACEMODINT));
1174 if (RT_LIKELY(pThis))
1175 {
1176 pThis->cRefs = 1;
1177 pThis->pUVM = pUVM;
1178 pThis->idCpu = idCpu;
1179 pThis->enmState = DBGFFLOWTRACEMODSTATE_CREATED;
1180 pThis->u64SeqNoNext = 0;
1181 pThis->cHitsLeft = 0;
1182 pThis->cRecordsMax = 0;
1183 pThis->cRecords = 0;
1184 pThis->cProbes = 0;
1185 RTListInit(&pThis->LstProbes);
1186 RTListInit(&pThis->LstRecords);
1187
1188 rc = RTSemFastMutexCreate(&pThis->hMtx);
1189 if (RT_SUCCESS(rc))
1190 {
1191 rc = DBGFR3BpOwnerCreate(pUVM, dbgfR3FlowTraceModProbeFiredWorker, &pThis->hBpOwner);
1192 if (RT_SUCCESS(rc))
1193 {
1194 PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbeCommon;
1195 if (pProbe)
1196 {
1197 DBGFR3FlowTraceProbeRetain(pProbe);
1198 pThis->pProbeCmn = pProbe;
1199 }
1200 }
1201
1202 *phFlowTraceMod = pThis;
1203 }
1204
1205 if (RT_FAILURE(rc))
1206 MMR3HeapFree(pThis);
1207 }
1208 else
1209 rc = VERR_NO_MEMORY;
1210
1211 return rc;
1212}
1213
1214
1215/**
1216 * Create a new flow trace module from the given control flow graph adding the given probes
1217 * at the entries, exits and branches.
1218 *
1219 * @returns VBox status code.
1220 * @param pUVM The usermode VM handle.
1221 * @param idCpu CPU ID the module is for, use VMCPUID_ANY for any CPU.
1222 * @param hFlow Control flow graph handle to use.
1223 * @param hFlowTraceProbeCommon Optional probe handle of data to capture regardless of the actual
1224 * probe.
1225 * @param hFlowTraceProbeEntry Probe handle to use for all entry blocks.
1226 * @param hFlowTraceProbeRegular Probe handle to use for all branches.
1227 * @param hFlowTraceProbeExit Probe handle to use for all exits.
1228 * @param phFlowTraceMod Where to store the handle to the created module on success.
1229 */
1230VMMR3DECL(int) DBGFR3FlowTraceModCreateFromFlowGraph(PUVM pUVM, VMCPUID idCpu, DBGFFLOW hFlow,
1231 DBGFFLOWTRACEPROBE hFlowTraceProbeCommon,
1232 DBGFFLOWTRACEPROBE hFlowTraceProbeEntry,
1233 DBGFFLOWTRACEPROBE hFlowTraceProbeRegular,
1234 DBGFFLOWTRACEPROBE hFlowTraceProbeExit,
1235 PDBGFFLOWTRACEMOD phFlowTraceMod)
1236{
1237 DBGFFLOWIT hFlowIt;
1238 int rc = DBGFR3FlowItCreate(hFlow, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hFlowIt);
1239 if (RT_SUCCESS(rc))
1240 {
1241 DBGFFLOWTRACEMOD hFlowTraceMod;
1242 rc = DBGFR3FlowTraceModCreate(pUVM, idCpu, hFlowTraceProbeCommon, &hFlowTraceMod);
1243 if (RT_SUCCESS(rc))
1244 {
1245 DBGFFLOWBB hFlowBb = DBGFR3FlowItNext(hFlowIt);
1246 while (hFlowBb && RT_SUCCESS(rc))
1247 {
1248 uint32_t fFlags = DBGFR3FlowBbGetFlags(hFlowBb);
1249
1250 if (!(fFlags & (DBGF_FLOW_BB_F_EMPTY | DBGF_FLOW_BB_F_INCOMPLETE_ERR)))
1251 {
1252 DBGFADDRESS AddrInstr;
1253
1254 if (fFlags & DBGF_FLOW_BB_F_ENTRY)
1255 {
1256 rc = DBGFR3FlowBbQueryInstr(hFlowBb, 0, &AddrInstr, NULL, NULL);
1257 AssertRC(rc);
1258
1259 rc = DBGFR3FlowTraceModAddProbe(hFlowTraceMod, &AddrInstr, hFlowTraceProbeEntry,
1260 DBGF_FLOW_TRACE_PROBE_ADD_F_BEFORE_EXEC);
1261 }
1262
1263 if (RT_SUCCESS(rc))
1264 {
1265 DBGFFLOWBBENDTYPE enmType = DBGFR3FlowBbGetType(hFlowBb);
1266 uint32_t cInstr = DBGFR3FlowBbGetInstrCount(hFlowBb);
1267 rc = DBGFR3FlowBbQueryInstr(hFlowBb, cInstr - 1, &AddrInstr, NULL, NULL);
1268 if (RT_SUCCESS(rc))
1269 {
1270 if (enmType == DBGFFLOWBBENDTYPE_EXIT)
1271 rc = DBGFR3FlowTraceModAddProbe(hFlowTraceMod, &AddrInstr, hFlowTraceProbeExit,
1272 DBGF_FLOW_TRACE_PROBE_ADD_F_AFTER_EXEC);
1273 else
1274 rc = DBGFR3FlowTraceModAddProbe(hFlowTraceMod, &AddrInstr, hFlowTraceProbeRegular,
1275 DBGF_FLOW_TRACE_PROBE_ADD_F_BEFORE_EXEC);
1276 }
1277 }
1278 }
1279
1280 hFlowBb = DBGFR3FlowItNext(hFlowIt);
1281 }
1282
1283 if (RT_FAILURE(rc))
1284 DBGFR3FlowTraceModRelease(hFlowTraceMod);
1285 else
1286 *phFlowTraceMod = hFlowTraceMod;
1287 }
1288
1289 DBGFR3FlowItDestroy(hFlowIt);
1290 }
1291
1292 return rc;
1293}
1294
1295
1296/**
1297 * Retain a reference to the given flow trace module.
1298 *
1299 * @returns New reference count.
1300 * @param hFlowTraceMod Flow trace module handle.
1301 */
1302VMMR3DECL(uint32_t) DBGFR3FlowTraceModRetain(DBGFFLOWTRACEMOD hFlowTraceMod)
1303{
1304 PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
1305 AssertPtrReturn(pThis, UINT32_MAX);
1306
1307 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
1308 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
1309 return cRefs;
1310}
1311
1312
1313/**
1314 * Release a reference of the given flow trace module.
1315 *
1316 * @returns New reference count, on 0 the module is destroyed and all containing records
1317 * are deleted.
1318 * @param hFlowTraceMod Flow trace module handle.
1319 */
1320VMMR3DECL(uint32_t) DBGFR3FlowTraceModRelease(DBGFFLOWTRACEMOD hFlowTraceMod)
1321{
1322 PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
1323 if (!pThis)
1324 return 0;
1325 AssertPtrReturn(pThis, UINT32_MAX);
1326
1327 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
1328 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
1329 if (cRefs == 0)
1330 dbgfR3FlowTraceModDestroy(pThis);
1331 return cRefs;
1332}
1333
1334
1335/**
1336 * Enables and arms all probes in the given flow trace module.
1337 *
1338 * @returns VBox status code.
1339 * @param hFlowTraceMod Flow trace module handle.
1340 * @param cHits Number of hits inside this module until the module is disabled
1341 * automatically, 0 if not to disable automatically.
1342 * @param cRecordsMax Maximum number of records to keep until the oldest is evicted.
1343 */
1344VMMR3DECL(int) DBGFR3FlowTraceModEnable(DBGFFLOWTRACEMOD hFlowTraceMod, uint32_t cHits, uint32_t cRecordsMax)
1345{
1346 PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
1347 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1348 AssertReturn(pThis->cProbes > 0, VERR_INVALID_STATE);
1349 AssertReturn(pThis->enmState == DBGFFLOWTRACEMODSTATE_CREATED, VERR_INVALID_STATE);
1350
1351 pThis->cHitsLeft = cHits;
1352 pThis->cRecordsMax = cRecordsMax;
1353
1354 return VMMR3EmtRendezvous(pThis->pUVM->pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
1355 dbgfR3FlowTraceModEnableWorker, pThis);
1356}
1357
1358
1359/**
1360 * Disables all probes in the given flow trace module.
1361 *
1362 * @returns VBox status code.
1363 * @param hFlowTraceMod Flow trace module handle.
1364 */
1365VMMR3DECL(int) DBGFR3FlowTraceModDisable(DBGFFLOWTRACEMOD hFlowTraceMod)
1366{
1367 PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
1368 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1369 AssertReturn(pThis->enmState == DBGFFLOWTRACEMODSTATE_ENABLED, VERR_INVALID_STATE);
1370
1371 return VMMR3EmtRendezvous(pThis->pUVM->pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
1372 dbgfR3FlowTraceModDisableWorker, pThis);
1373}
1374
1375
1376/**
1377 * Returns a report containing all existing records in the given flow trace module.
1378 *
1379 * @returns VBox status code.
1380 * @param hFlowTraceMod Flow trace module handle.
1381 * @param phFlowTraceReport Where to store the flow trace report handle on success.
1382 */
1383VMMR3DECL(int) DBGFR3FlowTraceModQueryReport(DBGFFLOWTRACEMOD hFlowTraceMod,
1384 PDBGFFLOWTRACEREPORT phFlowTraceReport)
1385{
1386 PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
1387 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1388 AssertPtrReturn(phFlowTraceReport, VERR_INVALID_POINTER);
1389
1390 /** @todo Locking. */
1391 int rc = VINF_SUCCESS;
1392 PDBGFFLOWTRACEREPORTINT pReport = dbgfR3FlowTraceReportCreate(pThis->pUVM, pThis->cRecords);
1393 if (RT_LIKELY(pReport))
1394 {
1395 PDBGFFLOWTRACERECORDINT pIt;
1396 uint32_t idx = 0;
1397
1398 RTSemFastMutexRequest(pThis->hMtx);
1399 RTListForEach(&pThis->LstRecords, pIt, DBGFFLOWTRACERECORDINT, NdRecord)
1400 {
1401 DBGFR3FlowTraceRecordRetain(pIt);
1402 pReport->apRec[idx++] = pIt;
1403 }
1404 RTSemFastMutexRelease(pThis->hMtx);
1405
1406 *phFlowTraceReport = pReport;
1407 }
1408 else
1409 rc = VERR_NO_MEMORY;
1410
1411 return rc;
1412}
1413
1414
1415/**
1416 * Clears all records contained in the flow trace module.
1417 *
1418 * @returns VBox status code.
1419 * @param hFlowTraceMod Flow trace module handle.
1420 */
1421VMMR3DECL(int) DBGFR3FlowTraceModClear(DBGFFLOWTRACEMOD hFlowTraceMod)
1422{
1423 PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
1424 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1425
1426 RTSemFastMutexRequest(pThis->hMtx);
1427 RTLISTANCHOR LstTmp;
1428 RTListMove(&LstTmp, &pThis->LstRecords);
1429 ASMAtomicWriteU32(&pThis->cRecords, 0);
1430 RTSemFastMutexRelease(pThis->hMtx);
1431
1432 PDBGFFLOWTRACERECORDINT pIt, pItNext;
1433 RTListForEachSafe(&LstTmp, pIt, pItNext, DBGFFLOWTRACERECORDINT, NdRecord)
1434 {
1435 RTListNodeRemove(&pIt->NdRecord);
1436 DBGFR3FlowTraceRecordRelease(pIt);
1437 }
1438
1439 return VINF_SUCCESS;
1440}
1441
1442
1443/**
1444 * Adds a new probe to the given flow trace module.
1445 *
1446 * @returns VBox status code
1447 * @retval VERR_INVALID_STATE if the probe is active or was destroyed already.
1448 * @retval VERR_ALREADY_EXISTS if there is already a probe at the specified location.
1449 * @param hFlowTraceMod Flow trace module handle.
1450 * @param pAddrProbe Guest address to insert the probe at.
1451 * @param hFlowTraceProbe The handle of the probe to insert.
1452 * @param fFlags Combination of DBGF_FLOW_TRACE_PROBE_ADD_F_*.
1453 */
1454VMMR3DECL(int) DBGFR3FlowTraceModAddProbe(DBGFFLOWTRACEMOD hFlowTraceMod, PCDBGFADDRESS pAddrProbe,
1455 DBGFFLOWTRACEPROBE hFlowTraceProbe, uint32_t fFlags)
1456{
1457 PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
1458 PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbe;
1459 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1460 AssertPtrReturn(pProbe, VERR_INVALID_HANDLE);
1461 AssertPtrReturn(pAddrProbe, VERR_INVALID_POINTER);
1462 AssertReturn(!(fFlags & ~DBGF_FLOW_TRACE_PROBE_ADD_F_VALID_MASK), VERR_INVALID_PARAMETER);
1463 AssertReturn(pThis->enmState == DBGFFLOWTRACEMODSTATE_CREATED, VERR_INVALID_STATE);
1464
1465 int rc = VINF_SUCCESS;
1466 PDBGFFLOWTRACEMODPROBELOC pProbeLoc = dbgfR3TraceModGetProbeLocAtAddr(pThis, pAddrProbe);
1467 if (!pProbeLoc)
1468 {
1469 pProbeLoc = (PDBGFFLOWTRACEMODPROBELOC)MMR3HeapAllocZU(pThis->pUVM, MM_TAG_DBGF_FLOWTRACE,
1470 sizeof(DBGFFLOWTRACEMODPROBELOC));
1471 if (RT_LIKELY(pProbeLoc))
1472 {
1473 pProbeLoc->pTraceMod = pThis;
1474 pProbeLoc->pProbe = pProbe;
1475 pProbeLoc->AddrProbe = *pAddrProbe;
1476 pProbeLoc->fFlags = fFlags;
1477 ASMAtomicIncU32(&pProbe->cRefs);
1478 ASMAtomicIncU32(&pProbe->cRefsMod);
1479 RTSemFastMutexRequest(pThis->hMtx);
1480 RTListAppend(&pThis->LstProbes, &pProbeLoc->NdProbes);
1481 pThis->cProbes++;
1482 RTSemFastMutexRelease(pThis->hMtx);
1483 }
1484 else
1485 rc = VERR_NO_MEMORY;
1486 }
1487 else
1488 rc = VERR_ALREADY_EXISTS;
1489
1490 return rc;
1491}
1492
1493
1494/**
1495 * Creates a new empty probe.
1496 *
1497 * @returns VBox status code.
1498 * @param pUVM The usermode VM handle.
1499 * @param pszDescr Description of the probe, optional.
1500 * @param phFlowTraceProbe Where to store the probe handle on success.
1501 */
1502VMMR3DECL(int) DBGFR3FlowTraceProbeCreate(PUVM pUVM, const char *pszDescr, PDBGFFLOWTRACEPROBE phFlowTraceProbe)
1503{
1504 int rc = VINF_SUCCESS;
1505 PDBGFFLOWTRACEPROBEINT pProbe = (PDBGFFLOWTRACEPROBEINT)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_FLOWTRACE,
1506 sizeof(DBGFFLOWTRACEPROBEINT));
1507 if (RT_LIKELY(pProbe))
1508 {
1509 pProbe->cRefs = 1;
1510 pProbe->cRefsMod = 0;
1511 pProbe->pUVM = pUVM;
1512 pProbe->cbProbe = 0;
1513 pProbe->cEntries = 0;
1514 pProbe->cEntriesMax = 0;
1515 pProbe->paEntries = NULL;
1516 pProbe->pszDescr = NULL;
1517 if (pszDescr)
1518 {
1519 pProbe->pszDescr = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_FLOWTRACE, pszDescr);
1520 if (!pProbe->pszDescr)
1521 {
1522 MMR3HeapFree(pProbe);
1523 rc = VERR_NO_MEMORY;
1524 }
1525 }
1526
1527 if (RT_SUCCESS(rc))
1528 *phFlowTraceProbe = pProbe;
1529 }
1530 else
1531 rc = VERR_NO_MEMORY;
1532
1533 return rc;
1534}
1535
1536
1537/**
1538 * Retains a reference to the probe.
1539 *
1540 * @returns New reference count.
1541 * @param hFlowTraceProbe Flow trace probe handle.
1542 */
1543VMMR3DECL(uint32_t) DBGFR3FlowTraceProbeRetain(DBGFFLOWTRACEPROBE hFlowTraceProbe)
1544{
1545 PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbe;
1546 AssertPtrReturn(pProbe, UINT32_MAX);
1547
1548 uint32_t cRefs = ASMAtomicIncU32(&pProbe->cRefs);
1549 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pProbe));
1550 return cRefs;
1551}
1552
1553
1554/**
1555 * Release a probe reference.
1556 *
1557 * @returns New reference count, on 0 the probe is destroyed.
1558 * @param hFlowTraceProbe Flow trace probe handle.
1559 */
1560VMMR3DECL(uint32_t) DBGFR3FlowTraceProbeRelease(DBGFFLOWTRACEPROBE hFlowTraceProbe)
1561{
1562 PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbe;
1563 if (!pProbe)
1564 return 0;
1565 AssertPtrReturn(pProbe, UINT32_MAX);
1566
1567 uint32_t cRefs = ASMAtomicDecU32(&pProbe->cRefs);
1568 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pProbe));
1569 if (cRefs == 0)
1570 dbgfR3FlowTraceProbeDestroy(pProbe);
1571 return cRefs;
1572}
1573
1574
1575/**
1576 * Adds new data to log in the given probe.
1577 *
1578 * @returns VBox status code.
1579 * @retval VERR_INVALID_STATE if the probe is already part of a trace module and it is not
1580 * possible to add new entries at this point.
1581 * @param hFlowTraceProbe Flow trace probe handle.
1582 * @param paEntries Pointer to the array of entry descriptors.
1583 * @param cEntries Number of entries in the array.
1584 */
1585VMMR3DECL(int) DBGFR3FlowTraceProbeEntriesAdd(DBGFFLOWTRACEPROBE hFlowTraceProbe,
1586 PCDBGFFLOWTRACEPROBEENTRY paEntries, uint32_t cEntries)
1587{
1588 PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbe;
1589 AssertPtrReturn(pProbe, VERR_INVALID_HANDLE);
1590 AssertPtrReturn(paEntries, VERR_INVALID_POINTER);
1591 AssertReturn(cEntries > 0, VERR_INVALID_PARAMETER);
1592 AssertReturn(!pProbe->cRefsMod, VERR_INVALID_STATE);
1593
1594 int rc = dbgfR3ProbeEnsureSize(pProbe, cEntries);
1595 if (RT_SUCCESS(rc))
1596 {
1597 uint32_t idxEntry;
1598
1599 for (idxEntry = 0; idxEntry < cEntries && RT_SUCCESS(rc); idxEntry++)
1600 {
1601 PCDBGFFLOWTRACEPROBEENTRY pEntry = &paEntries[idxEntry];
1602 PDBGFFLOWTRACEPROBEENTRY pProbeEntry = &pProbe->paEntries[pProbe->cEntries + idxEntry];
1603
1604 rc = dbgfR3ProbeEntryDup(pProbe->pUVM, pProbeEntry, pEntry);
1605 }
1606
1607 if (RT_FAILURE(rc))
1608 dbgfR3ProbeEntryCleanup(pProbe, pProbe->cEntries, idxEntry + 1);
1609 else
1610 {
1611 pProbe->cEntries += cEntries;
1612 dbgfR3ProbeRecalcSize(pProbe);
1613 }
1614 }
1615
1616 return rc;
1617}
1618
1619
1620/**
1621 * Retains a reference to the given flow trace report.
1622 *
1623 * @returns New reference count.
1624 * @param hFlowTraceReport Flow trace report handle.
1625 */
1626VMMR3DECL(uint32_t) DBGFR3FlowTraceReportRetain(DBGFFLOWTRACEREPORT hFlowTraceReport)
1627{
1628 PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
1629 AssertPtrReturn(pReport, UINT32_MAX);
1630
1631 uint32_t cRefs = ASMAtomicIncU32(&pReport->cRefs);
1632 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pReport));
1633 return cRefs;
1634}
1635
1636
1637/**
1638 * Releases a reference of the given flow trace report.
1639 *
1640 * @returns New reference count, on 0 the report is destroyed.
1641 * @param hFlowTraceReport Flow trace report handle.
1642 */
1643VMMR3DECL(uint32_t) DBGFR3FlowTraceReportRelease(DBGFFLOWTRACEREPORT hFlowTraceReport)
1644{
1645 PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
1646 if (!pReport)
1647 return 0;
1648 AssertPtrReturn(pReport, UINT32_MAX);
1649
1650 uint32_t cRefs = ASMAtomicDecU32(&pReport->cRefs);
1651 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pReport));
1652 if (cRefs == 0)
1653 dbgfR3FlowTraceReportDestroy(pReport);
1654 return cRefs;
1655}
1656
1657
1658/**
1659 * Returns the number of records in the given report.
1660 *
1661 * @returns Number of records.
1662 * @param hFlowTraceReport Flow trace report handle.
1663 */
1664VMMR3DECL(uint32_t) DBGFR3FlowTraceReportGetRecordCount(DBGFFLOWTRACEREPORT hFlowTraceReport)
1665{
1666 PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
1667 AssertPtrReturn(pReport, 0);
1668
1669 return pReport->cRecords;
1670}
1671
1672
1673/**
1674 * Filters the given flow trace report by the given criterias and returns a filtered report.
1675 *
1676 * @returns VBox status code.
1677 * @param hFlowTraceReport Flow trace report handle.
1678 * @param fFlags Combination of DBGF_FLOW_TRACE_REPORT_FILTER_F_*.
1679 * @param paFilters Pointer to the array of filters.
1680 * @param cFilters Number of entries in the filter array.
1681 * @param enmOp How the filters are connected to each other.
1682 * @param phFlowTraceReportFiltered Where to return the handle to the report containing the
1683 * filtered records on success.
1684 */
1685VMMR3DECL(int) DBGFR3FlowTraceReportQueryFiltered(DBGFFLOWTRACEREPORT hFlowTraceReport, uint32_t fFlags,
1686 PDBGFFLOWTRACEREPORTFILTER paFilters, uint32_t cFilters,
1687 DBGFFLOWTRACEREPORTFILTEROP enmOp,
1688 PDBGFFLOWTRACEREPORT phFlowTraceReportFiltered)
1689{
1690 PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
1691 AssertPtrReturn(pReport, VERR_INVALID_HANDLE);
1692 AssertReturn(!(fFlags & DBGF_FLOW_TRACE_REPORT_FILTER_F_VALID), VERR_INVALID_PARAMETER);
1693 AssertPtrReturn(paFilters, VERR_INVALID_POINTER);
1694 AssertReturn(cFilters > 0, VERR_INVALID_PARAMETER);
1695 AssertReturn(enmOp > DBGFFLOWTRACEREPORTFILTEROP_INVALID && enmOp <= DBGFFLOWTRACEREPORTFILTEROP_OR,
1696 VERR_INVALID_PARAMETER);
1697 AssertPtrReturn(phFlowTraceReportFiltered, VERR_INVALID_POINTER);
1698
1699 int rc = VINF_SUCCESS;
1700 PDBGFFLOWTRACEREPORTINT pReportFiltered = dbgfR3FlowTraceReportCreate(pReport->pUVM, pReport->cRecords);
1701 if (RT_LIKELY(pReport))
1702 {
1703 uint32_t idxFiltered = 0;
1704
1705 for (uint32_t i = 0; i < pReport->cRecords; i++)
1706 {
1707 PDBGFFLOWTRACERECORDINT pCur = pReport->apRec[i];
1708 bool fRecFilterMatch = dbgfR3FlowTraceDoesRecordMatchFilter(pCur, paFilters, cFilters, enmOp);
1709
1710 if ( ( fRecFilterMatch
1711 && !(fFlags & DBGF_FLOW_TRACE_REPORT_FILTER_F_REVERSE))
1712 || ( !fRecFilterMatch
1713 && (fFlags & DBGF_FLOW_TRACE_REPORT_FILTER_F_REVERSE)))
1714 {
1715 DBGFR3FlowTraceRecordRetain(pCur);
1716 pReportFiltered->apRec[idxFiltered++] = pCur;
1717 }
1718 }
1719
1720 pReportFiltered->cRecords = idxFiltered;
1721 *phFlowTraceReportFiltered = pReportFiltered;
1722 }
1723 else
1724 rc = VERR_NO_MEMORY;
1725
1726 return rc;
1727}
1728
1729
1730/**
1731 * Enumerates all records in the given flow trace report calling the supplied
1732 * enumeration callback.
1733 *
1734 * @returns VBox status code, return value of pfnEnum on error.
1735 * @param hFlowTraceReport Flow trace report handle.
1736 * @param pfnEnum The callback to call for every record.
1737 * @param pvUser Opaque user data to pass to the callback.
1738 */
1739VMMR3DECL(int) DBGFR3FlowTraceReportEnumRecords(DBGFFLOWTRACEREPORT hFlowTraceReport,
1740 PFNDBGFFLOWTRACEREPORTENUMCLBK pfnEnum,
1741 void *pvUser)
1742{
1743 PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
1744 AssertPtrReturn(pReport, VERR_INVALID_HANDLE);
1745
1746 int rc = VINF_SUCCESS;
1747 for (uint32_t i = 0; i < pReport->cRecords && RT_SUCCESS(rc); i++)
1748 rc = pfnEnum(pReport, pReport->apRec[i], pvUser);
1749
1750 return rc;
1751}
1752
1753
1754/**
1755 * Retains a reference to the given flow trace record handle.
1756 *
1757 * @returns New reference count.
1758 * @param hFlowTraceRecord The record handle to retain.
1759 */
1760VMMR3DECL(uint32_t) DBGFR3FlowTraceRecordRetain(DBGFFLOWTRACERECORD hFlowTraceRecord)
1761{
1762 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1763 AssertPtrReturn(pRecord, UINT32_MAX);
1764
1765 uint32_t cRefs = ASMAtomicIncU32(&pRecord->cRefs);
1766 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pRecord));
1767 return cRefs;
1768}
1769
1770
1771/**
1772 * Releases a reference of the given flow trace record.
1773 *
1774 * @returns New reference count, on 0 the record is destroyed.
1775 * @param hFlowTraceRecord Flow trace record handle.
1776 */
1777VMMR3DECL(uint32_t) DBGFR3FlowTraceRecordRelease(DBGFFLOWTRACERECORD hFlowTraceRecord)
1778{
1779 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1780 if (!pRecord)
1781 return 0;
1782 AssertPtrReturn(pRecord, UINT32_MAX);
1783
1784 uint32_t cRefs = ASMAtomicDecU32(&pRecord->cRefs);
1785 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pRecord));
1786 if (cRefs == 0)
1787 dbgfR3FlowTraceRecordDestroy(pRecord);
1788 return cRefs;
1789}
1790
1791
1792/**
1793 * Gets the sequence number of the given record handle.
1794 *
1795 * @returns Sequence number.
1796 * @param hFlowTraceRecord Flow trace record handle.
1797 */
1798VMMR3DECL(uint64_t) DBGFR3FlowTraceRecordGetSeqNo(DBGFFLOWTRACERECORD hFlowTraceRecord)
1799{
1800 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1801 AssertPtrReturn(pRecord, 0);
1802
1803 return pRecord->u64SeqNo;
1804}
1805
1806
1807/**
1808 * Returns the timestamp when the record was created.
1809 *
1810 * @returns Timestamp in nano seconds.
1811 * @param hFlowTraceRecord Flow trace record handle.
1812 */
1813VMMR3DECL(uint64_t) DBGFR3FlowTraceRecordGetTimestamp(DBGFFLOWTRACERECORD hFlowTraceRecord)
1814{
1815 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1816 AssertPtrReturn(pRecord, 0);
1817
1818 return pRecord->u64TsCollected;
1819}
1820
1821
1822/**
1823 * Gets the address in the guest the record was created.
1824 *
1825 * @returns Pointer to the address containing the guest location the record was created at.
1826 * @param hFlowTraceRecord Flow trace record handle.
1827 * @param pAddr Where to store the guest address.
1828 */
1829VMMR3DECL(PDBGFADDRESS) DBGFR3FlowTraceRecordGetAddr(DBGFFLOWTRACERECORD hFlowTraceRecord, PDBGFADDRESS pAddr)
1830{
1831 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1832 AssertPtrReturn(pRecord, NULL);
1833 AssertPtrReturn(pAddr, NULL);
1834
1835 *pAddr = pRecord->AddrProbe;
1836 return pAddr;
1837}
1838
1839
1840/**
1841 * Returns the handle to the probe for the given record.
1842 *
1843 * @returns Handle to the probe.
1844 * @param hFlowTraceRecord Flow trace record handle.
1845 */
1846VMMR3DECL(DBGFFLOWTRACEPROBE) DBGFR3FlowTraceRecordGetProbe(DBGFFLOWTRACERECORD hFlowTraceRecord)
1847{
1848 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1849 AssertPtrReturn(pRecord, NULL);
1850
1851 DBGFR3FlowTraceProbeRetain(pRecord->pProbe);
1852 return pRecord->pProbe;
1853}
1854
1855
1856/**
1857 * Returns the number of values contained in the record.
1858 *
1859 * @returns Number of values in the record.
1860 * @param hFlowTraceRecord Flow trace record handle.
1861 */
1862VMMR3DECL(uint32_t) DBGFR3FlowTraceRecordGetValCount(DBGFFLOWTRACERECORD hFlowTraceRecord)
1863{
1864 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1865 AssertPtrReturn(pRecord, 0);
1866
1867 return pRecord->pProbe->cEntries;
1868}
1869
1870
1871/**
1872 * Returns the number of values contained in the record.
1873 *
1874 * @returns Number of values in the record.
1875 * @param hFlowTraceRecord Flow trace record handle.
1876 */
1877VMMR3DECL(uint32_t) DBGFR3FlowTraceRecordGetValCommonCount(DBGFFLOWTRACERECORD hFlowTraceRecord)
1878{
1879 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1880 AssertPtrReturn(pRecord, 0);
1881
1882 return pRecord->pProbeCmn ? pRecord->pProbeCmn->cEntries : 0;
1883}
1884
1885
1886/**
1887 * Returns the values for the given record.
1888 *
1889 * @returns Pointer to the array of values.
1890 * @param hFlowTraceRecord Flow trace record handle.
1891 */
1892VMMR3DECL(PCDBGFFLOWTRACEPROBEVAL) DBGFR3FlowTraceRecordGetVals(DBGFFLOWTRACERECORD hFlowTraceRecord)
1893{
1894 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1895 AssertPtrReturn(pRecord, NULL);
1896
1897 return &pRecord->aVal[0];
1898}
1899
1900
1901/**
1902 * Returns data collected by the common probe for the trace module this record is in if one
1903 * is active.
1904 *
1905 * @returns Pointer to the array of common probe values or NULL if no common probe was specified
1906 * for the trace module.
1907 * @param hFlowTraceRecord Flow trace record handle.
1908 */
1909VMMR3DECL(PCDBGFFLOWTRACEPROBEVAL) DBGFR3FlowTraceRecordGetValsCommon(DBGFFLOWTRACERECORD hFlowTraceRecord)
1910{
1911 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1912 AssertPtrReturn(pRecord, NULL);
1913
1914 return pRecord->paValCmn;
1915}
1916
1917
1918/**
1919 * Returns the vCPU ID the record was created on.
1920 *
1921 * @returns vCPU ID.
1922 * @param hFlowTraceRecord Flow trace record handle.
1923 */
1924VMMR3DECL(VMCPUID) DBGFR3FlowTraceRecordGetCpuId(DBGFFLOWTRACERECORD hFlowTraceRecord)
1925{
1926 PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
1927 AssertPtrReturn(pRecord, VMCPUID_ANY);
1928
1929 return pRecord->idCpu;
1930}
1931
Note: See TracBrowser for help on using the repository browser.

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