1 | /* $Id: DBGFR3Flow.cpp 64589 2016-11-06 18:07:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGF - Debugger Facility, Control Flow Graph Interface (CFG).
|
---|
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 DBGFR3Flow - Control Flow Graph Interface
|
---|
20 | *
|
---|
21 | * The control flow graph interface provides an API to disassemble
|
---|
22 | * guest code providing the result in a control flow graph.
|
---|
23 | */
|
---|
24 |
|
---|
25 |
|
---|
26 | /*********************************************************************************************************************************
|
---|
27 | * Header Files *
|
---|
28 | *********************************************************************************************************************************/
|
---|
29 | #define LOG_GROUP LOG_GROUP_DBGF
|
---|
30 | #include <VBox/vmm/dbgf.h>
|
---|
31 | #include "DBGFInternal.h"
|
---|
32 | #include <VBox/vmm/mm.h>
|
---|
33 | #include <VBox/vmm/uvm.h>
|
---|
34 | #include <VBox/vmm/vm.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 |
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 | #include <iprt/param.h>
|
---|
41 | #include <iprt/list.h>
|
---|
42 | #include <iprt/mem.h>
|
---|
43 | #include <iprt/sort.h>
|
---|
44 | #include <iprt/strcache.h>
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Structures and Typedefs *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Internal control flow graph state.
|
---|
58 | */
|
---|
59 | typedef struct DBGFFLOWINT
|
---|
60 | {
|
---|
61 | /** Reference counter. */
|
---|
62 | uint32_t volatile cRefs;
|
---|
63 | /** Internal reference counter for basic blocks. */
|
---|
64 | uint32_t volatile cRefsBb;
|
---|
65 | /** Flags during creation. */
|
---|
66 | uint32_t fFlags;
|
---|
67 | /** List of all basic blocks. */
|
---|
68 | RTLISTANCHOR LstFlowBb;
|
---|
69 | /** List of identified branch tables. */
|
---|
70 | RTLISTANCHOR LstBranchTbl;
|
---|
71 | /** Number of basic blocks in this control flow graph. */
|
---|
72 | uint32_t cBbs;
|
---|
73 | /** Number of branch tables in this control flow graph. */
|
---|
74 | uint32_t cBranchTbls;
|
---|
75 | /** The lowest addres of a basic block. */
|
---|
76 | DBGFADDRESS AddrLowest;
|
---|
77 | /** The highest address of a basic block. */
|
---|
78 | DBGFADDRESS AddrHighest;
|
---|
79 | /** String cache for disassembled instructions. */
|
---|
80 | RTSTRCACHE hStrCacheInstr;
|
---|
81 | } DBGFFLOWINT;
|
---|
82 | /** Pointer to an internal control flow graph state. */
|
---|
83 | typedef DBGFFLOWINT *PDBGFFLOWINT;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Instruction record
|
---|
87 | */
|
---|
88 | typedef struct DBGFFLOWBBINSTR
|
---|
89 | {
|
---|
90 | /** Instruction address. */
|
---|
91 | DBGFADDRESS AddrInstr;
|
---|
92 | /** Size of instruction. */
|
---|
93 | uint32_t cbInstr;
|
---|
94 | /** Disassembled instruction string. */
|
---|
95 | const char *pszInstr;
|
---|
96 | } DBGFFLOWBBINSTR;
|
---|
97 | /** Pointer to an instruction record. */
|
---|
98 | typedef DBGFFLOWBBINSTR *PDBGFFLOWBBINSTR;
|
---|
99 |
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * A branch table identified by the graph processor.
|
---|
103 | */
|
---|
104 | typedef struct DBGFFLOWBRANCHTBLINT
|
---|
105 | {
|
---|
106 | /** Node for the list of branch tables. */
|
---|
107 | RTLISTNODE NdBranchTbl;
|
---|
108 | /** The owning control flow graph. */
|
---|
109 | PDBGFFLOWINT pFlow;
|
---|
110 | /** Reference counter. */
|
---|
111 | uint32_t volatile cRefs;
|
---|
112 | /** The general register index holding the bracnh table base. */
|
---|
113 | uint8_t idxGenRegBase;
|
---|
114 | /** Start address of the branch table. */
|
---|
115 | DBGFADDRESS AddrStart;
|
---|
116 | /** Number of valid entries in the branch table. */
|
---|
117 | uint32_t cSlots;
|
---|
118 | /** The addresses contained in the branch table - variable in size. */
|
---|
119 | DBGFADDRESS aAddresses[1];
|
---|
120 | } DBGFFLOWBRANCHTBLINT;
|
---|
121 | /** Pointer to a branch table structure. */
|
---|
122 | typedef DBGFFLOWBRANCHTBLINT *PDBGFFLOWBRANCHTBLINT;
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Internal control flow graph basic block state.
|
---|
127 | */
|
---|
128 | typedef struct DBGFFLOWBBINT
|
---|
129 | {
|
---|
130 | /** Node for the list of all basic blocks. */
|
---|
131 | RTLISTNODE NdFlowBb;
|
---|
132 | /** The control flow graph the basic block belongs to. */
|
---|
133 | PDBGFFLOWINT pFlow;
|
---|
134 | /** Reference counter. */
|
---|
135 | uint32_t volatile cRefs;
|
---|
136 | /** Basic block end type. */
|
---|
137 | DBGFFLOWBBENDTYPE enmEndType;
|
---|
138 | /** Start address of this basic block. */
|
---|
139 | DBGFADDRESS AddrStart;
|
---|
140 | /** End address of this basic block. */
|
---|
141 | DBGFADDRESS AddrEnd;
|
---|
142 | /** Address of the block succeeding.
|
---|
143 | * This is valid for conditional jumps
|
---|
144 | * (the other target is referenced by AddrEnd+1) and
|
---|
145 | * unconditional jumps (not ret, iret, etc.) except
|
---|
146 | * if we can't infer the jump target (jmp *eax for example). */
|
---|
147 | DBGFADDRESS AddrTarget;
|
---|
148 | /** The indirect branch table identified for indirect branches. */
|
---|
149 | PDBGFFLOWBRANCHTBLINT pFlowBranchTbl;
|
---|
150 | /** Last status error code if DBGF_FLOW_BB_F_INCOMPLETE_ERR is set. */
|
---|
151 | int rcError;
|
---|
152 | /** Error message if DBGF_FLOW_BB_F_INCOMPLETE_ERR is set. */
|
---|
153 | char *pszErr;
|
---|
154 | /** Flags for this basic block. */
|
---|
155 | uint32_t fFlags;
|
---|
156 | /** Number of instructions in this basic block. */
|
---|
157 | uint32_t cInstr;
|
---|
158 | /** Maximum number of instruction records for this basic block. */
|
---|
159 | uint32_t cInstrMax;
|
---|
160 | /** Instruction records, variable in size. */
|
---|
161 | DBGFFLOWBBINSTR aInstr[1];
|
---|
162 | } DBGFFLOWBBINT;
|
---|
163 | /** Pointer to an internal control flow graph basic block state. */
|
---|
164 | typedef DBGFFLOWBBINT *PDBGFFLOWBBINT;
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Control flow graph iterator state.
|
---|
169 | */
|
---|
170 | typedef struct DBGFFLOWITINT
|
---|
171 | {
|
---|
172 | /** Pointer to the control flow graph (holding a reference). */
|
---|
173 | PDBGFFLOWINT pFlow;
|
---|
174 | /** Next basic block to return. */
|
---|
175 | uint32_t idxBbNext;
|
---|
176 | /** Array of basic blocks sorted by the specified order - variable in size. */
|
---|
177 | PDBGFFLOWBBINT apBb[1];
|
---|
178 | } DBGFFLOWITINT;
|
---|
179 | /** Pointer to the internal control flow graph iterator state. */
|
---|
180 | typedef DBGFFLOWITINT *PDBGFFLOWITINT;
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Control flow graph branch table iterator state.
|
---|
185 | */
|
---|
186 | typedef struct DBGFFLOWBRANCHTBLITINT
|
---|
187 | {
|
---|
188 | /** Pointer to the control flow graph (holding a reference). */
|
---|
189 | PDBGFFLOWINT pFlow;
|
---|
190 | /** Next branch table to return. */
|
---|
191 | uint32_t idxTblNext;
|
---|
192 | /** Array of branch table pointers sorted by the specified order - variable in size. */
|
---|
193 | PDBGFFLOWBRANCHTBLINT apBranchTbl[1];
|
---|
194 | } DBGFFLOWBRANCHTBLITINT;
|
---|
195 | /** Pointer to the internal control flow graph branch table iterator state. */
|
---|
196 | typedef DBGFFLOWBRANCHTBLITINT *PDBGFFLOWBRANCHTBLITINT;
|
---|
197 |
|
---|
198 | /*********************************************************************************************************************************
|
---|
199 | * Internal Functions *
|
---|
200 | *********************************************************************************************************************************/
|
---|
201 |
|
---|
202 | static uint32_t dbgfR3FlowBbReleaseInt(PDBGFFLOWBBINT pFlowBb, bool fMayDestroyFlow);
|
---|
203 | static void dbgfR3FlowBranchTblDestroy(PDBGFFLOWBRANCHTBLINT pFlowBranchTbl);
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Checks whether both addresses are equal.
|
---|
208 | *
|
---|
209 | * @returns true if both addresses point to the same location, false otherwise.
|
---|
210 | * @param pAddr1 First address.
|
---|
211 | * @param pAddr2 Second address.
|
---|
212 | */
|
---|
213 | static bool dbgfR3FlowAddrEqual(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
|
---|
214 | {
|
---|
215 | return pAddr1->Sel == pAddr2->Sel
|
---|
216 | && pAddr1->off == pAddr2->off;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Checks whether the first given address is lower than the second one.
|
---|
222 | *
|
---|
223 | * @returns true if both addresses point to the same location, false otherwise.
|
---|
224 | * @param pAddr1 First address.
|
---|
225 | * @param pAddr2 Second address.
|
---|
226 | */
|
---|
227 | static bool dbgfR3FlowAddrLower(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
|
---|
228 | {
|
---|
229 | return pAddr1->Sel == pAddr2->Sel
|
---|
230 | && pAddr1->off < pAddr2->off;
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Checks whether the given basic block and address intersect.
|
---|
236 | *
|
---|
237 | * @returns true if they intersect, false otherwise.
|
---|
238 | * @param pFlowBb The basic block to check.
|
---|
239 | * @param pAddr The address to check for.
|
---|
240 | */
|
---|
241 | static bool dbgfR3FlowAddrIntersect(PDBGFFLOWBBINT pFlowBb, PDBGFADDRESS pAddr)
|
---|
242 | {
|
---|
243 | return (pFlowBb->AddrStart.Sel == pAddr->Sel)
|
---|
244 | && (pFlowBb->AddrStart.off <= pAddr->off)
|
---|
245 | && (pFlowBb->AddrEnd.off >= pAddr->off);
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Returns the distance of the two given addresses.
|
---|
251 | *
|
---|
252 | * @returns Distance of the addresses.
|
---|
253 | * @param pAddr1 The first address.
|
---|
254 | * @param pAddr2 The second address.
|
---|
255 | */
|
---|
256 | static RTGCUINTPTR dbgfR3FlowAddrGetDistance(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
|
---|
257 | {
|
---|
258 | if (pAddr1->Sel == pAddr2->Sel)
|
---|
259 | {
|
---|
260 | if (pAddr1->off >= pAddr2->off)
|
---|
261 | return pAddr1->off - pAddr2->off;
|
---|
262 | else
|
---|
263 | return pAddr2->off - pAddr1->off;
|
---|
264 | }
|
---|
265 | else
|
---|
266 | AssertFailed();
|
---|
267 |
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Creates a new basic block.
|
---|
274 | *
|
---|
275 | * @returns Pointer to the basic block on success or NULL if out of memory.
|
---|
276 | * @param pThis The control flow graph.
|
---|
277 | * @param pAddrStart The start of the basic block.
|
---|
278 | * @param fFlowBbFlags Additional flags for this bascic block.
|
---|
279 | * @param cInstrMax Maximum number of instructions this block can hold initially.
|
---|
280 | */
|
---|
281 | static PDBGFFLOWBBINT dbgfR3FlowBbCreate(PDBGFFLOWINT pThis, PDBGFADDRESS pAddrStart, uint32_t fFlowBbFlags,
|
---|
282 | uint32_t cInstrMax)
|
---|
283 | {
|
---|
284 | PDBGFFLOWBBINT pFlowBb = (PDBGFFLOWBBINT)RTMemAllocZ(RT_OFFSETOF(DBGFFLOWBBINT, aInstr[cInstrMax]));
|
---|
285 | if (RT_LIKELY(pFlowBb))
|
---|
286 | {
|
---|
287 | RTListInit(&pFlowBb->NdFlowBb);
|
---|
288 | pFlowBb->cRefs = 1;
|
---|
289 | pFlowBb->enmEndType = DBGFFLOWBBENDTYPE_INVALID;
|
---|
290 | pFlowBb->pFlow = pThis;
|
---|
291 | pFlowBb->fFlags = DBGF_FLOW_BB_F_EMPTY | fFlowBbFlags;
|
---|
292 | pFlowBb->AddrStart = *pAddrStart;
|
---|
293 | pFlowBb->AddrEnd = *pAddrStart;
|
---|
294 | pFlowBb->rcError = VINF_SUCCESS;
|
---|
295 | pFlowBb->pszErr = NULL;
|
---|
296 | pFlowBb->cInstr = 0;
|
---|
297 | pFlowBb->cInstrMax = cInstrMax;
|
---|
298 | pFlowBb->pFlowBranchTbl = NULL;
|
---|
299 | ASMAtomicIncU32(&pThis->cRefsBb);
|
---|
300 | }
|
---|
301 |
|
---|
302 | return pFlowBb;
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Creates an empty branch table with the given size.
|
---|
308 | *
|
---|
309 | * @returns Pointer to the empty branch table on success or NULL if out of memory.
|
---|
310 | * @param pThis The control flow graph.
|
---|
311 | * @param pAddrStart The start of the branch table.
|
---|
312 | * @param idxGenRegBase The general register index holding the base address.
|
---|
313 | * @param cSlots Number of slots the table has.
|
---|
314 | */
|
---|
315 | static PDBGFFLOWBRANCHTBLINT dbgfR3FlowBranchTblCreate(PDBGFFLOWINT pThis, PDBGFADDRESS pAddrStart, uint8_t idxGenRegBase, uint32_t cSlots)
|
---|
316 | {
|
---|
317 | PDBGFFLOWBRANCHTBLINT pBranchTbl = (PDBGFFLOWBRANCHTBLINT)RTMemAllocZ(RT_OFFSETOF(DBGFFLOWBRANCHTBLINT, aAddresses[cSlots]));
|
---|
318 | if (RT_LIKELY(pBranchTbl))
|
---|
319 | {
|
---|
320 | RTListInit(&pBranchTbl->NdBranchTbl);
|
---|
321 | pBranchTbl->pFlow = pThis;
|
---|
322 | pBranchTbl->idxGenRegBase = idxGenRegBase;
|
---|
323 | pBranchTbl->AddrStart = *pAddrStart;
|
---|
324 | pBranchTbl->cSlots = cSlots;
|
---|
325 | pBranchTbl->cRefs = 1;
|
---|
326 | }
|
---|
327 |
|
---|
328 | return pBranchTbl;
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Destroys a control flow graph.
|
---|
334 | *
|
---|
335 | * @returns nothing.
|
---|
336 | * @param pThis The control flow graph to destroy.
|
---|
337 | */
|
---|
338 | static void dbgfR3FlowDestroy(PDBGFFLOWINT pThis)
|
---|
339 | {
|
---|
340 | /* Defer destruction if there are still basic blocks referencing us. */
|
---|
341 | PDBGFFLOWBBINT pFlowBb = NULL;
|
---|
342 | PDBGFFLOWBBINT pFlowBbNext = NULL;
|
---|
343 | RTListForEachSafe(&pThis->LstFlowBb, pFlowBb, pFlowBbNext, DBGFFLOWBBINT, NdFlowBb)
|
---|
344 | {
|
---|
345 | dbgfR3FlowBbReleaseInt(pFlowBb, false /*fMayDestroyFlow*/);
|
---|
346 | }
|
---|
347 |
|
---|
348 | Assert(!pThis->cRefs);
|
---|
349 | if (!pThis->cRefsBb)
|
---|
350 | {
|
---|
351 | /* Destroy the branch tables. */
|
---|
352 | PDBGFFLOWBRANCHTBLINT pTbl = NULL;
|
---|
353 | PDBGFFLOWBRANCHTBLINT pTblNext = NULL;
|
---|
354 | RTListForEachSafe(&pThis->LstBranchTbl, pTbl, pTblNext, DBGFFLOWBRANCHTBLINT, NdBranchTbl)
|
---|
355 | {
|
---|
356 | dbgfR3FlowBranchTblDestroy(pTbl);
|
---|
357 | }
|
---|
358 |
|
---|
359 | RTStrCacheDestroy(pThis->hStrCacheInstr);
|
---|
360 | RTMemFree(pThis);
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Destroys a basic block.
|
---|
367 | *
|
---|
368 | * @returns nothing.
|
---|
369 | * @param pFlowBb The basic block to destroy.
|
---|
370 | * @param fMayDestroyFlow Flag whether the control flow graph container
|
---|
371 | * should be destroyed when there is nothing referencing it.
|
---|
372 | */
|
---|
373 | static void dbgfR3FlowBbDestroy(PDBGFFLOWBBINT pFlowBb, bool fMayDestroyFlow)
|
---|
374 | {
|
---|
375 | PDBGFFLOWINT pThis = pFlowBb->pFlow;
|
---|
376 |
|
---|
377 | RTListNodeRemove(&pFlowBb->NdFlowBb);
|
---|
378 | pThis->cBbs--;
|
---|
379 | for (uint32_t idxInstr = 0; idxInstr < pFlowBb->cInstr; idxInstr++)
|
---|
380 | RTStrCacheRelease(pThis->hStrCacheInstr, pFlowBb->aInstr[idxInstr].pszInstr);
|
---|
381 | uint32_t cRefsBb = ASMAtomicDecU32(&pThis->cRefsBb);
|
---|
382 | RTMemFree(pFlowBb);
|
---|
383 |
|
---|
384 | if (!cRefsBb && !pThis->cRefs && fMayDestroyFlow)
|
---|
385 | dbgfR3FlowDestroy(pThis);
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Destroys a given branch table.
|
---|
391 | *
|
---|
392 | * @returns nothing.
|
---|
393 | * @param pFlowBranchTbl The flow branch table to destroy.
|
---|
394 | */
|
---|
395 | static void dbgfR3FlowBranchTblDestroy(PDBGFFLOWBRANCHTBLINT pFlowBranchTbl)
|
---|
396 | {
|
---|
397 | RTListNodeRemove(&pFlowBranchTbl->NdBranchTbl);
|
---|
398 | RTMemFree(pFlowBranchTbl);
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Internal basic block release worker.
|
---|
404 | *
|
---|
405 | * @returns New reference count of the released basic block, on 0
|
---|
406 | * it is destroyed.
|
---|
407 | * @param pFlowBb The basic block to release.
|
---|
408 | * @param fMayDestroyFlow Flag whether the control flow graph container
|
---|
409 | * should be destroyed when there is nothing referencing it.
|
---|
410 | */
|
---|
411 | static uint32_t dbgfR3FlowBbReleaseInt(PDBGFFLOWBBINT pFlowBb, bool fMayDestroyFlow)
|
---|
412 | {
|
---|
413 | uint32_t cRefs = ASMAtomicDecU32(&pFlowBb->cRefs);
|
---|
414 | AssertMsg(cRefs < _1M, ("%#x %p %d\n", cRefs, pFlowBb, pFlowBb->enmEndType));
|
---|
415 | if (cRefs == 0)
|
---|
416 | dbgfR3FlowBbDestroy(pFlowBb, fMayDestroyFlow);
|
---|
417 | return cRefs;
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Links the given basic block into the control flow graph.
|
---|
423 | *
|
---|
424 | * @returns nothing.
|
---|
425 | * @param pThis The control flow graph to link into.
|
---|
426 | * @param pFlowBb The basic block to link.
|
---|
427 | */
|
---|
428 | DECLINLINE(void) dbgfR3FlowLink(PDBGFFLOWINT pThis, PDBGFFLOWBBINT pFlowBb)
|
---|
429 | {
|
---|
430 | RTListAppend(&pThis->LstFlowBb, &pFlowBb->NdFlowBb);
|
---|
431 | pThis->cBbs++;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Links the given branch table into the control flow graph.
|
---|
437 | *
|
---|
438 | * @returns nothing.
|
---|
439 | * @param pThis The control flow graph to link into.
|
---|
440 | * @param pBranchTbl The branch table to link.
|
---|
441 | */
|
---|
442 | DECLINLINE(void) dbgfR3FlowBranchTblLink(PDBGFFLOWINT pThis, PDBGFFLOWBRANCHTBLINT pBranchTbl)
|
---|
443 | {
|
---|
444 | RTListAppend(&pThis->LstBranchTbl, &pBranchTbl->NdBranchTbl);
|
---|
445 | pThis->cBranchTbls++;
|
---|
446 | }
|
---|
447 |
|
---|
448 |
|
---|
449 | /**
|
---|
450 | * Returns the first unpopulated basic block of the given control flow graph.
|
---|
451 | *
|
---|
452 | * @returns The first unpopulated control flow graph or NULL if not found.
|
---|
453 | * @param pThis The control flow graph.
|
---|
454 | */
|
---|
455 | DECLINLINE(PDBGFFLOWBBINT) dbgfR3FlowGetUnpopulatedBb(PDBGFFLOWINT pThis)
|
---|
456 | {
|
---|
457 | PDBGFFLOWBBINT pFlowBb = NULL;
|
---|
458 | RTListForEach(&pThis->LstFlowBb, pFlowBb, DBGFFLOWBBINT, NdFlowBb)
|
---|
459 | {
|
---|
460 | if (pFlowBb->fFlags & DBGF_FLOW_BB_F_EMPTY)
|
---|
461 | return pFlowBb;
|
---|
462 | }
|
---|
463 |
|
---|
464 | return NULL;
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Returns the branch table with the given address if it exists.
|
---|
470 | *
|
---|
471 | * @returns Pointer to the branch table record or NULL if not found.
|
---|
472 | * @param pThis The control flow graph.
|
---|
473 | * @param pAddrTbl The branch table address.
|
---|
474 | */
|
---|
475 | DECLINLINE(PDBGFFLOWBRANCHTBLINT) dbgfR3FlowBranchTblFindByAddr(PDBGFFLOWINT pThis, PDBGFADDRESS pAddrTbl)
|
---|
476 | {
|
---|
477 | PDBGFFLOWBRANCHTBLINT pTbl = NULL;
|
---|
478 | RTListForEach(&pThis->LstBranchTbl, pTbl, DBGFFLOWBRANCHTBLINT, NdBranchTbl)
|
---|
479 | {
|
---|
480 | if (dbgfR3FlowAddrEqual(&pTbl->AddrStart, pAddrTbl))
|
---|
481 | return pTbl;
|
---|
482 | }
|
---|
483 |
|
---|
484 | return NULL;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Sets the given error status for the basic block.
|
---|
490 | *
|
---|
491 | * @returns nothing.
|
---|
492 | * @param pFlowBb The basic block causing the error.
|
---|
493 | * @param rcError The error to set.
|
---|
494 | * @param pszFmt Format string of the error description.
|
---|
495 | * @param ... Arguments for the format string.
|
---|
496 | */
|
---|
497 | static void dbgfR3FlowBbSetError(PDBGFFLOWBBINT pFlowBb, int rcError, const char *pszFmt, ...)
|
---|
498 | {
|
---|
499 | va_list va;
|
---|
500 | va_start(va, pszFmt);
|
---|
501 |
|
---|
502 | Assert(!(pFlowBb->fFlags & DBGF_FLOW_BB_F_INCOMPLETE_ERR));
|
---|
503 | pFlowBb->fFlags |= DBGF_FLOW_BB_F_INCOMPLETE_ERR;
|
---|
504 | pFlowBb->fFlags &= ~DBGF_FLOW_BB_F_EMPTY;
|
---|
505 | pFlowBb->rcError = rcError;
|
---|
506 | pFlowBb->pszErr = RTStrAPrintf2V(pszFmt, va);
|
---|
507 | va_end(va);
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Checks whether the given control flow graph contains a basic block
|
---|
513 | * with the given start address.
|
---|
514 | *
|
---|
515 | * @returns true if there is a basic block with the start address, false otherwise.
|
---|
516 | * @param pThis The control flow graph.
|
---|
517 | * @param pAddr The address to check for.
|
---|
518 | */
|
---|
519 | static bool dbgfR3FlowHasBbWithStartAddr(PDBGFFLOWINT pThis, PDBGFADDRESS pAddr)
|
---|
520 | {
|
---|
521 | PDBGFFLOWBBINT pFlowBb = NULL;
|
---|
522 | RTListForEach(&pThis->LstFlowBb, pFlowBb, DBGFFLOWBBINT, NdFlowBb)
|
---|
523 | {
|
---|
524 | if (dbgfR3FlowAddrEqual(&pFlowBb->AddrStart, pAddr))
|
---|
525 | return true;
|
---|
526 | }
|
---|
527 | return false;
|
---|
528 | }
|
---|
529 |
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Splits a given basic block into two at the given address.
|
---|
533 | *
|
---|
534 | * @returns VBox status code.
|
---|
535 | * @param pThis The control flow graph.
|
---|
536 | * @param pFlowBb The basic block to split.
|
---|
537 | * @param pAddr The address to split at.
|
---|
538 | */
|
---|
539 | static int dbgfR3FlowBbSplit(PDBGFFLOWINT pThis, PDBGFFLOWBBINT pFlowBb, PDBGFADDRESS pAddr)
|
---|
540 | {
|
---|
541 | int rc = VINF_SUCCESS;
|
---|
542 | uint32_t idxInstrSplit;
|
---|
543 |
|
---|
544 | /* If the block is empty it will get populated later so there is nothing to split,
|
---|
545 | * same if the start address equals. */
|
---|
546 | if ( pFlowBb->fFlags & DBGF_FLOW_BB_F_EMPTY
|
---|
547 | || dbgfR3FlowAddrEqual(&pFlowBb->AddrStart, pAddr))
|
---|
548 | return VINF_SUCCESS;
|
---|
549 |
|
---|
550 | /* Find the instruction to split at. */
|
---|
551 | for (idxInstrSplit = 1; idxInstrSplit < pFlowBb->cInstr; idxInstrSplit++)
|
---|
552 | if (dbgfR3FlowAddrEqual(&pFlowBb->aInstr[idxInstrSplit].AddrInstr, pAddr))
|
---|
553 | break;
|
---|
554 |
|
---|
555 | Assert(idxInstrSplit > 0);
|
---|
556 |
|
---|
557 | /*
|
---|
558 | * Given address might not be on instruction boundary, this is not supported
|
---|
559 | * so far and results in an error.
|
---|
560 | */
|
---|
561 | if (idxInstrSplit < pFlowBb->cInstr)
|
---|
562 | {
|
---|
563 | /* Create new basic block. */
|
---|
564 | uint32_t cInstrNew = pFlowBb->cInstr - idxInstrSplit;
|
---|
565 | PDBGFFLOWBBINT pFlowBbNew = dbgfR3FlowBbCreate(pThis, &pFlowBb->aInstr[idxInstrSplit].AddrInstr,
|
---|
566 | 0 /*fFlowBbFlags*/, cInstrNew);
|
---|
567 | if (pFlowBbNew)
|
---|
568 | {
|
---|
569 | /* Move instructions over. */
|
---|
570 | pFlowBbNew->cInstr = cInstrNew;
|
---|
571 | pFlowBbNew->AddrEnd = pFlowBb->AddrEnd;
|
---|
572 | pFlowBbNew->enmEndType = pFlowBb->enmEndType;
|
---|
573 | pFlowBbNew->AddrTarget = pFlowBb->AddrTarget;
|
---|
574 | pFlowBbNew->fFlags = pFlowBb->fFlags & ~DBGF_FLOW_BB_F_ENTRY;
|
---|
575 | pFlowBbNew->pFlowBranchTbl = pFlowBb->pFlowBranchTbl;
|
---|
576 | pFlowBb->pFlowBranchTbl = NULL;
|
---|
577 |
|
---|
578 | /* Move any error to the new basic block and clear them in the old basic block. */
|
---|
579 | pFlowBbNew->rcError = pFlowBb->rcError;
|
---|
580 | pFlowBbNew->pszErr = pFlowBb->pszErr;
|
---|
581 | pFlowBb->rcError = VINF_SUCCESS;
|
---|
582 | pFlowBb->pszErr = NULL;
|
---|
583 | pFlowBb->fFlags &= ~DBGF_FLOW_BB_F_INCOMPLETE_ERR;
|
---|
584 |
|
---|
585 | memcpy(&pFlowBbNew->aInstr[0], &pFlowBb->aInstr[idxInstrSplit], cInstrNew * sizeof(DBGFFLOWBBINSTR));
|
---|
586 | pFlowBb->cInstr = idxInstrSplit;
|
---|
587 | pFlowBb->enmEndType = DBGFFLOWBBENDTYPE_UNCOND;
|
---|
588 | pFlowBb->AddrEnd = pFlowBb->aInstr[idxInstrSplit-1].AddrInstr;
|
---|
589 | pFlowBb->AddrTarget = pFlowBbNew->AddrStart;
|
---|
590 | DBGFR3AddrAdd(&pFlowBb->AddrEnd, pFlowBb->aInstr[idxInstrSplit-1].cbInstr - 1);
|
---|
591 | RT_BZERO(&pFlowBb->aInstr[idxInstrSplit], cInstrNew * sizeof(DBGFFLOWBBINSTR));
|
---|
592 |
|
---|
593 | dbgfR3FlowLink(pThis, pFlowBbNew);
|
---|
594 | }
|
---|
595 | else
|
---|
596 | rc = VERR_NO_MEMORY;
|
---|
597 | }
|
---|
598 | else
|
---|
599 | AssertFailedStmt(rc = VERR_INVALID_STATE); /** @todo: Proper status code. */
|
---|
600 |
|
---|
601 | return rc;
|
---|
602 | }
|
---|
603 |
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Makes sure there is an successor at the given address splitting already existing
|
---|
607 | * basic blocks if they intersect.
|
---|
608 | *
|
---|
609 | * @returns VBox status code.
|
---|
610 | * @param pThis The control flow graph.
|
---|
611 | * @param pAddrSucc The guest address the new successor should start at.
|
---|
612 | * @param fNewBbFlags Flags for the new basic block.
|
---|
613 | * @param pBranchTbl Branch table candidate for this basic block.
|
---|
614 | */
|
---|
615 | static int dbgfR3FlowBbSuccessorAdd(PDBGFFLOWINT pThis, PDBGFADDRESS pAddrSucc,
|
---|
616 | uint32_t fNewBbFlags, PDBGFFLOWBRANCHTBLINT pBranchTbl)
|
---|
617 | {
|
---|
618 | PDBGFFLOWBBINT pFlowBb = NULL;
|
---|
619 | RTListForEach(&pThis->LstFlowBb, pFlowBb, DBGFFLOWBBINT, NdFlowBb)
|
---|
620 | {
|
---|
621 | /*
|
---|
622 | * The basic block must be split if it intersects with the given address
|
---|
623 | * and the start address does not equal the given one.
|
---|
624 | */
|
---|
625 | if (dbgfR3FlowAddrIntersect(pFlowBb, pAddrSucc))
|
---|
626 | return dbgfR3FlowBbSplit(pThis, pFlowBb, pAddrSucc);
|
---|
627 | }
|
---|
628 |
|
---|
629 | int rc = VINF_SUCCESS;
|
---|
630 | pFlowBb = dbgfR3FlowBbCreate(pThis, pAddrSucc, fNewBbFlags, 10);
|
---|
631 | if (pFlowBb)
|
---|
632 | {
|
---|
633 | pFlowBb->pFlowBranchTbl = pBranchTbl;
|
---|
634 | dbgfR3FlowLink(pThis, pFlowBb);
|
---|
635 | }
|
---|
636 | else
|
---|
637 | rc = VERR_NO_MEMORY;
|
---|
638 |
|
---|
639 | return rc;
|
---|
640 | }
|
---|
641 |
|
---|
642 |
|
---|
643 | /**
|
---|
644 | * Returns whether the parameter indicates an indirect branch.
|
---|
645 | *
|
---|
646 | * @returns Flag whether this is an indirect branch.
|
---|
647 | * @param pDisParam The parameter from the disassembler.
|
---|
648 | */
|
---|
649 | DECLINLINE(bool) dbgfR3FlowBranchTargetIsIndirect(PDISOPPARAM pDisParam)
|
---|
650 | {
|
---|
651 | bool fIndirect = true;
|
---|
652 |
|
---|
653 | if ( pDisParam->fUse & (DISUSE_IMMEDIATE8 | DISUSE_IMMEDIATE16 | DISUSE_IMMEDIATE32 | DISUSE_IMMEDIATE64)
|
---|
654 | || pDisParam->fUse & (DISUSE_IMMEDIATE8_REL | DISUSE_IMMEDIATE16_REL | DISUSE_IMMEDIATE32_REL | DISUSE_IMMEDIATE64_REL))
|
---|
655 | fIndirect = false;
|
---|
656 |
|
---|
657 | return fIndirect;
|
---|
658 | }
|
---|
659 |
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Resolves the direct branch target address if possible from the given instruction address
|
---|
663 | * and instruction parameter.
|
---|
664 | *
|
---|
665 | * @returns VBox status code.
|
---|
666 | * @param pUVM The usermode VM handle.
|
---|
667 | * @param idCpu CPU id for resolving the address.
|
---|
668 | * @param pDisParam The parameter from the disassembler.
|
---|
669 | * @param pAddrInstr The instruction address.
|
---|
670 | * @param cbInstr Size of instruction in bytes.
|
---|
671 | * @param fRelJmp Flag whether this is a reltive jump.
|
---|
672 | * @param pAddrJmpTarget Where to store the address to the jump target on success.
|
---|
673 | */
|
---|
674 | static int dbgfR3FlowQueryDirectBranchTarget(PUVM pUVM, VMCPUID idCpu, PDISOPPARAM pDisParam, PDBGFADDRESS pAddrInstr,
|
---|
675 | uint32_t cbInstr, bool fRelJmp, PDBGFADDRESS pAddrJmpTarget)
|
---|
676 | {
|
---|
677 | int rc = VINF_SUCCESS;
|
---|
678 |
|
---|
679 | Assert(!dbgfR3FlowBranchTargetIsIndirect(pDisParam));
|
---|
680 |
|
---|
681 | /* Relative jumps are always from the beginning of the next instruction. */
|
---|
682 | *pAddrJmpTarget = *pAddrInstr;
|
---|
683 | DBGFR3AddrAdd(pAddrJmpTarget, cbInstr);
|
---|
684 |
|
---|
685 | if (fRelJmp)
|
---|
686 | {
|
---|
687 | RTGCINTPTR iRel = 0;
|
---|
688 | if (pDisParam->fUse & DISUSE_IMMEDIATE8_REL)
|
---|
689 | iRel = (int8_t)pDisParam->uValue;
|
---|
690 | else if (pDisParam->fUse & DISUSE_IMMEDIATE16_REL)
|
---|
691 | iRel = (int16_t)pDisParam->uValue;
|
---|
692 | else if (pDisParam->fUse & DISUSE_IMMEDIATE32_REL)
|
---|
693 | iRel = (int32_t)pDisParam->uValue;
|
---|
694 | else if (pDisParam->fUse & DISUSE_IMMEDIATE64_REL)
|
---|
695 | iRel = (int64_t)pDisParam->uValue;
|
---|
696 | else
|
---|
697 | AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
|
---|
698 |
|
---|
699 | if (iRel < 0)
|
---|
700 | DBGFR3AddrSub(pAddrJmpTarget, -iRel);
|
---|
701 | else
|
---|
702 | DBGFR3AddrAdd(pAddrJmpTarget, iRel);
|
---|
703 | }
|
---|
704 | else
|
---|
705 | {
|
---|
706 | if (pDisParam->fUse & (DISUSE_IMMEDIATE8 | DISUSE_IMMEDIATE16 | DISUSE_IMMEDIATE32 | DISUSE_IMMEDIATE64))
|
---|
707 | {
|
---|
708 | if (DBGFADDRESS_IS_FLAT(pAddrInstr))
|
---|
709 | DBGFR3AddrFromFlat(pUVM, pAddrJmpTarget, pDisParam->uValue);
|
---|
710 | else
|
---|
711 | DBGFR3AddrFromSelOff(pUVM, idCpu, pAddrJmpTarget, pAddrInstr->Sel, pDisParam->uValue);
|
---|
712 | }
|
---|
713 | else
|
---|
714 | AssertFailedStmt(rc = VERR_INVALID_STATE);
|
---|
715 | }
|
---|
716 |
|
---|
717 | return rc;
|
---|
718 | }
|
---|
719 |
|
---|
720 |
|
---|
721 | /**
|
---|
722 | * Returns the CPU mode based on the given assembler flags.
|
---|
723 | *
|
---|
724 | * @returns CPU mode.
|
---|
725 | * @param pUVM The user mode VM handle.
|
---|
726 | * @param idCpu CPU id for disassembling.
|
---|
727 | * @param fFlagsDisasm The flags used for disassembling.
|
---|
728 | */
|
---|
729 | static CPUMMODE dbgfR3FlowGetDisasCpuMode(PUVM pUVM, VMCPUID idCpu, uint32_t fFlagsDisasm)
|
---|
730 | {
|
---|
731 | CPUMMODE enmMode = CPUMMODE_INVALID;
|
---|
732 | uint32_t fDisasMode = fFlagsDisasm & DBGF_DISAS_FLAGS_MODE_MASK;
|
---|
733 | if (fDisasMode == DBGF_DISAS_FLAGS_DEFAULT_MODE)
|
---|
734 | enmMode = DBGFR3CpuGetMode(pUVM, idCpu);
|
---|
735 | else if ( fDisasMode == DBGF_DISAS_FLAGS_16BIT_MODE
|
---|
736 | || fDisasMode == DBGF_DISAS_FLAGS_16BIT_REAL_MODE)
|
---|
737 | enmMode = CPUMMODE_REAL;
|
---|
738 | else if (fDisasMode == DBGF_DISAS_FLAGS_32BIT_MODE)
|
---|
739 | enmMode = CPUMMODE_PROTECTED;
|
---|
740 | else if (fDisasMode == DBGF_DISAS_FLAGS_64BIT_MODE)
|
---|
741 | enmMode = CPUMMODE_LONG;
|
---|
742 | else
|
---|
743 | AssertFailed();
|
---|
744 |
|
---|
745 | return enmMode;
|
---|
746 | }
|
---|
747 |
|
---|
748 |
|
---|
749 | /**
|
---|
750 | * Searches backwards in the given basic block starting the given instruction index for
|
---|
751 | * a mov instruction with the given register as the target where the constant looks like
|
---|
752 | * a pointer.
|
---|
753 | *
|
---|
754 | * @returns Flag whether a candidate was found.
|
---|
755 | * @param pFlowBb The basic block containing the indirect branch.
|
---|
756 | * @param idxRegTgt The general register the mov targets.
|
---|
757 | * @param cbPtr The pointer size to look for.
|
---|
758 | * @param pUVM The user mode VM handle.
|
---|
759 | * @param idCpu CPU id for disassembling.
|
---|
760 | * @param fFlagsDisasm The flags to use for disassembling.
|
---|
761 | * @param pidxInstrStart The instruction index to start searching for on input,
|
---|
762 | * The last instruction evaluated on output.
|
---|
763 | * @param pAddrDest Where to store the candidate address on success.
|
---|
764 | */
|
---|
765 | static bool dbgfR3FlowSearchMovWithConstantPtrSizeBackwards(PDBGFFLOWBBINT pFlowBb, uint8_t idxRegTgt, uint32_t cbPtr,
|
---|
766 | PUVM pUVM, VMCPUID idCpu, uint32_t fFlagsDisasm,
|
---|
767 | uint32_t *pidxInstrStart, PDBGFADDRESS pAddrDest)
|
---|
768 | {
|
---|
769 | bool fFound = false;
|
---|
770 | uint32_t idxInstrCur = *pidxInstrStart;
|
---|
771 | uint32_t cInstrCheck = idxInstrCur + 1;
|
---|
772 |
|
---|
773 | for (;;)
|
---|
774 | {
|
---|
775 | /** @todo: Avoid to disassemble again. */
|
---|
776 | PDBGFFLOWBBINSTR pInstr = &pFlowBb->aInstr[idxInstrCur];
|
---|
777 | DBGFDISSTATE DisState;
|
---|
778 | char szOutput[_4K];
|
---|
779 |
|
---|
780 | int rc = dbgfR3DisasInstrStateEx(pUVM, idCpu, &pInstr->AddrInstr, fFlagsDisasm,
|
---|
781 | &szOutput[0], sizeof(szOutput), &DisState);
|
---|
782 | if (RT_SUCCESS(rc))
|
---|
783 | {
|
---|
784 | if ( DisState.pCurInstr->uOpcode == OP_MOV
|
---|
785 | && (DisState.Param1.fUse & (DISUSE_REG_GEN16 | DISUSE_REG_GEN32 | DISUSE_REG_GEN64))
|
---|
786 | && DisState.Param1.Base.idxGenReg == idxRegTgt
|
---|
787 | /*&& DisState.Param1.cb == cbPtr*/
|
---|
788 | && DisState.Param2.cb == cbPtr
|
---|
789 | && (DisState.Param2.fUse & (DISUSE_IMMEDIATE16 | DISUSE_IMMEDIATE32 | DISUSE_IMMEDIATE64)))
|
---|
790 | {
|
---|
791 | /* Found possible candidate. */
|
---|
792 | fFound = true;
|
---|
793 | if (DBGFADDRESS_IS_FLAT(&pInstr->AddrInstr))
|
---|
794 | DBGFR3AddrFromFlat(pUVM, pAddrDest, DisState.Param2.uValue);
|
---|
795 | else
|
---|
796 | DBGFR3AddrFromSelOff(pUVM, idCpu, pAddrDest, pInstr->AddrInstr.Sel, DisState.Param2.uValue);
|
---|
797 | break;
|
---|
798 | }
|
---|
799 | }
|
---|
800 | else
|
---|
801 | break;
|
---|
802 |
|
---|
803 | cInstrCheck--;
|
---|
804 | if (!cInstrCheck)
|
---|
805 | break;
|
---|
806 |
|
---|
807 | idxInstrCur--;
|
---|
808 | }
|
---|
809 |
|
---|
810 | *pidxInstrStart = idxInstrCur;
|
---|
811 | return fFound;
|
---|
812 | }
|
---|
813 |
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * Verifies the given branch table candidate and adds it to the control flow graph on success.
|
---|
817 | *
|
---|
818 | * @returns VBox status code.
|
---|
819 | * @param pThis The flow control graph.
|
---|
820 | * @param pFlowBb The basic block causing the indirect branch.
|
---|
821 | * @param pAddrBranchTbl Address of the branch table location.
|
---|
822 | * @param idxGenRegBase The general register holding the base address.
|
---|
823 | * @param cbPtr Guest pointer size.
|
---|
824 | * @param pUVM The user mode VM handle.
|
---|
825 | * @param idCpu CPU id for disassembling.
|
---|
826 | *
|
---|
827 | * @todo Handle branch tables greater than 4KB (lazy coder).
|
---|
828 | */
|
---|
829 | static int dbgfR3FlowBranchTblVerifyAdd(PDBGFFLOWINT pThis, PDBGFFLOWBBINT pFlowBb, PDBGFADDRESS pAddrBranchTbl,
|
---|
830 | uint8_t idxGenRegBase, uint32_t cbPtr, PUVM pUVM, VMCPUID idCpu)
|
---|
831 | {
|
---|
832 | int rc = VINF_SUCCESS;
|
---|
833 | PDBGFFLOWBRANCHTBLINT pBranchTbl = dbgfR3FlowBranchTblFindByAddr(pThis, pAddrBranchTbl);
|
---|
834 |
|
---|
835 | if (!pBranchTbl)
|
---|
836 | {
|
---|
837 | uint32_t cSlots = 0;
|
---|
838 | uint8_t abBuf[_4K];
|
---|
839 |
|
---|
840 | rc = DBGFR3MemRead(pUVM, idCpu, pAddrBranchTbl, &abBuf[0], sizeof(abBuf));
|
---|
841 | if (RT_SUCCESS(rc))
|
---|
842 | {
|
---|
843 | uint8_t *pbBuf = &abBuf[0];
|
---|
844 | while (pbBuf < &abBuf[0] + sizeof(abBuf))
|
---|
845 | {
|
---|
846 | DBGFADDRESS AddrDest;
|
---|
847 | RTGCUINTPTR GCPtr = cbPtr == sizeof(uint64_t)
|
---|
848 | ? *(uint64_t *)pbBuf
|
---|
849 | : cbPtr == sizeof(uint32_t)
|
---|
850 | ? *(uint32_t *)pbBuf
|
---|
851 | : *(uint16_t *)pbBuf;
|
---|
852 | pbBuf += cbPtr;
|
---|
853 |
|
---|
854 | if (DBGFADDRESS_IS_FLAT(pAddrBranchTbl))
|
---|
855 | DBGFR3AddrFromFlat(pUVM, &AddrDest, GCPtr);
|
---|
856 | else
|
---|
857 | DBGFR3AddrFromSelOff(pUVM, idCpu, &AddrDest, pAddrBranchTbl->Sel, GCPtr);
|
---|
858 |
|
---|
859 | if (dbgfR3FlowAddrGetDistance(&AddrDest, &pFlowBb->AddrEnd) > _512K)
|
---|
860 | break;
|
---|
861 |
|
---|
862 | cSlots++;
|
---|
863 | }
|
---|
864 |
|
---|
865 | /* If there are any slots use it. */
|
---|
866 | if (cSlots)
|
---|
867 | {
|
---|
868 | pBranchTbl = dbgfR3FlowBranchTblCreate(pThis, pAddrBranchTbl, idxGenRegBase, cSlots);
|
---|
869 | if (pBranchTbl)
|
---|
870 | {
|
---|
871 | /* Get the addresses. */
|
---|
872 | for (unsigned i = 0; i < cSlots && RT_SUCCESS(rc); i++)
|
---|
873 | {
|
---|
874 | RTGCUINTPTR GCPtr = cbPtr == sizeof(uint64_t)
|
---|
875 | ? *(uint64_t *)&abBuf[i * cbPtr]
|
---|
876 | : cbPtr == sizeof(uint32_t)
|
---|
877 | ? *(uint32_t *)&abBuf[i * cbPtr]
|
---|
878 | : *(uint16_t *)&abBuf[i * cbPtr];
|
---|
879 |
|
---|
880 | if (DBGFADDRESS_IS_FLAT(pAddrBranchTbl))
|
---|
881 | DBGFR3AddrFromFlat(pUVM, &pBranchTbl->aAddresses[i], GCPtr);
|
---|
882 | else
|
---|
883 | DBGFR3AddrFromSelOff(pUVM, idCpu, &pBranchTbl->aAddresses[i],
|
---|
884 | pAddrBranchTbl->Sel, GCPtr);
|
---|
885 | rc = dbgfR3FlowBbSuccessorAdd(pThis, &pBranchTbl->aAddresses[i], DBGF_FLOW_BB_F_BRANCH_TABLE,
|
---|
886 | pBranchTbl);
|
---|
887 | }
|
---|
888 | dbgfR3FlowBranchTblLink(pThis, pBranchTbl);
|
---|
889 | }
|
---|
890 | else
|
---|
891 | rc = VERR_NO_MEMORY;
|
---|
892 | }
|
---|
893 | }
|
---|
894 | }
|
---|
895 |
|
---|
896 | if (pBranchTbl)
|
---|
897 | pFlowBb->pFlowBranchTbl = pBranchTbl;
|
---|
898 |
|
---|
899 | return rc;
|
---|
900 | }
|
---|
901 |
|
---|
902 |
|
---|
903 | /**
|
---|
904 | * Checks whether the location for the branch target candidate contains a valid code address.
|
---|
905 | *
|
---|
906 | * @returns VBox status code.
|
---|
907 | * @param pThis The flow control graph.
|
---|
908 | * @param pFlowBb The basic block causing the indirect branch.
|
---|
909 | * @param pAddrBranchTgt Address of the branch target location.
|
---|
910 | * @param idxGenRegBase The general register holding the address of the location.
|
---|
911 | * @param cbPtr Guest pointer size.
|
---|
912 | * @param pUVM The user mode VM handle.
|
---|
913 | * @param idCpu CPU id for disassembling.
|
---|
914 | * @param fBranchTbl Flag whether this is a possible branch table containing multiple
|
---|
915 | * targets.
|
---|
916 | */
|
---|
917 | static int dbgfR3FlowCheckBranchTargetLocation(PDBGFFLOWINT pThis, PDBGFFLOWBBINT pFlowBb, PDBGFADDRESS pAddrBranchTgt,
|
---|
918 | uint8_t idxGenRegBase, uint32_t cbPtr, PUVM pUVM, VMCPUID idCpu, bool fBranchTbl)
|
---|
919 | {
|
---|
920 | int rc = VINF_SUCCESS;
|
---|
921 |
|
---|
922 | if (!fBranchTbl)
|
---|
923 | {
|
---|
924 | union { uint16_t u16Val; uint32_t u32Val; uint64_t u64Val; } uVal;
|
---|
925 | rc = DBGFR3MemRead(pUVM, idCpu, pAddrBranchTgt, &uVal, cbPtr);
|
---|
926 | if (RT_SUCCESS(rc))
|
---|
927 | {
|
---|
928 | DBGFADDRESS AddrTgt;
|
---|
929 | RTGCUINTPTR GCPtr = cbPtr == sizeof(uint64_t)
|
---|
930 | ? uVal.u64Val
|
---|
931 | : cbPtr == sizeof(uint32_t)
|
---|
932 | ? uVal.u32Val
|
---|
933 | : uVal.u16Val;
|
---|
934 | if (DBGFADDRESS_IS_FLAT(pAddrBranchTgt))
|
---|
935 | DBGFR3AddrFromFlat(pUVM, &AddrTgt, GCPtr);
|
---|
936 | else
|
---|
937 | DBGFR3AddrFromSelOff(pUVM, idCpu, &AddrTgt, pAddrBranchTgt->Sel, GCPtr);
|
---|
938 |
|
---|
939 | if (dbgfR3FlowAddrGetDistance(&AddrTgt, &pFlowBb->AddrEnd) <= _128K)
|
---|
940 | {
|
---|
941 | /* Finish the basic block. */
|
---|
942 | pFlowBb->AddrTarget = AddrTgt;
|
---|
943 | rc = dbgfR3FlowBbSuccessorAdd(pThis, &AddrTgt,
|
---|
944 | (pFlowBb->fFlags & DBGF_FLOW_BB_F_BRANCH_TABLE),
|
---|
945 | pFlowBb->pFlowBranchTbl);
|
---|
946 | }
|
---|
947 | else
|
---|
948 | rc = VERR_NOT_FOUND;
|
---|
949 | }
|
---|
950 | }
|
---|
951 | else
|
---|
952 | rc = dbgfR3FlowBranchTblVerifyAdd(pThis, pFlowBb, pAddrBranchTgt,
|
---|
953 | idxGenRegBase, cbPtr, pUVM, idCpu);
|
---|
954 |
|
---|
955 | return rc;
|
---|
956 | }
|
---|
957 |
|
---|
958 |
|
---|
959 | /**
|
---|
960 | * Tries to resolve the indirect branch.
|
---|
961 | *
|
---|
962 | * @returns VBox status code.
|
---|
963 | * @param pThis The flow control graph.
|
---|
964 | * @param pFlowBb The basic block causing the indirect branch.
|
---|
965 | * @param pUVM The user mode VM handle.
|
---|
966 | * @param idCpu CPU id for disassembling.
|
---|
967 | * @param pDisParam The parameter from the disassembler.
|
---|
968 | * @param fFlagsDisasm Flags for the disassembler.
|
---|
969 | */
|
---|
970 | static int dbgfR3FlowTryResolveIndirectBranch(PDBGFFLOWINT pThis, PDBGFFLOWBBINT pFlowBb, PUVM pUVM,
|
---|
971 | VMCPUID idCpu, PDISOPPARAM pDisParam, uint32_t fFlagsDisasm)
|
---|
972 | {
|
---|
973 | Assert(dbgfR3FlowBranchTargetIsIndirect(pDisParam));
|
---|
974 |
|
---|
975 | uint32_t cbPtr = 0;
|
---|
976 | CPUMMODE enmMode = dbgfR3FlowGetDisasCpuMode(pUVM, idCpu, fFlagsDisasm);
|
---|
977 |
|
---|
978 | switch (enmMode)
|
---|
979 | {
|
---|
980 | case CPUMMODE_REAL:
|
---|
981 | cbPtr = sizeof(uint16_t);
|
---|
982 | break;
|
---|
983 | case CPUMMODE_PROTECTED:
|
---|
984 | cbPtr = sizeof(uint32_t);
|
---|
985 | break;
|
---|
986 | case CPUMMODE_LONG:
|
---|
987 | cbPtr = sizeof(uint64_t);
|
---|
988 | break;
|
---|
989 | default:
|
---|
990 | AssertMsgFailed(("Invalid CPU mode %u\n", enmMode));
|
---|
991 | }
|
---|
992 |
|
---|
993 | if (pDisParam->fUse & DISUSE_BASE)
|
---|
994 | {
|
---|
995 | uint8_t idxRegBase = pDisParam->Base.idxGenReg;
|
---|
996 |
|
---|
997 | /* Check that the used register size and the pointer size match. */
|
---|
998 | if ( ((pDisParam->fUse & DISUSE_REG_GEN16) && cbPtr == sizeof(uint16_t))
|
---|
999 | || ((pDisParam->fUse & DISUSE_REG_GEN32) && cbPtr == sizeof(uint32_t))
|
---|
1000 | || ((pDisParam->fUse & DISUSE_REG_GEN64) && cbPtr == sizeof(uint64_t)))
|
---|
1001 | {
|
---|
1002 | /*
|
---|
1003 | * Search all instructions backwards until a move to the used general register
|
---|
1004 | * is detected with a constant using the pointer size.
|
---|
1005 | */
|
---|
1006 | uint32_t idxInstrStart = pFlowBb->cInstr - 1 - 1; /* Don't look at the branch. */
|
---|
1007 | bool fCandidateFound = false;
|
---|
1008 | bool fBranchTbl = RT_BOOL(pDisParam->fUse & DISUSE_INDEX);
|
---|
1009 | DBGFADDRESS AddrBranchTgt;
|
---|
1010 | do
|
---|
1011 | {
|
---|
1012 | fCandidateFound = dbgfR3FlowSearchMovWithConstantPtrSizeBackwards(pFlowBb, idxRegBase, cbPtr,
|
---|
1013 | pUVM, idCpu, fFlagsDisasm,
|
---|
1014 | &idxInstrStart, &AddrBranchTgt);
|
---|
1015 | if (fCandidateFound)
|
---|
1016 | {
|
---|
1017 | /* Check that the address is not too far away from the instruction address. */
|
---|
1018 | RTGCUINTPTR offPtr = dbgfR3FlowAddrGetDistance(&AddrBranchTgt, &pFlowBb->AddrEnd);
|
---|
1019 | if (offPtr <= 20 * _1M)
|
---|
1020 | {
|
---|
1021 | /* Read the content at the address and check that it is near this basic block too. */
|
---|
1022 | int rc = dbgfR3FlowCheckBranchTargetLocation(pThis, pFlowBb, &AddrBranchTgt, idxRegBase,
|
---|
1023 | cbPtr, pUVM, idCpu, fBranchTbl);
|
---|
1024 | if (RT_SUCCESS(rc))
|
---|
1025 | break;
|
---|
1026 | fCandidateFound = false;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | if (idxInstrStart > 0)
|
---|
1030 | idxInstrStart--;
|
---|
1031 | }
|
---|
1032 | } while (idxInstrStart > 0 && !fCandidateFound);
|
---|
1033 | }
|
---|
1034 | else
|
---|
1035 | dbgfR3FlowBbSetError(pFlowBb, VERR_INVALID_STATE,
|
---|
1036 | "The base register size and selected pointer size do not match (fUse=%#x cbPtr=%u)",
|
---|
1037 | pDisParam->fUse, cbPtr);
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | return VINF_SUCCESS;
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 |
|
---|
1044 | /**
|
---|
1045 | * Tries to resolve the indirect branch.
|
---|
1046 | *
|
---|
1047 | * @returns VBox status code.
|
---|
1048 | * @param pThis The flow control graph.
|
---|
1049 | * @param pFlowBb The basic block causing the indirect branch.
|
---|
1050 | * @param pUVM The user mode VM handle.
|
---|
1051 | * @param idCpu CPU id for disassembling.
|
---|
1052 | * @param pDisParam The parameter from the disassembler.
|
---|
1053 | * @param fFlagsDisasm Flags for the disassembler.
|
---|
1054 | */
|
---|
1055 | static int dbgfR3FlowBbCheckBranchTblCandidate(PDBGFFLOWINT pThis, PDBGFFLOWBBINT pFlowBb, PUVM pUVM,
|
---|
1056 | VMCPUID idCpu, PDISOPPARAM pDisParam, uint32_t fFlagsDisasm)
|
---|
1057 | {
|
---|
1058 | int rc = VINF_SUCCESS;
|
---|
1059 |
|
---|
1060 | Assert(pFlowBb->fFlags & DBGF_FLOW_BB_F_BRANCH_TABLE && pFlowBb->pFlowBranchTbl);
|
---|
1061 |
|
---|
1062 | uint32_t cbPtr = 0;
|
---|
1063 | CPUMMODE enmMode = dbgfR3FlowGetDisasCpuMode(pUVM, idCpu, fFlagsDisasm);
|
---|
1064 |
|
---|
1065 | switch (enmMode)
|
---|
1066 | {
|
---|
1067 | case CPUMMODE_REAL:
|
---|
1068 | cbPtr = sizeof(uint16_t);
|
---|
1069 | break;
|
---|
1070 | case CPUMMODE_PROTECTED:
|
---|
1071 | cbPtr = sizeof(uint32_t);
|
---|
1072 | break;
|
---|
1073 | case CPUMMODE_LONG:
|
---|
1074 | cbPtr = sizeof(uint64_t);
|
---|
1075 | break;
|
---|
1076 | default:
|
---|
1077 | AssertMsgFailed(("Invalid CPU mode %u\n", enmMode));
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | if (pDisParam->fUse & DISUSE_BASE)
|
---|
1081 | {
|
---|
1082 | uint8_t idxRegBase = pDisParam->Base.idxGenReg;
|
---|
1083 |
|
---|
1084 | /* Check that the used register size and the pointer size match. */
|
---|
1085 | if ( ((pDisParam->fUse & DISUSE_REG_GEN16) && cbPtr == sizeof(uint16_t))
|
---|
1086 | || ((pDisParam->fUse & DISUSE_REG_GEN32) && cbPtr == sizeof(uint32_t))
|
---|
1087 | || ((pDisParam->fUse & DISUSE_REG_GEN64) && cbPtr == sizeof(uint64_t)))
|
---|
1088 | {
|
---|
1089 | if (idxRegBase != pFlowBb->pFlowBranchTbl->idxGenRegBase)
|
---|
1090 | {
|
---|
1091 | /* Try to find the new branch table. */
|
---|
1092 | pFlowBb->pFlowBranchTbl = NULL;
|
---|
1093 | rc = dbgfR3FlowTryResolveIndirectBranch(pThis, pFlowBb, pUVM, idCpu, pDisParam, fFlagsDisasm);
|
---|
1094 | }
|
---|
1095 | /** @todo: else check that the base register is not modified in this basic block. */
|
---|
1096 | }
|
---|
1097 | else
|
---|
1098 | dbgfR3FlowBbSetError(pFlowBb, VERR_INVALID_STATE,
|
---|
1099 | "The base register size and selected pointer size do not match (fUse=%#x cbPtr=%u)",
|
---|
1100 | pDisParam->fUse, cbPtr);
|
---|
1101 | }
|
---|
1102 | else
|
---|
1103 | dbgfR3FlowBbSetError(pFlowBb, VERR_INVALID_STATE,
|
---|
1104 | "The instruction does not use a register");
|
---|
1105 |
|
---|
1106 | return rc;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 |
|
---|
1110 | /**
|
---|
1111 | * Processes and fills one basic block.
|
---|
1112 | *
|
---|
1113 | * @returns VBox status code.
|
---|
1114 | * @param pUVM The user mode VM handle.
|
---|
1115 | * @param idCpu CPU id for disassembling.
|
---|
1116 | * @param pThis The control flow graph to populate.
|
---|
1117 | * @param pFlowBb The basic block to fill.
|
---|
1118 | * @param cbDisasmMax The maximum amount to disassemble.
|
---|
1119 | * @param fFlags Combination of DBGF_DISAS_FLAGS_*.
|
---|
1120 | */
|
---|
1121 | static int dbgfR3FlowBbProcess(PUVM pUVM, VMCPUID idCpu, PDBGFFLOWINT pThis, PDBGFFLOWBBINT pFlowBb,
|
---|
1122 | uint32_t cbDisasmMax, uint32_t fFlags)
|
---|
1123 | {
|
---|
1124 | int rc = VINF_SUCCESS;
|
---|
1125 | uint32_t cbDisasmLeft = cbDisasmMax ? cbDisasmMax : UINT32_MAX;
|
---|
1126 | DBGFADDRESS AddrDisasm = pFlowBb->AddrEnd;
|
---|
1127 |
|
---|
1128 | Assert(pFlowBb->fFlags & DBGF_FLOW_BB_F_EMPTY);
|
---|
1129 |
|
---|
1130 | /*
|
---|
1131 | * Disassemble instruction by instruction until we get a conditional or
|
---|
1132 | * unconditional jump or some sort of return.
|
---|
1133 | */
|
---|
1134 | while ( cbDisasmLeft
|
---|
1135 | && RT_SUCCESS(rc))
|
---|
1136 | {
|
---|
1137 | DBGFDISSTATE DisState;
|
---|
1138 | char szOutput[_4K];
|
---|
1139 |
|
---|
1140 | /*
|
---|
1141 | * Before disassembling we have to check whether the address belongs
|
---|
1142 | * to another basic block and stop here.
|
---|
1143 | */
|
---|
1144 | if ( !(pFlowBb->fFlags & DBGF_FLOW_BB_F_EMPTY)
|
---|
1145 | && dbgfR3FlowHasBbWithStartAddr(pThis, &AddrDisasm))
|
---|
1146 | {
|
---|
1147 | pFlowBb->AddrTarget = AddrDisasm;
|
---|
1148 | pFlowBb->enmEndType = DBGFFLOWBBENDTYPE_UNCOND;
|
---|
1149 | break;
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | pFlowBb->fFlags &= ~DBGF_FLOW_BB_F_EMPTY;
|
---|
1153 |
|
---|
1154 | rc = dbgfR3DisasInstrStateEx(pUVM, idCpu, &AddrDisasm, fFlags,
|
---|
1155 | &szOutput[0], sizeof(szOutput), &DisState);
|
---|
1156 | if (RT_SUCCESS(rc))
|
---|
1157 | {
|
---|
1158 | cbDisasmLeft -= DisState.cbInstr;
|
---|
1159 |
|
---|
1160 | if (pFlowBb->cInstr == pFlowBb->cInstrMax)
|
---|
1161 | {
|
---|
1162 | /* Reallocate. */
|
---|
1163 | RTListNodeRemove(&pFlowBb->NdFlowBb);
|
---|
1164 | PDBGFFLOWBBINT pFlowBbNew = (PDBGFFLOWBBINT)RTMemRealloc(pFlowBb, RT_OFFSETOF(DBGFFLOWBBINT, aInstr[pFlowBb->cInstrMax + 10]));
|
---|
1165 | if (pFlowBbNew)
|
---|
1166 | {
|
---|
1167 | pFlowBbNew->cInstrMax += 10;
|
---|
1168 | pFlowBb = pFlowBbNew;
|
---|
1169 | }
|
---|
1170 | else
|
---|
1171 | rc = VERR_NO_MEMORY;
|
---|
1172 | RTListAppend(&pThis->LstFlowBb, &pFlowBb->NdFlowBb);
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | if (RT_SUCCESS(rc))
|
---|
1176 | {
|
---|
1177 | PDBGFFLOWBBINSTR pInstr = &pFlowBb->aInstr[pFlowBb->cInstr];
|
---|
1178 |
|
---|
1179 | pInstr->AddrInstr = AddrDisasm;
|
---|
1180 | pInstr->cbInstr = DisState.cbInstr;
|
---|
1181 | pInstr->pszInstr = RTStrCacheEnter(pThis->hStrCacheInstr, &szOutput[0]);
|
---|
1182 | pFlowBb->cInstr++;
|
---|
1183 |
|
---|
1184 | pFlowBb->AddrEnd = AddrDisasm;
|
---|
1185 | DBGFR3AddrAdd(&pFlowBb->AddrEnd, pInstr->cbInstr - 1);
|
---|
1186 | DBGFR3AddrAdd(&AddrDisasm, pInstr->cbInstr);
|
---|
1187 |
|
---|
1188 | /*
|
---|
1189 | * Check control flow instructions and create new basic blocks
|
---|
1190 | * marking the current one as complete.
|
---|
1191 | */
|
---|
1192 | if (DisState.pCurInstr->fOpType & DISOPTYPE_CONTROLFLOW)
|
---|
1193 | {
|
---|
1194 | uint16_t uOpc = DisState.pCurInstr->uOpcode;
|
---|
1195 |
|
---|
1196 | if ( uOpc == OP_RETN || uOpc == OP_RETF || uOpc == OP_IRET
|
---|
1197 | || uOpc == OP_SYSEXIT || uOpc == OP_SYSRET)
|
---|
1198 | pFlowBb->enmEndType = DBGFFLOWBBENDTYPE_EXIT;
|
---|
1199 | else if (uOpc == OP_JMP)
|
---|
1200 | {
|
---|
1201 | Assert(DisState.pCurInstr->fOpType & DISOPTYPE_UNCOND_CONTROLFLOW);
|
---|
1202 |
|
---|
1203 | if (dbgfR3FlowBranchTargetIsIndirect(&DisState.Param1))
|
---|
1204 | {
|
---|
1205 | pFlowBb->enmEndType = DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP;
|
---|
1206 |
|
---|
1207 | if (pFlowBb->fFlags & DBGF_FLOW_BB_F_BRANCH_TABLE)
|
---|
1208 | {
|
---|
1209 | Assert(pThis->fFlags & DBGF_FLOW_CREATE_F_TRY_RESOLVE_INDIRECT_BRANCHES);
|
---|
1210 |
|
---|
1211 | /*
|
---|
1212 | * This basic block was already discovered by parsing a jump table and
|
---|
1213 | * there should be a candidate for the branch table. Check whether it uses the
|
---|
1214 | * same branch table.
|
---|
1215 | */
|
---|
1216 | rc = dbgfR3FlowBbCheckBranchTblCandidate(pThis, pFlowBb, pUVM, idCpu,
|
---|
1217 | &DisState.Param1, fFlags);
|
---|
1218 | }
|
---|
1219 | else
|
---|
1220 | {
|
---|
1221 | if (pThis->fFlags & DBGF_FLOW_CREATE_F_TRY_RESOLVE_INDIRECT_BRANCHES)
|
---|
1222 | rc = dbgfR3FlowTryResolveIndirectBranch(pThis, pFlowBb, pUVM, idCpu,
|
---|
1223 | &DisState.Param1, fFlags);
|
---|
1224 | else
|
---|
1225 | dbgfR3FlowBbSetError(pFlowBb, VERR_NOT_SUPPORTED,
|
---|
1226 | "Detected indirect branch and resolving it not being enabled");
|
---|
1227 | }
|
---|
1228 | }
|
---|
1229 | else
|
---|
1230 | {
|
---|
1231 | pFlowBb->enmEndType = DBGFFLOWBBENDTYPE_UNCOND_JMP;
|
---|
1232 |
|
---|
1233 | /* Create one new basic block with the jump target address. */
|
---|
1234 | rc = dbgfR3FlowQueryDirectBranchTarget(pUVM, idCpu, &DisState.Param1, &pInstr->AddrInstr, pInstr->cbInstr,
|
---|
1235 | RT_BOOL(DisState.pCurInstr->fOpType & DISOPTYPE_RELATIVE_CONTROLFLOW),
|
---|
1236 | &pFlowBb->AddrTarget);
|
---|
1237 | if (RT_SUCCESS(rc))
|
---|
1238 | rc = dbgfR3FlowBbSuccessorAdd(pThis, &pFlowBb->AddrTarget,
|
---|
1239 | (pFlowBb->fFlags & DBGF_FLOW_BB_F_BRANCH_TABLE),
|
---|
1240 | pFlowBb->pFlowBranchTbl);
|
---|
1241 | }
|
---|
1242 | }
|
---|
1243 | else if (uOpc != OP_CALL)
|
---|
1244 | {
|
---|
1245 | Assert(DisState.pCurInstr->fOpType & DISOPTYPE_COND_CONTROLFLOW);
|
---|
1246 | pFlowBb->enmEndType = DBGFFLOWBBENDTYPE_COND;
|
---|
1247 |
|
---|
1248 | /*
|
---|
1249 | * Create two new basic blocks, one with the jump target address
|
---|
1250 | * and one starting after the current instruction.
|
---|
1251 | */
|
---|
1252 | rc = dbgfR3FlowBbSuccessorAdd(pThis, &AddrDisasm,
|
---|
1253 | (pFlowBb->fFlags & DBGF_FLOW_BB_F_BRANCH_TABLE),
|
---|
1254 | pFlowBb->pFlowBranchTbl);
|
---|
1255 | if (RT_SUCCESS(rc))
|
---|
1256 | {
|
---|
1257 | rc = dbgfR3FlowQueryDirectBranchTarget(pUVM, idCpu, &DisState.Param1, &pInstr->AddrInstr, pInstr->cbInstr,
|
---|
1258 | RT_BOOL(DisState.pCurInstr->fOpType & DISOPTYPE_RELATIVE_CONTROLFLOW),
|
---|
1259 | &pFlowBb->AddrTarget);
|
---|
1260 | if (RT_SUCCESS(rc))
|
---|
1261 | rc = dbgfR3FlowBbSuccessorAdd(pThis, &pFlowBb->AddrTarget,
|
---|
1262 | (pFlowBb->fFlags & DBGF_FLOW_BB_F_BRANCH_TABLE),
|
---|
1263 | pFlowBb->pFlowBranchTbl);
|
---|
1264 | }
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | if (RT_FAILURE(rc))
|
---|
1268 | dbgfR3FlowBbSetError(pFlowBb, rc, "Adding successor blocks failed with %Rrc", rc);
|
---|
1269 |
|
---|
1270 | /* Quit disassembling. */
|
---|
1271 | if ( uOpc != OP_CALL
|
---|
1272 | || RT_FAILURE(rc))
|
---|
1273 | break;
|
---|
1274 | }
|
---|
1275 | }
|
---|
1276 | else
|
---|
1277 | dbgfR3FlowBbSetError(pFlowBb, rc, "Increasing basic block failed with %Rrc", rc);
|
---|
1278 | }
|
---|
1279 | else
|
---|
1280 | dbgfR3FlowBbSetError(pFlowBb, rc, "Disassembling the instruction failed with %Rrc", rc);
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | return VINF_SUCCESS;
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 | /**
|
---|
1287 | * Populate all empty basic blocks.
|
---|
1288 | *
|
---|
1289 | * @returns VBox status code.
|
---|
1290 | * @param pUVM The user mode VM handle.
|
---|
1291 | * @param idCpu CPU id for disassembling.
|
---|
1292 | * @param pThis The control flow graph to populate.
|
---|
1293 | * @param pAddrStart The start address to disassemble at.
|
---|
1294 | * @param cbDisasmMax The maximum amount to disassemble.
|
---|
1295 | * @param fFlags Combination of DBGF_DISAS_FLAGS_*.
|
---|
1296 | */
|
---|
1297 | static int dbgfR3FlowPopulate(PUVM pUVM, VMCPUID idCpu, PDBGFFLOWINT pThis, PDBGFADDRESS pAddrStart,
|
---|
1298 | uint32_t cbDisasmMax, uint32_t fFlags)
|
---|
1299 | {
|
---|
1300 | int rc = VINF_SUCCESS;
|
---|
1301 | PDBGFFLOWBBINT pFlowBb = dbgfR3FlowGetUnpopulatedBb(pThis);
|
---|
1302 | DBGFADDRESS AddrEnd = *pAddrStart;
|
---|
1303 | DBGFR3AddrAdd(&AddrEnd, cbDisasmMax);
|
---|
1304 |
|
---|
1305 | while (VALID_PTR(pFlowBb))
|
---|
1306 | {
|
---|
1307 | rc = dbgfR3FlowBbProcess(pUVM, idCpu, pThis, pFlowBb, cbDisasmMax, fFlags);
|
---|
1308 | if (RT_FAILURE(rc))
|
---|
1309 | break;
|
---|
1310 |
|
---|
1311 | pFlowBb = dbgfR3FlowGetUnpopulatedBb(pThis);
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | return rc;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | /**
|
---|
1318 | * Creates a new control flow graph from the given start address.
|
---|
1319 | *
|
---|
1320 | * @returns VBox status code.
|
---|
1321 | * @param pUVM The user mode VM handle.
|
---|
1322 | * @param idCpu CPU id for disassembling.
|
---|
1323 | * @param pAddressStart Where to start creating the control flow graph.
|
---|
1324 | * @param cbDisasmMax Limit the amount of bytes to disassemble, 0 for no limit.
|
---|
1325 | * @param fFlagsFlow Combination of DBGF_FLOW_CREATE_F_* to control the creation of the flow graph.
|
---|
1326 | * @param fFlagsDisasm Combination of DBGF_DISAS_FLAGS_* controlling the style of the disassembled
|
---|
1327 | * instructions.
|
---|
1328 | * @param phFlow Where to store the handle to the control flow graph on success.
|
---|
1329 | */
|
---|
1330 | VMMR3DECL(int) DBGFR3FlowCreate(PUVM pUVM, VMCPUID idCpu, PDBGFADDRESS pAddressStart, uint32_t cbDisasmMax,
|
---|
1331 | uint32_t fFlagsFlow, uint32_t fFlagsDisasm, PDBGFFLOW phFlow)
|
---|
1332 | {
|
---|
1333 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
1334 | PVM pVM = pUVM->pVM;
|
---|
1335 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1336 | AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
1337 | AssertPtrReturn(pAddressStart, VERR_INVALID_POINTER);
|
---|
1338 | AssertReturn(!(fFlagsDisasm & ~DBGF_DISAS_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
1339 | AssertReturn((fFlagsDisasm & DBGF_DISAS_FLAGS_MODE_MASK) <= DBGF_DISAS_FLAGS_64BIT_MODE, VERR_INVALID_PARAMETER);
|
---|
1340 |
|
---|
1341 | /* Create the control flow graph container. */
|
---|
1342 | int rc = VINF_SUCCESS;
|
---|
1343 | PDBGFFLOWINT pThis = (PDBGFFLOWINT)RTMemAllocZ(sizeof(DBGFFLOWINT));
|
---|
1344 | if (RT_LIKELY(pThis))
|
---|
1345 | {
|
---|
1346 | rc = RTStrCacheCreate(&pThis->hStrCacheInstr, "DBGFFLOW");
|
---|
1347 | if (RT_SUCCESS(rc))
|
---|
1348 | {
|
---|
1349 | pThis->cRefs = 1;
|
---|
1350 | pThis->cRefsBb = 0;
|
---|
1351 | pThis->cBbs = 0;
|
---|
1352 | pThis->cBranchTbls = 0;
|
---|
1353 | pThis->fFlags = fFlagsFlow;
|
---|
1354 | RTListInit(&pThis->LstFlowBb);
|
---|
1355 | RTListInit(&pThis->LstBranchTbl);
|
---|
1356 | /* Create the entry basic block and start the work. */
|
---|
1357 |
|
---|
1358 | PDBGFFLOWBBINT pFlowBb = dbgfR3FlowBbCreate(pThis, pAddressStart, DBGF_FLOW_BB_F_ENTRY, 10);
|
---|
1359 | if (RT_LIKELY(pFlowBb))
|
---|
1360 | {
|
---|
1361 | dbgfR3FlowLink(pThis, pFlowBb);
|
---|
1362 | rc = dbgfR3FlowPopulate(pUVM, idCpu, pThis, pAddressStart, cbDisasmMax, fFlagsDisasm);
|
---|
1363 | if (RT_SUCCESS(rc))
|
---|
1364 | {
|
---|
1365 | *phFlow = pThis;
|
---|
1366 | return VINF_SUCCESS;
|
---|
1367 | }
|
---|
1368 | }
|
---|
1369 | else
|
---|
1370 | rc = VERR_NO_MEMORY;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | ASMAtomicDecU32(&pThis->cRefs);
|
---|
1374 | dbgfR3FlowDestroy(pThis);
|
---|
1375 | }
|
---|
1376 | else
|
---|
1377 | rc = VERR_NO_MEMORY;
|
---|
1378 |
|
---|
1379 | return rc;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 |
|
---|
1383 | /**
|
---|
1384 | * Retains the control flow graph handle.
|
---|
1385 | *
|
---|
1386 | * @returns Current reference count.
|
---|
1387 | * @param hFlow The control flow graph handle to retain.
|
---|
1388 | */
|
---|
1389 | VMMR3DECL(uint32_t) DBGFR3FlowRetain(DBGFFLOW hFlow)
|
---|
1390 | {
|
---|
1391 | PDBGFFLOWINT pThis = hFlow;
|
---|
1392 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
1393 |
|
---|
1394 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
1395 | AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
|
---|
1396 | return cRefs;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 |
|
---|
1400 | /**
|
---|
1401 | * Releases the control flow graph handle.
|
---|
1402 | *
|
---|
1403 | * @returns Current reference count, on 0 the control flow graph will be destroyed.
|
---|
1404 | * @param hFlow The control flow graph handle to release.
|
---|
1405 | */
|
---|
1406 | VMMR3DECL(uint32_t) DBGFR3FlowRelease(DBGFFLOW hFlow)
|
---|
1407 | {
|
---|
1408 | PDBGFFLOWINT pThis = hFlow;
|
---|
1409 | if (!pThis)
|
---|
1410 | return 0;
|
---|
1411 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
1412 |
|
---|
1413 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
1414 | AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
|
---|
1415 | if (cRefs == 0)
|
---|
1416 | dbgfR3FlowDestroy(pThis);
|
---|
1417 | return cRefs;
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 |
|
---|
1421 | /**
|
---|
1422 | * Queries the basic block denoting the entry point into the control flow graph.
|
---|
1423 | *
|
---|
1424 | * @returns VBox status code.
|
---|
1425 | * @param hFlow The control flow graph handle.
|
---|
1426 | * @param phFlowBb Where to store the basic block handle on success.
|
---|
1427 | */
|
---|
1428 | VMMR3DECL(int) DBGFR3FlowQueryStartBb(DBGFFLOW hFlow, PDBGFFLOWBB phFlowBb)
|
---|
1429 | {
|
---|
1430 | PDBGFFLOWINT pThis = hFlow;
|
---|
1431 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1432 |
|
---|
1433 | PDBGFFLOWBBINT pFlowBb = NULL;
|
---|
1434 | RTListForEach(&pThis->LstFlowBb, pFlowBb, DBGFFLOWBBINT, NdFlowBb)
|
---|
1435 | {
|
---|
1436 | if (pFlowBb->fFlags & DBGF_FLOW_BB_F_ENTRY)
|
---|
1437 | {
|
---|
1438 | *phFlowBb = pFlowBb;
|
---|
1439 | return VINF_SUCCESS;
|
---|
1440 | }
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | AssertFailed(); /* Should never get here. */
|
---|
1444 | return VERR_INTERNAL_ERROR;
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 |
|
---|
1448 | /**
|
---|
1449 | * Queries a basic block in the given control flow graph which covers the given
|
---|
1450 | * address.
|
---|
1451 | *
|
---|
1452 | * @returns VBox status code.
|
---|
1453 | * @retval VERR_NOT_FOUND if there is no basic block intersecting with the address.
|
---|
1454 | * @param hFlow The control flow graph handle.
|
---|
1455 | * @param pAddr The address to look for.
|
---|
1456 | * @param phFlowBb Where to store the basic block handle on success.
|
---|
1457 | */
|
---|
1458 | VMMR3DECL(int) DBGFR3FlowQueryBbByAddress(DBGFFLOW hFlow, PDBGFADDRESS pAddr, PDBGFFLOWBB phFlowBb)
|
---|
1459 | {
|
---|
1460 | PDBGFFLOWINT pThis = hFlow;
|
---|
1461 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1462 | AssertPtrReturn(pAddr, VERR_INVALID_POINTER);
|
---|
1463 | AssertPtrReturn(phFlowBb, VERR_INVALID_POINTER);
|
---|
1464 |
|
---|
1465 | PDBGFFLOWBBINT pFlowBb = NULL;
|
---|
1466 | RTListForEach(&pThis->LstFlowBb, pFlowBb, DBGFFLOWBBINT, NdFlowBb)
|
---|
1467 | {
|
---|
1468 | if (dbgfR3FlowAddrIntersect(pFlowBb, pAddr))
|
---|
1469 | {
|
---|
1470 | DBGFR3FlowBbRetain(pFlowBb);
|
---|
1471 | *phFlowBb = pFlowBb;
|
---|
1472 | return VINF_SUCCESS;
|
---|
1473 | }
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | return VERR_NOT_FOUND;
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 |
|
---|
1480 | /**
|
---|
1481 | * Queries a branch table in the given control flow graph by the given address.
|
---|
1482 | *
|
---|
1483 | * @returns VBox status code.
|
---|
1484 | * @retval VERR_NOT_FOUND if there is no branch table with the given address.
|
---|
1485 | * @param hFlow The control flow graph handle.
|
---|
1486 | * @param pAddr The address of the branch table.
|
---|
1487 | * @param phFlowBranchTbl Where to store the handle to branch table on success.
|
---|
1488 | *
|
---|
1489 | * @note Call DBGFR3FlowBranchTblRelease() when the handle is not required anymore.
|
---|
1490 | */
|
---|
1491 | VMMR3DECL(int) DBGFR3FlowQueryBranchTblByAddress(DBGFFLOW hFlow, PDBGFADDRESS pAddr, PDBGFFLOWBRANCHTBL phFlowBranchTbl)
|
---|
1492 | {
|
---|
1493 | PDBGFFLOWINT pThis = hFlow;
|
---|
1494 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1495 | AssertPtrReturn(pAddr, VERR_INVALID_POINTER);
|
---|
1496 | AssertPtrReturn(phFlowBranchTbl, VERR_INVALID_POINTER);
|
---|
1497 |
|
---|
1498 | PDBGFFLOWBRANCHTBLINT pBranchTbl = dbgfR3FlowBranchTblFindByAddr(pThis, pAddr);
|
---|
1499 | if (pBranchTbl)
|
---|
1500 | {
|
---|
1501 | DBGFR3FlowBranchTblRetain(pBranchTbl);
|
---|
1502 | *phFlowBranchTbl = pBranchTbl;
|
---|
1503 | return VINF_SUCCESS;
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | return VERR_NOT_FOUND;
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 |
|
---|
1510 | /**
|
---|
1511 | * Returns the number of basic blcoks inside the control flow graph.
|
---|
1512 | *
|
---|
1513 | * @returns Number of basic blocks.
|
---|
1514 | * @param hFlow The control flow graph handle.
|
---|
1515 | */
|
---|
1516 | VMMR3DECL(uint32_t) DBGFR3FlowGetBbCount(DBGFFLOW hFlow)
|
---|
1517 | {
|
---|
1518 | PDBGFFLOWINT pThis = hFlow;
|
---|
1519 | AssertPtrReturn(pThis, 0);
|
---|
1520 |
|
---|
1521 | return pThis->cBbs;
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 |
|
---|
1525 | /**
|
---|
1526 | * Retains the basic block handle.
|
---|
1527 | *
|
---|
1528 | * @returns Current reference count.
|
---|
1529 | * @param hFlowBb The basic block handle to retain.
|
---|
1530 | */
|
---|
1531 | VMMR3DECL(uint32_t) DBGFR3FlowBbRetain(DBGFFLOWBB hFlowBb)
|
---|
1532 | {
|
---|
1533 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1534 | AssertPtrReturn(pFlowBb, UINT32_MAX);
|
---|
1535 |
|
---|
1536 | uint32_t cRefs = ASMAtomicIncU32(&pFlowBb->cRefs);
|
---|
1537 | AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p %d\n", cRefs, pFlowBb, pFlowBb->enmEndType));
|
---|
1538 | return cRefs;
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 |
|
---|
1542 | /**
|
---|
1543 | * Releases the basic block handle.
|
---|
1544 | *
|
---|
1545 | * @returns Current reference count, on 0 the basic block will be destroyed.
|
---|
1546 | * @param hFlowBb The basic block handle to release.
|
---|
1547 | */
|
---|
1548 | VMMR3DECL(uint32_t) DBGFR3FlowBbRelease(DBGFFLOWBB hFlowBb)
|
---|
1549 | {
|
---|
1550 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1551 | if (!pFlowBb)
|
---|
1552 | return 0;
|
---|
1553 |
|
---|
1554 | return dbgfR3FlowBbReleaseInt(pFlowBb, true /* fMayDestroyFlow */);
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 |
|
---|
1558 | /**
|
---|
1559 | * Returns the start address of the basic block.
|
---|
1560 | *
|
---|
1561 | * @returns Pointer to DBGF adress containing the start address of the basic block.
|
---|
1562 | * @param hFlowBb The basic block handle.
|
---|
1563 | * @param pAddrStart Where to store the start address of the basic block.
|
---|
1564 | */
|
---|
1565 | VMMR3DECL(PDBGFADDRESS) DBGFR3FlowBbGetStartAddress(DBGFFLOWBB hFlowBb, PDBGFADDRESS pAddrStart)
|
---|
1566 | {
|
---|
1567 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1568 | AssertPtrReturn(pFlowBb, NULL);
|
---|
1569 | AssertPtrReturn(pAddrStart, NULL);
|
---|
1570 |
|
---|
1571 | *pAddrStart = pFlowBb->AddrStart;
|
---|
1572 | return pAddrStart;
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 |
|
---|
1576 | /**
|
---|
1577 | * Returns the end address of the basic block (inclusive).
|
---|
1578 | *
|
---|
1579 | * @returns Pointer to DBGF adress containing the end address of the basic block.
|
---|
1580 | * @param hFlowBb The basic block handle.
|
---|
1581 | * @param pAddrEnd Where to store the end address of the basic block.
|
---|
1582 | */
|
---|
1583 | VMMR3DECL(PDBGFADDRESS) DBGFR3FlowBbGetEndAddress(DBGFFLOWBB hFlowBb, PDBGFADDRESS pAddrEnd)
|
---|
1584 | {
|
---|
1585 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1586 | AssertPtrReturn(pFlowBb, NULL);
|
---|
1587 | AssertPtrReturn(pAddrEnd, NULL);
|
---|
1588 |
|
---|
1589 | *pAddrEnd = pFlowBb->AddrEnd;
|
---|
1590 | return pAddrEnd;
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 |
|
---|
1594 | /**
|
---|
1595 | * Returns the address the last instruction in the basic block branches to.
|
---|
1596 | *
|
---|
1597 | * @returns Pointer to DBGF adress containing the branch address of the basic block.
|
---|
1598 | * @param hFlowBb The basic block handle.
|
---|
1599 | * @param pAddrTarget Where to store the branch address of the basic block.
|
---|
1600 | *
|
---|
1601 | * @note This is only valid for unconditional or conditional branches and will assert
|
---|
1602 | * for every other basic block type.
|
---|
1603 | */
|
---|
1604 | VMMR3DECL(PDBGFADDRESS) DBGFR3FlowBbGetBranchAddress(DBGFFLOWBB hFlowBb, PDBGFADDRESS pAddrTarget)
|
---|
1605 | {
|
---|
1606 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1607 | AssertPtrReturn(pFlowBb, NULL);
|
---|
1608 | AssertPtrReturn(pAddrTarget, NULL);
|
---|
1609 | AssertReturn( pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_UNCOND_JMP
|
---|
1610 | || pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_COND,
|
---|
1611 | NULL);
|
---|
1612 |
|
---|
1613 | *pAddrTarget = pFlowBb->AddrTarget;
|
---|
1614 | return pAddrTarget;
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 |
|
---|
1618 | /**
|
---|
1619 | * Returns the address of the next block following this one in the instruction stream.
|
---|
1620 | * (usually end address + 1).
|
---|
1621 | *
|
---|
1622 | * @returns Pointer to DBGF adress containing the following address of the basic block.
|
---|
1623 | * @param hFlowBb The basic block handle.
|
---|
1624 | * @param pAddrFollow Where to store the following address of the basic block.
|
---|
1625 | *
|
---|
1626 | * @note This is only valid for conditional branches and if the last instruction in the
|
---|
1627 | * given basic block doesn't change the control flow but the blocks were split
|
---|
1628 | * because the successor is referenced by multiple other blocks as an entry point.
|
---|
1629 | */
|
---|
1630 | VMMR3DECL(PDBGFADDRESS) DBGFR3FlowBbGetFollowingAddress(DBGFFLOWBB hFlowBb, PDBGFADDRESS pAddrFollow)
|
---|
1631 | {
|
---|
1632 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1633 | AssertPtrReturn(pFlowBb, NULL);
|
---|
1634 | AssertPtrReturn(pAddrFollow, NULL);
|
---|
1635 | AssertReturn( pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_UNCOND
|
---|
1636 | || pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_COND,
|
---|
1637 | NULL);
|
---|
1638 |
|
---|
1639 | *pAddrFollow = pFlowBb->AddrEnd;
|
---|
1640 | DBGFR3AddrAdd(pAddrFollow, 1);
|
---|
1641 | return pAddrFollow;
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 |
|
---|
1645 | /**
|
---|
1646 | * Returns the type of the last instruction in the basic block.
|
---|
1647 | *
|
---|
1648 | * @returns Last instruction type.
|
---|
1649 | * @param hFlowBb The basic block handle.
|
---|
1650 | */
|
---|
1651 | VMMR3DECL(DBGFFLOWBBENDTYPE) DBGFR3FlowBbGetType(DBGFFLOWBB hFlowBb)
|
---|
1652 | {
|
---|
1653 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1654 | AssertPtrReturn(pFlowBb, DBGFFLOWBBENDTYPE_INVALID);
|
---|
1655 |
|
---|
1656 | return pFlowBb->enmEndType;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 |
|
---|
1660 | /**
|
---|
1661 | * Get the number of instructions contained in the basic block.
|
---|
1662 | *
|
---|
1663 | * @returns Number of instructions in the basic block.
|
---|
1664 | * @param hFlowBb The basic block handle.
|
---|
1665 | */
|
---|
1666 | VMMR3DECL(uint32_t) DBGFR3FlowBbGetInstrCount(DBGFFLOWBB hFlowBb)
|
---|
1667 | {
|
---|
1668 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1669 | AssertPtrReturn(pFlowBb, 0);
|
---|
1670 |
|
---|
1671 | return pFlowBb->cInstr;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 |
|
---|
1675 | /**
|
---|
1676 | * Get flags for the given basic block.
|
---|
1677 | *
|
---|
1678 | * @returns Combination of DBGF_FLOW_BB_F_*
|
---|
1679 | * @param hFlowBb The basic block handle.
|
---|
1680 | */
|
---|
1681 | VMMR3DECL(uint32_t) DBGFR3FlowBbGetFlags(DBGFFLOWBB hFlowBb)
|
---|
1682 | {
|
---|
1683 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1684 | AssertPtrReturn(pFlowBb, 0);
|
---|
1685 |
|
---|
1686 | return pFlowBb->fFlags;
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 |
|
---|
1690 | /**
|
---|
1691 | * Queries the branch table used if the given basic block ends with an indirect branch
|
---|
1692 | * and has a branch table referenced.
|
---|
1693 | *
|
---|
1694 | * @returns VBox status code.
|
---|
1695 | * @param hFlowBb The basic block handle.
|
---|
1696 | * @param phBranchTbl Where to store the branch table handle on success.
|
---|
1697 | *
|
---|
1698 | * @note Release the branch table reference with DBGFR3FlowBranchTblRelease() when not required
|
---|
1699 | * anymore.
|
---|
1700 | */
|
---|
1701 | VMMR3DECL(int) DBGFR3FlowBbQueryBranchTbl(DBGFFLOWBB hFlowBb, PDBGFFLOWBRANCHTBL phBranchTbl)
|
---|
1702 | {
|
---|
1703 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1704 | AssertPtrReturn(pFlowBb, VERR_INVALID_HANDLE);
|
---|
1705 | AssertReturn(pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP, VERR_INVALID_STATE);
|
---|
1706 | AssertPtrReturn(pFlowBb->pFlowBranchTbl, VERR_INVALID_STATE);
|
---|
1707 | AssertPtrReturn(phBranchTbl, VERR_INVALID_POINTER);
|
---|
1708 |
|
---|
1709 | DBGFR3FlowBranchTblRetain(pFlowBb->pFlowBranchTbl);
|
---|
1710 | *phBranchTbl = pFlowBb->pFlowBranchTbl;
|
---|
1711 | return VINF_SUCCESS;
|
---|
1712 | }
|
---|
1713 |
|
---|
1714 |
|
---|
1715 | /**
|
---|
1716 | * Returns the error status and message if the given basic block has an error.
|
---|
1717 | *
|
---|
1718 | * @returns VBox status code of the error for the basic block.
|
---|
1719 | * @param hFlowBb The basic block handle.
|
---|
1720 | * @param ppszErr Where to store the pointer to the error message - optional.
|
---|
1721 | */
|
---|
1722 | VMMR3DECL(int) DBGFR3FlowBbQueryError(DBGFFLOWBB hFlowBb, const char **ppszErr)
|
---|
1723 | {
|
---|
1724 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1725 | AssertPtrReturn(pFlowBb, VERR_INVALID_HANDLE);
|
---|
1726 |
|
---|
1727 | if (ppszErr)
|
---|
1728 | *ppszErr = pFlowBb->pszErr;
|
---|
1729 |
|
---|
1730 | return pFlowBb->rcError;
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 |
|
---|
1734 | /**
|
---|
1735 | * Store the disassembled instruction as a string in the given output buffer.
|
---|
1736 | *
|
---|
1737 | * @returns VBox status code.
|
---|
1738 | * @param hFlowBb The basic block handle.
|
---|
1739 | * @param idxInstr The instruction to query.
|
---|
1740 | * @param pAddrInstr Where to store the guest instruction address on success, optional.
|
---|
1741 | * @param pcbInstr Where to store the instruction size on success, optional.
|
---|
1742 | * @param ppszInstr Where to store the pointer to the disassembled instruction string, optional.
|
---|
1743 | */
|
---|
1744 | VMMR3DECL(int) DBGFR3FlowBbQueryInstr(DBGFFLOWBB hFlowBb, uint32_t idxInstr, PDBGFADDRESS pAddrInstr,
|
---|
1745 | uint32_t *pcbInstr, const char **ppszInstr)
|
---|
1746 | {
|
---|
1747 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1748 | AssertPtrReturn(pFlowBb, VERR_INVALID_POINTER);
|
---|
1749 | AssertReturn(idxInstr < pFlowBb->cInstr, VERR_INVALID_PARAMETER);
|
---|
1750 |
|
---|
1751 | if (pAddrInstr)
|
---|
1752 | *pAddrInstr = pFlowBb->aInstr[idxInstr].AddrInstr;
|
---|
1753 | if (pcbInstr)
|
---|
1754 | *pcbInstr = pFlowBb->aInstr[idxInstr].cbInstr;
|
---|
1755 | if (ppszInstr)
|
---|
1756 | *ppszInstr = pFlowBb->aInstr[idxInstr].pszInstr;
|
---|
1757 |
|
---|
1758 | return VINF_SUCCESS;
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 |
|
---|
1762 | /**
|
---|
1763 | * Queries the successors of the basic block.
|
---|
1764 | *
|
---|
1765 | * @returns VBox status code.
|
---|
1766 | * @param hFlowBb The basic block handle.
|
---|
1767 | * @param phFlowBbFollow Where to store the handle to the basic block following
|
---|
1768 | * this one (optional).
|
---|
1769 | * @param phFlowBbTarget Where to store the handle to the basic block being the
|
---|
1770 | * branch target for this one (optional).
|
---|
1771 | */
|
---|
1772 | VMMR3DECL(int) DBGFR3FlowBbQuerySuccessors(DBGFFLOWBB hFlowBb, PDBGFFLOWBB phFlowBbFollow, PDBGFFLOWBB phFlowBbTarget)
|
---|
1773 | {
|
---|
1774 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1775 | AssertPtrReturn(pFlowBb, VERR_INVALID_POINTER);
|
---|
1776 |
|
---|
1777 | if ( phFlowBbFollow
|
---|
1778 | && ( pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_UNCOND
|
---|
1779 | || pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_COND))
|
---|
1780 | {
|
---|
1781 | DBGFADDRESS AddrStart = pFlowBb->AddrEnd;
|
---|
1782 | DBGFR3AddrAdd(&AddrStart, 1);
|
---|
1783 | int rc = DBGFR3FlowQueryBbByAddress(pFlowBb->pFlow, &AddrStart, phFlowBbFollow);
|
---|
1784 | AssertRC(rc);
|
---|
1785 | }
|
---|
1786 |
|
---|
1787 | if ( phFlowBbTarget
|
---|
1788 | && ( pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_UNCOND_JMP
|
---|
1789 | || pFlowBb->enmEndType == DBGFFLOWBBENDTYPE_COND))
|
---|
1790 | {
|
---|
1791 | int rc = DBGFR3FlowQueryBbByAddress(pFlowBb->pFlow, &pFlowBb->AddrTarget, phFlowBbTarget);
|
---|
1792 | AssertRC(rc);
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | return VINF_SUCCESS;
|
---|
1796 | }
|
---|
1797 |
|
---|
1798 |
|
---|
1799 | /**
|
---|
1800 | * Returns the number of basic blocks referencing this basic block as a target.
|
---|
1801 | *
|
---|
1802 | * @returns Number of other basic blocks referencing this one.
|
---|
1803 | * @param hFlowBb The basic block handle.
|
---|
1804 | *
|
---|
1805 | * @note If the given basic block references itself (loop, etc.) this will be counted as well.
|
---|
1806 | */
|
---|
1807 | VMMR3DECL(uint32_t) DBGFR3FlowBbGetRefBbCount(DBGFFLOWBB hFlowBb)
|
---|
1808 | {
|
---|
1809 | PDBGFFLOWBBINT pFlowBb = hFlowBb;
|
---|
1810 | AssertPtrReturn(pFlowBb, 0);
|
---|
1811 |
|
---|
1812 | uint32_t cRefsBb = 0;
|
---|
1813 | PDBGFFLOWBBINT pFlowBbCur = NULL;
|
---|
1814 | RTListForEach(&pFlowBb->pFlow->LstFlowBb, pFlowBbCur, DBGFFLOWBBINT, NdFlowBb)
|
---|
1815 | {
|
---|
1816 | if (pFlowBbCur->fFlags & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
|
---|
1817 | continue;
|
---|
1818 |
|
---|
1819 | if ( pFlowBbCur->enmEndType == DBGFFLOWBBENDTYPE_UNCOND
|
---|
1820 | || pFlowBbCur->enmEndType == DBGFFLOWBBENDTYPE_COND)
|
---|
1821 | {
|
---|
1822 | DBGFADDRESS AddrStart = pFlowBb->AddrEnd;
|
---|
1823 | DBGFR3AddrAdd(&AddrStart, 1);
|
---|
1824 | if (dbgfR3FlowAddrEqual(&pFlowBbCur->AddrStart, &AddrStart))
|
---|
1825 | cRefsBb++;
|
---|
1826 | }
|
---|
1827 |
|
---|
1828 | if ( ( pFlowBbCur->enmEndType == DBGFFLOWBBENDTYPE_UNCOND_JMP
|
---|
1829 | || pFlowBbCur->enmEndType == DBGFFLOWBBENDTYPE_COND)
|
---|
1830 | && dbgfR3FlowAddrEqual(&pFlowBbCur->AddrStart, &pFlowBb->AddrTarget))
|
---|
1831 | cRefsBb++;
|
---|
1832 | }
|
---|
1833 | return cRefsBb;
|
---|
1834 | }
|
---|
1835 |
|
---|
1836 |
|
---|
1837 | /**
|
---|
1838 | * Returns the basic block handles referencing the given basic block.
|
---|
1839 | *
|
---|
1840 | * @returns VBox status code.
|
---|
1841 | * @retval VERR_BUFFER_OVERFLOW if the array can't hold all the basic blocks.
|
---|
1842 | * @param hFlowBb The basic block handle.
|
---|
1843 | * @param paFlowBbRef Pointer to the array containing the referencing basic block handles on success.
|
---|
1844 | * @param cRef Number of entries in the given array.
|
---|
1845 | */
|
---|
1846 | VMMR3DECL(int) DBGFR3FlowBbGetRefBb(DBGFFLOWBB hFlowBb, PDBGFFLOWBB paFlowBbRef, uint32_t cRef)
|
---|
1847 | {
|
---|
1848 | RT_NOREF3(hFlowBb, paFlowBbRef, cRef);
|
---|
1849 | return VERR_NOT_IMPLEMENTED;
|
---|
1850 | }
|
---|
1851 |
|
---|
1852 |
|
---|
1853 | /**
|
---|
1854 | * Retains a reference for the given control flow graph branch table.
|
---|
1855 | *
|
---|
1856 | * @returns new reference count.
|
---|
1857 | * @param hFlowBranchTbl The branch table handle.
|
---|
1858 | */
|
---|
1859 | VMMR3DECL(uint32_t) DBGFR3FlowBranchTblRetain(DBGFFLOWBRANCHTBL hFlowBranchTbl)
|
---|
1860 | {
|
---|
1861 | PDBGFFLOWBRANCHTBLINT pFlowBranchTbl = hFlowBranchTbl;
|
---|
1862 | AssertPtrReturn(pFlowBranchTbl, UINT32_MAX);
|
---|
1863 |
|
---|
1864 | uint32_t cRefs = ASMAtomicIncU32(&pFlowBranchTbl->cRefs);
|
---|
1865 | AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pFlowBranchTbl));
|
---|
1866 | return cRefs;
|
---|
1867 | }
|
---|
1868 |
|
---|
1869 |
|
---|
1870 | /**
|
---|
1871 | * Releases a given branch table handle.
|
---|
1872 | *
|
---|
1873 | * @returns the new reference count of the given branch table, on 0 it is destroyed.
|
---|
1874 | * @param hFlowBranchTbl The branch table handle.
|
---|
1875 | */
|
---|
1876 | VMMR3DECL(uint32_t) DBGFR3FlowBranchTblRelease(DBGFFLOWBRANCHTBL hFlowBranchTbl)
|
---|
1877 | {
|
---|
1878 | PDBGFFLOWBRANCHTBLINT pFlowBranchTbl = hFlowBranchTbl;
|
---|
1879 | if (!pFlowBranchTbl)
|
---|
1880 | return 0;
|
---|
1881 | AssertPtrReturn(pFlowBranchTbl, UINT32_MAX);
|
---|
1882 |
|
---|
1883 | uint32_t cRefs = ASMAtomicDecU32(&pFlowBranchTbl->cRefs);
|
---|
1884 | AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pFlowBranchTbl));
|
---|
1885 | if (cRefs == 0)
|
---|
1886 | dbgfR3FlowBranchTblDestroy(pFlowBranchTbl);
|
---|
1887 | return cRefs;
|
---|
1888 | }
|
---|
1889 |
|
---|
1890 |
|
---|
1891 | /**
|
---|
1892 | * Return the number of slots the branch table has.
|
---|
1893 | *
|
---|
1894 | * @returns Number of slots in the branch table.
|
---|
1895 | * @param hFlowBranchTbl The branch table handle.
|
---|
1896 | */
|
---|
1897 | VMMR3DECL(uint32_t) DBGFR3FlowBranchTblGetSlots(DBGFFLOWBRANCHTBL hFlowBranchTbl)
|
---|
1898 | {
|
---|
1899 | PDBGFFLOWBRANCHTBLINT pFlowBranchTbl = hFlowBranchTbl;
|
---|
1900 | AssertPtrReturn(pFlowBranchTbl, 0);
|
---|
1901 |
|
---|
1902 | return pFlowBranchTbl->cSlots;
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 |
|
---|
1906 | /**
|
---|
1907 | * Returns the start address of the branch table in the guest.
|
---|
1908 | *
|
---|
1909 | * @returns Pointer to start address of the branch table (pAddrStart).
|
---|
1910 | * @param hFlowBranchTbl The branch table handle.
|
---|
1911 | * @param pAddrStart Where to store the branch table address.
|
---|
1912 | */
|
---|
1913 | VMMR3DECL(PDBGFADDRESS) DBGFR3FlowBranchTblGetStartAddress(DBGFFLOWBRANCHTBL hFlowBranchTbl, PDBGFADDRESS pAddrStart)
|
---|
1914 | {
|
---|
1915 | PDBGFFLOWBRANCHTBLINT pFlowBranchTbl = hFlowBranchTbl;
|
---|
1916 | AssertPtrReturn(pFlowBranchTbl, NULL);
|
---|
1917 | AssertPtrReturn(pAddrStart, NULL);
|
---|
1918 |
|
---|
1919 | *pAddrStart = pFlowBranchTbl->AddrStart;
|
---|
1920 | return pAddrStart;
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 |
|
---|
1924 | /**
|
---|
1925 | * Query all addresses contained in the given branch table.
|
---|
1926 | *
|
---|
1927 | * @returns VBox status code.
|
---|
1928 | * @retval VERR_BUFFER_OVERFLOW if there is not enough space in the array to hold all addresses.
|
---|
1929 | * @param hFlowBranchTbl The branch table handle.
|
---|
1930 | * @param paAddrs Where to store the addresses on success.
|
---|
1931 | * @param cAddrs Number of entries the array can hold.
|
---|
1932 | */
|
---|
1933 | VMMR3DECL(int) DBGFR3FlowBranchTblQueryAddresses(DBGFFLOWBRANCHTBL hFlowBranchTbl, PDBGFADDRESS paAddrs, uint32_t cAddrs)
|
---|
1934 | {
|
---|
1935 | PDBGFFLOWBRANCHTBLINT pFlowBranchTbl = hFlowBranchTbl;
|
---|
1936 | AssertPtrReturn(pFlowBranchTbl, VERR_INVALID_HANDLE);
|
---|
1937 | AssertPtrReturn(paAddrs, VERR_INVALID_POINTER);
|
---|
1938 | AssertReturn(cAddrs > 0, VERR_INVALID_PARAMETER);
|
---|
1939 |
|
---|
1940 | if (cAddrs < pFlowBranchTbl->cSlots)
|
---|
1941 | return VERR_BUFFER_OVERFLOW;
|
---|
1942 |
|
---|
1943 | memcpy(paAddrs, &pFlowBranchTbl->aAddresses[0], pFlowBranchTbl->cSlots * sizeof(DBGFADDRESS));
|
---|
1944 | return VINF_SUCCESS;
|
---|
1945 | }
|
---|
1946 |
|
---|
1947 |
|
---|
1948 | /**
|
---|
1949 | * @callback_method_impl{FNRTSORTCMP}
|
---|
1950 | */
|
---|
1951 | static DECLCALLBACK(int) dbgfR3FlowItSortCmp(void const *pvElement1, void const *pvElement2, void *pvUser)
|
---|
1952 | {
|
---|
1953 | PDBGFFLOWITORDER penmOrder = (PDBGFFLOWITORDER)pvUser;
|
---|
1954 | PDBGFFLOWBBINT pFlowBb1 = *(PDBGFFLOWBBINT *)pvElement1;
|
---|
1955 | PDBGFFLOWBBINT pFlowBb2 = *(PDBGFFLOWBBINT *)pvElement2;
|
---|
1956 |
|
---|
1957 | if (dbgfR3FlowAddrEqual(&pFlowBb1->AddrStart, &pFlowBb2->AddrStart))
|
---|
1958 | return 0;
|
---|
1959 |
|
---|
1960 | if (*penmOrder == DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST)
|
---|
1961 | {
|
---|
1962 | if (dbgfR3FlowAddrLower(&pFlowBb1->AddrStart, &pFlowBb2->AddrStart))
|
---|
1963 | return -1;
|
---|
1964 | else
|
---|
1965 | return 1;
|
---|
1966 | }
|
---|
1967 | else
|
---|
1968 | {
|
---|
1969 | if (dbgfR3FlowAddrLower(&pFlowBb1->AddrStart, &pFlowBb2->AddrStart))
|
---|
1970 | return 1;
|
---|
1971 | else
|
---|
1972 | return -1;
|
---|
1973 | }
|
---|
1974 | }
|
---|
1975 |
|
---|
1976 |
|
---|
1977 | /**
|
---|
1978 | * Creates a new iterator for the given control flow graph.
|
---|
1979 | *
|
---|
1980 | * @returns VBox status code.
|
---|
1981 | * @param hFlow The control flow graph handle.
|
---|
1982 | * @param enmOrder The order in which the basic blocks are enumerated.
|
---|
1983 | * @param phFlowIt Where to store the handle to the iterator on success.
|
---|
1984 | */
|
---|
1985 | VMMR3DECL(int) DBGFR3FlowItCreate(DBGFFLOW hFlow, DBGFFLOWITORDER enmOrder, PDBGFFLOWIT phFlowIt)
|
---|
1986 | {
|
---|
1987 | int rc = VINF_SUCCESS;
|
---|
1988 | PDBGFFLOWINT pFlow = hFlow;
|
---|
1989 | AssertPtrReturn(pFlow, VERR_INVALID_POINTER);
|
---|
1990 | AssertPtrReturn(phFlowIt, VERR_INVALID_POINTER);
|
---|
1991 | AssertReturn(enmOrder > DBGFFLOWITORDER_INVALID && enmOrder < DBGFFLOWITORDER_BREADTH_FIRST,
|
---|
1992 | VERR_INVALID_PARAMETER);
|
---|
1993 | AssertReturn(enmOrder < DBGFFLOWITORDER_DEPTH_FRIST, VERR_NOT_IMPLEMENTED); /** @todo */
|
---|
1994 |
|
---|
1995 | PDBGFFLOWITINT pIt = (PDBGFFLOWITINT)RTMemAllocZ(RT_OFFSETOF(DBGFFLOWITINT, apBb[pFlow->cBbs]));
|
---|
1996 | if (RT_LIKELY(pIt))
|
---|
1997 | {
|
---|
1998 | DBGFR3FlowRetain(hFlow);
|
---|
1999 | pIt->pFlow = pFlow;
|
---|
2000 | pIt->idxBbNext = 0;
|
---|
2001 | /* Fill the list and then sort. */
|
---|
2002 | PDBGFFLOWBBINT pFlowBb;
|
---|
2003 | uint32_t idxBb = 0;
|
---|
2004 | RTListForEach(&pFlow->LstFlowBb, pFlowBb, DBGFFLOWBBINT, NdFlowBb)
|
---|
2005 | {
|
---|
2006 | DBGFR3FlowBbRetain(pFlowBb);
|
---|
2007 | pIt->apBb[idxBb++] = pFlowBb;
|
---|
2008 | }
|
---|
2009 |
|
---|
2010 | /* Sort the blocks by address. */
|
---|
2011 | RTSortShell(&pIt->apBb[0], pFlow->cBbs, sizeof(PDBGFFLOWBBINT), dbgfR3FlowItSortCmp, &enmOrder);
|
---|
2012 |
|
---|
2013 | *phFlowIt = pIt;
|
---|
2014 | }
|
---|
2015 | else
|
---|
2016 | rc = VERR_NO_MEMORY;
|
---|
2017 |
|
---|
2018 | return rc;
|
---|
2019 | }
|
---|
2020 |
|
---|
2021 |
|
---|
2022 | /**
|
---|
2023 | * Destroys a given control flow graph iterator.
|
---|
2024 | *
|
---|
2025 | * @returns nothing.
|
---|
2026 | * @param hFlowIt The control flow graph iterator handle.
|
---|
2027 | */
|
---|
2028 | VMMR3DECL(void) DBGFR3FlowItDestroy(DBGFFLOWIT hFlowIt)
|
---|
2029 | {
|
---|
2030 | PDBGFFLOWITINT pIt = hFlowIt;
|
---|
2031 | AssertPtrReturnVoid(pIt);
|
---|
2032 |
|
---|
2033 | for (unsigned i = 0; i < pIt->pFlow->cBbs; i++)
|
---|
2034 | DBGFR3FlowBbRelease(pIt->apBb[i]);
|
---|
2035 |
|
---|
2036 | DBGFR3FlowRelease(pIt->pFlow);
|
---|
2037 | RTMemFree(pIt);
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 |
|
---|
2041 | /**
|
---|
2042 | * Returns the next basic block in the iterator or NULL if there is no
|
---|
2043 | * basic block left.
|
---|
2044 | *
|
---|
2045 | * @returns Handle to the next basic block in the iterator or NULL if the end
|
---|
2046 | * was reached.
|
---|
2047 | * @param hFlowIt The iterator handle.
|
---|
2048 | *
|
---|
2049 | * @note If a valid handle is returned it must be release with DBGFR3FlowBbRelease()
|
---|
2050 | * when not required anymore.
|
---|
2051 | */
|
---|
2052 | VMMR3DECL(DBGFFLOWBB) DBGFR3FlowItNext(DBGFFLOWIT hFlowIt)
|
---|
2053 | {
|
---|
2054 | PDBGFFLOWITINT pIt = hFlowIt;
|
---|
2055 | AssertPtrReturn(pIt, NULL);
|
---|
2056 |
|
---|
2057 | PDBGFFLOWBBINT pFlowBb = NULL;
|
---|
2058 | if (pIt->idxBbNext < pIt->pFlow->cBbs)
|
---|
2059 | {
|
---|
2060 | pFlowBb = pIt->apBb[pIt->idxBbNext++];
|
---|
2061 | DBGFR3FlowBbRetain(pFlowBb);
|
---|
2062 | }
|
---|
2063 |
|
---|
2064 | return pFlowBb;
|
---|
2065 | }
|
---|
2066 |
|
---|
2067 |
|
---|
2068 | /**
|
---|
2069 | * Resets the given iterator to the beginning.
|
---|
2070 | *
|
---|
2071 | * @returns VBox status code.
|
---|
2072 | * @param hFlowIt The iterator handle.
|
---|
2073 | */
|
---|
2074 | VMMR3DECL(int) DBGFR3FlowItReset(DBGFFLOWIT hFlowIt)
|
---|
2075 | {
|
---|
2076 | PDBGFFLOWITINT pIt = hFlowIt;
|
---|
2077 | AssertPtrReturn(pIt, VERR_INVALID_HANDLE);
|
---|
2078 |
|
---|
2079 | pIt->idxBbNext = 0;
|
---|
2080 | return VINF_SUCCESS;
|
---|
2081 | }
|
---|
2082 |
|
---|
2083 |
|
---|
2084 | /**
|
---|
2085 | * @callback_method_impl{FNRTSORTCMP}
|
---|
2086 | */
|
---|
2087 | static DECLCALLBACK(int) dbgfR3FlowBranchTblItSortCmp(void const *pvElement1, void const *pvElement2, void *pvUser)
|
---|
2088 | {
|
---|
2089 | PDBGFFLOWITORDER penmOrder = (PDBGFFLOWITORDER)pvUser;
|
---|
2090 | PDBGFFLOWBRANCHTBLINT pTbl1 = *(PDBGFFLOWBRANCHTBLINT *)pvElement1;
|
---|
2091 | PDBGFFLOWBRANCHTBLINT pTbl2 = *(PDBGFFLOWBRANCHTBLINT *)pvElement2;
|
---|
2092 |
|
---|
2093 | if (dbgfR3FlowAddrEqual(&pTbl1->AddrStart, &pTbl2->AddrStart))
|
---|
2094 | return 0;
|
---|
2095 |
|
---|
2096 | if (*penmOrder == DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST)
|
---|
2097 | {
|
---|
2098 | if (dbgfR3FlowAddrLower(&pTbl1->AddrStart, &pTbl2->AddrStart))
|
---|
2099 | return -1;
|
---|
2100 | else
|
---|
2101 | return 1;
|
---|
2102 | }
|
---|
2103 | else
|
---|
2104 | {
|
---|
2105 | if (dbgfR3FlowAddrLower(&pTbl1->AddrStart, &pTbl2->AddrStart))
|
---|
2106 | return 1;
|
---|
2107 | else
|
---|
2108 | return -1;
|
---|
2109 | }
|
---|
2110 | }
|
---|
2111 |
|
---|
2112 |
|
---|
2113 | /**
|
---|
2114 | * Creates a new branch table iterator for the given control flow graph.
|
---|
2115 | *
|
---|
2116 | * @returns VBox status code.
|
---|
2117 | * @param hFlow The control flow graph handle.
|
---|
2118 | * @param enmOrder The order in which the basic blocks are enumerated.
|
---|
2119 | * @param phFlowBranchTblIt Where to store the handle to the iterator on success.
|
---|
2120 | */
|
---|
2121 | VMMR3DECL(int) DBGFR3FlowBranchTblItCreate(DBGFFLOW hFlow, DBGFFLOWITORDER enmOrder,
|
---|
2122 | PDBGFFLOWBRANCHTBLIT phFlowBranchTblIt)
|
---|
2123 | {
|
---|
2124 | int rc = VINF_SUCCESS;
|
---|
2125 | PDBGFFLOWINT pFlow = hFlow;
|
---|
2126 | AssertPtrReturn(pFlow, VERR_INVALID_POINTER);
|
---|
2127 | AssertPtrReturn(phFlowBranchTblIt, VERR_INVALID_POINTER);
|
---|
2128 | AssertReturn(enmOrder > DBGFFLOWITORDER_INVALID && enmOrder < DBGFFLOWITORDER_BREADTH_FIRST,
|
---|
2129 | VERR_INVALID_PARAMETER);
|
---|
2130 | AssertReturn(enmOrder < DBGFFLOWITORDER_DEPTH_FRIST, VERR_NOT_SUPPORTED);
|
---|
2131 |
|
---|
2132 | PDBGFFLOWBRANCHTBLITINT pIt = (PDBGFFLOWBRANCHTBLITINT)RTMemAllocZ(RT_OFFSETOF(DBGFFLOWBRANCHTBLITINT, apBranchTbl[pFlow->cBranchTbls]));
|
---|
2133 | if (RT_LIKELY(pIt))
|
---|
2134 | {
|
---|
2135 | DBGFR3FlowRetain(hFlow);
|
---|
2136 | pIt->pFlow = pFlow;
|
---|
2137 | pIt->idxTblNext = 0;
|
---|
2138 | /* Fill the list and then sort. */
|
---|
2139 | PDBGFFLOWBRANCHTBLINT pFlowBranchTbl;
|
---|
2140 | uint32_t idxTbl = 0;
|
---|
2141 | RTListForEach(&pFlow->LstBranchTbl, pFlowBranchTbl, DBGFFLOWBRANCHTBLINT, NdBranchTbl)
|
---|
2142 | {
|
---|
2143 | DBGFR3FlowBranchTblRetain(pFlowBranchTbl);
|
---|
2144 | pIt->apBranchTbl[idxTbl++] = pFlowBranchTbl;
|
---|
2145 | }
|
---|
2146 |
|
---|
2147 | /* Sort the blocks by address. */
|
---|
2148 | RTSortShell(&pIt->apBranchTbl[0], pFlow->cBranchTbls, sizeof(PDBGFFLOWBRANCHTBLINT), dbgfR3FlowBranchTblItSortCmp, &enmOrder);
|
---|
2149 |
|
---|
2150 | *phFlowBranchTblIt = pIt;
|
---|
2151 | }
|
---|
2152 | else
|
---|
2153 | rc = VERR_NO_MEMORY;
|
---|
2154 |
|
---|
2155 | return rc;
|
---|
2156 | }
|
---|
2157 |
|
---|
2158 |
|
---|
2159 | /**
|
---|
2160 | * Destroys a given control flow graph branch table iterator.
|
---|
2161 | *
|
---|
2162 | * @returns nothing.
|
---|
2163 | * @param hFlowBranchTblIt The control flow graph branch table iterator handle.
|
---|
2164 | */
|
---|
2165 | VMMR3DECL(void) DBGFR3FlowBranchTblItDestroy(DBGFFLOWBRANCHTBLIT hFlowBranchTblIt)
|
---|
2166 | {
|
---|
2167 | PDBGFFLOWBRANCHTBLITINT pIt = hFlowBranchTblIt;
|
---|
2168 | AssertPtrReturnVoid(pIt);
|
---|
2169 |
|
---|
2170 | for (unsigned i = 0; i < pIt->pFlow->cBranchTbls; i++)
|
---|
2171 | DBGFR3FlowBranchTblRelease(pIt->apBranchTbl[i]);
|
---|
2172 |
|
---|
2173 | DBGFR3FlowRelease(pIt->pFlow);
|
---|
2174 | RTMemFree(pIt);
|
---|
2175 | }
|
---|
2176 |
|
---|
2177 |
|
---|
2178 | /**
|
---|
2179 | * Returns the next branch table in the iterator or NULL if there is no
|
---|
2180 | * branch table left.
|
---|
2181 | *
|
---|
2182 | * @returns Handle to the next basic block in the iterator or NULL if the end
|
---|
2183 | * was reached.
|
---|
2184 | * @param hFlowBranchTblIt The iterator handle.
|
---|
2185 | *
|
---|
2186 | * @note If a valid handle is returned it must be release with DBGFR3FlowBranchTblRelease()
|
---|
2187 | * when not required anymore.
|
---|
2188 | */
|
---|
2189 | VMMR3DECL(DBGFFLOWBRANCHTBL) DBGFR3FlowBranchTblItNext(DBGFFLOWBRANCHTBLIT hFlowBranchTblIt)
|
---|
2190 | {
|
---|
2191 | PDBGFFLOWBRANCHTBLITINT pIt = hFlowBranchTblIt;
|
---|
2192 | AssertPtrReturn(pIt, NULL);
|
---|
2193 |
|
---|
2194 | PDBGFFLOWBRANCHTBLINT pTbl = NULL;
|
---|
2195 | if (pIt->idxTblNext < pIt->pFlow->cBranchTbls)
|
---|
2196 | {
|
---|
2197 | pTbl = pIt->apBranchTbl[pIt->idxTblNext++];
|
---|
2198 | DBGFR3FlowBranchTblRetain(pTbl);
|
---|
2199 | }
|
---|
2200 |
|
---|
2201 | return pTbl;
|
---|
2202 | }
|
---|
2203 |
|
---|
2204 |
|
---|
2205 | /**
|
---|
2206 | * Resets the given iterator to the beginning.
|
---|
2207 | *
|
---|
2208 | * @returns VBox status code.
|
---|
2209 | * @param hFlowBranchTblIt The iterator handle.
|
---|
2210 | */
|
---|
2211 | VMMR3DECL(int) DBGFR3FlowBranchTblItReset(DBGFFLOWBRANCHTBLIT hFlowBranchTblIt)
|
---|
2212 | {
|
---|
2213 | PDBGFFLOWBRANCHTBLITINT pIt = hFlowBranchTblIt;
|
---|
2214 | AssertPtrReturn(pIt, VERR_INVALID_HANDLE);
|
---|
2215 |
|
---|
2216 | pIt->idxTblNext = 0;
|
---|
2217 | return VINF_SUCCESS;
|
---|
2218 | }
|
---|