VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGF.cpp@ 77758

Last change on this file since 77758 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 69.1 KB
Line 
1/* $Id: DBGF.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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 DBGF - The Debugger Facility
20 *
21 * The purpose of the DBGF is to provide an interface for debuggers to
22 * manipulate the VMM without having to mess up the source code for each of
23 * them. The DBGF is always built in and will always work when a debugger
24 * attaches to the VM. The DBGF provides the basic debugger features, such as
25 * halting execution, handling breakpoints, single step execution, instruction
26 * disassembly, info querying, OS specific diggers, symbol and module
27 * management.
28 *
29 * The interface is working in a manner similar to the win32, linux and os2
30 * debugger interfaces. The interface has an asynchronous nature. This comes
31 * from the fact that the VMM and the Debugger are running in different threads.
32 * They are referred to as the "emulation thread" and the "debugger thread", or
33 * as the "ping thread" and the "pong thread, respectivly. (The last set of
34 * names comes from the use of the Ping-Pong synchronization construct from the
35 * RTSem API.)
36 *
37 * @see grp_dbgf
38 *
39 *
40 * @section sec_dbgf_scenario Usage Scenario
41 *
42 * The debugger starts by attaching to the VM. For practical reasons we limit the
43 * number of concurrently attached debuggers to 1 per VM. The action of
44 * attaching to the VM causes the VM to check and generate debug events.
45 *
46 * The debugger then will wait/poll for debug events and issue commands.
47 *
48 * The waiting and polling is done by the DBGFEventWait() function. It will wait
49 * for the emulation thread to send a ping, thus indicating that there is an
50 * event waiting to be processed.
51 *
52 * An event can be a response to a command issued previously, the hitting of a
53 * breakpoint, or running into a bad/fatal VMM condition. The debugger now has
54 * the ping and must respond to the event at hand - the VMM is waiting. This
55 * usually means that the user of the debugger must do something, but it doesn't
56 * have to. The debugger is free to call any DBGF function (nearly at least)
57 * while processing the event.
58 *
59 * Typically the user will issue a request for the execution to be resumed, so
60 * the debugger calls DBGFResume() and goes back to waiting/polling for events.
61 *
62 * When the user eventually terminates the debugging session or selects another
63 * VM, the debugger detaches from the VM. This means that breakpoints are
64 * disabled and that the emulation thread no longer polls for debugger commands.
65 *
66 */
67
68
69/*********************************************************************************************************************************
70* Header Files *
71*********************************************************************************************************************************/
72#define LOG_GROUP LOG_GROUP_DBGF
73#include <VBox/vmm/dbgf.h>
74#include <VBox/vmm/selm.h>
75#ifdef VBOX_WITH_REM
76# include <VBox/vmm/rem.h>
77#endif
78#include <VBox/vmm/em.h>
79#include <VBox/vmm/hm.h>
80#include "DBGFInternal.h"
81#include <VBox/vmm/vm.h>
82#include <VBox/vmm/uvm.h>
83#include <VBox/err.h>
84
85#include <VBox/log.h>
86#include <iprt/semaphore.h>
87#include <iprt/thread.h>
88#include <iprt/asm.h>
89#include <iprt/time.h>
90#include <iprt/assert.h>
91#include <iprt/stream.h>
92#include <iprt/env.h>
93
94
95/*********************************************************************************************************************************
96* Structures and Typedefs *
97*********************************************************************************************************************************/
98/**
99 * Instruction type returned by dbgfStepGetCurInstrType.
100 */
101typedef enum DBGFSTEPINSTRTYPE
102{
103 DBGFSTEPINSTRTYPE_INVALID = 0,
104 DBGFSTEPINSTRTYPE_OTHER,
105 DBGFSTEPINSTRTYPE_RET,
106 DBGFSTEPINSTRTYPE_CALL,
107 DBGFSTEPINSTRTYPE_END,
108 DBGFSTEPINSTRTYPE_32BIT_HACK = 0x7fffffff
109} DBGFSTEPINSTRTYPE;
110
111
112/*********************************************************************************************************************************
113* Internal Functions *
114*********************************************************************************************************************************/
115static int dbgfR3VMMWait(PVM pVM);
116static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution);
117static DECLCALLBACK(int) dbgfR3Attach(PVM pVM);
118static DBGFSTEPINSTRTYPE dbgfStepGetCurInstrType(PVM pVM, PVMCPU pVCpu);
119static bool dbgfStepAreWeThereYet(PVM pVM, PVMCPU pVCpu);
120
121
122/**
123 * Sets the VMM Debug Command variable.
124 *
125 * @returns Previous command.
126 * @param pVM The cross context VM structure.
127 * @param enmCmd The command.
128 */
129DECLINLINE(DBGFCMD) dbgfR3SetCmd(PVM pVM, DBGFCMD enmCmd)
130{
131 DBGFCMD rc;
132 if (enmCmd == DBGFCMD_NO_COMMAND)
133 {
134 Log2(("DBGF: Setting command to %d (DBGFCMD_NO_COMMAND)\n", enmCmd));
135 rc = (DBGFCMD)ASMAtomicXchgU32((uint32_t volatile *)(void *)&pVM->dbgf.s.enmVMMCmd, enmCmd);
136 VM_FF_CLEAR(pVM, VM_FF_DBGF);
137 }
138 else
139 {
140 Log2(("DBGF: Setting command to %d\n", enmCmd));
141 AssertMsg(pVM->dbgf.s.enmVMMCmd == DBGFCMD_NO_COMMAND, ("enmCmd=%d enmVMMCmd=%d\n", enmCmd, pVM->dbgf.s.enmVMMCmd));
142 rc = (DBGFCMD)ASMAtomicXchgU32((uint32_t volatile *)(void *)&pVM->dbgf.s.enmVMMCmd, enmCmd);
143 VM_FF_SET(pVM, VM_FF_DBGF);
144 VMR3NotifyGlobalFFU(pVM->pUVM, 0 /* didn't notify REM */);
145 }
146 return rc;
147}
148
149
150/**
151 * Initializes the DBGF.
152 *
153 * @returns VBox status code.
154 * @param pVM The cross context VM structure.
155 */
156VMMR3_INT_DECL(int) DBGFR3Init(PVM pVM)
157{
158 PUVM pUVM = pVM->pUVM;
159 AssertCompile(sizeof(pUVM->dbgf.s) <= sizeof(pUVM->dbgf.padding));
160 AssertCompile(sizeof(pUVM->aCpus[0].dbgf.s) <= sizeof(pUVM->aCpus[0].dbgf.padding));
161
162 pVM->dbgf.s.SteppingFilter.idCpu = NIL_VMCPUID;
163
164 /*
165 * The usual sideways mountain climbing style of init:
166 */
167 int rc = dbgfR3InfoInit(pUVM); /* (First, initalizes the shared critical section.) */
168 if (RT_SUCCESS(rc))
169 {
170 rc = dbgfR3TraceInit(pVM);
171 if (RT_SUCCESS(rc))
172 {
173 rc = dbgfR3RegInit(pUVM);
174 if (RT_SUCCESS(rc))
175 {
176 rc = dbgfR3AsInit(pUVM);
177 if (RT_SUCCESS(rc))
178 {
179 rc = dbgfR3BpInit(pVM);
180 if (RT_SUCCESS(rc))
181 {
182 rc = dbgfR3OSInit(pUVM);
183 if (RT_SUCCESS(rc))
184 {
185 rc = dbgfR3PlugInInit(pUVM);
186 if (RT_SUCCESS(rc))
187 {
188 rc = dbgfR3BugCheckInit(pVM);
189 if (RT_SUCCESS(rc))
190 {
191 return VINF_SUCCESS;
192 }
193 dbgfR3PlugInTerm(pUVM);
194 }
195 dbgfR3OSTermPart1(pUVM);
196 dbgfR3OSTermPart2(pUVM);
197 }
198 }
199 dbgfR3AsTerm(pUVM);
200 }
201 dbgfR3RegTerm(pUVM);
202 }
203 dbgfR3TraceTerm(pVM);
204 }
205 dbgfR3InfoTerm(pUVM);
206 }
207 return rc;
208}
209
210
211/**
212 * Terminates and cleans up resources allocated by the DBGF.
213 *
214 * @returns VBox status code.
215 * @param pVM The cross context VM structure.
216 */
217VMMR3_INT_DECL(int) DBGFR3Term(PVM pVM)
218{
219 PUVM pUVM = pVM->pUVM;
220
221 dbgfR3OSTermPart1(pUVM);
222 dbgfR3PlugInTerm(pUVM);
223 dbgfR3OSTermPart2(pUVM);
224 dbgfR3AsTerm(pUVM);
225 dbgfR3RegTerm(pUVM);
226 dbgfR3TraceTerm(pVM);
227 dbgfR3InfoTerm(pUVM);
228
229 return VINF_SUCCESS;
230}
231
232
233/**
234 * Called when the VM is powered off to detach debuggers.
235 *
236 * @param pVM The cross context VM structure.
237 */
238VMMR3_INT_DECL(void) DBGFR3PowerOff(PVM pVM)
239{
240
241 /*
242 * Send a termination event to any attached debugger.
243 */
244 /* wait to become the speaker (we should already be that). */
245 if ( pVM->dbgf.s.fAttached
246 && RTSemPingShouldWait(&pVM->dbgf.s.PingPong))
247 RTSemPingWait(&pVM->dbgf.s.PingPong, 5000);
248
249 if (pVM->dbgf.s.fAttached)
250 {
251 /* Just mark it as detached if we're not in a position to send a power
252 off event. It should fail later on. */
253 if (!RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
254 {
255 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
256 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
257 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
258 }
259
260 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
261 {
262 /* Try send the power off event. */
263 int rc;
264 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
265 if (enmCmd == DBGFCMD_DETACH_DEBUGGER)
266 /* the debugger beat us to initiating the detaching. */
267 rc = VINF_SUCCESS;
268 else
269 {
270 /* ignore the command (if any). */
271 enmCmd = DBGFCMD_NO_COMMAND;
272 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_POWERING_OFF;
273 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
274 rc = RTSemPing(&pVM->dbgf.s.PingPong);
275 }
276
277 /*
278 * Process commands and priority requests until we get a command
279 * indicating that the debugger has detached.
280 */
281 uint32_t cPollHack = 1;
282 PVMCPU pVCpu = VMMGetCpu(pVM);
283 while (RT_SUCCESS(rc))
284 {
285 if (enmCmd != DBGFCMD_NO_COMMAND)
286 {
287 /* process command */
288 bool fResumeExecution;
289 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
290 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
291 if (enmCmd == DBGFCMD_DETACHED_DEBUGGER)
292 break;
293 enmCmd = DBGFCMD_NO_COMMAND;
294 }
295 else
296 {
297 /* Wait for new command, processing pending priority requests
298 first. The request processing is a bit crazy, but
299 unfortunately required by plugin unloading. */
300 if ( VM_FF_IS_SET(pVM, VM_FF_REQUEST)
301 || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_REQUEST))
302 {
303 LogFlow(("DBGFR3PowerOff: Processes priority requests...\n"));
304 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
305 if (rc == VINF_SUCCESS)
306 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, true /*fPriorityOnly*/);
307 LogFlow(("DBGFR3PowerOff: VMR3ReqProcess -> %Rrc\n", rc));
308 cPollHack = 1;
309 }
310 /* Need to handle rendezvous too, for generic debug event management. */
311 else if (VM_FF_IS_SET(pVM, VM_FF_EMT_RENDEZVOUS))
312 {
313 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
314 AssertLogRel(rc == VINF_SUCCESS);
315 cPollHack = 1;
316 }
317 else if (cPollHack < 120)
318 cPollHack++;
319
320 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
321 if (RT_SUCCESS(rc))
322 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
323 else if (rc == VERR_TIMEOUT)
324 rc = VINF_SUCCESS;
325 }
326 }
327
328 /*
329 * Clear the FF so we won't get confused later on.
330 */
331 VM_FF_CLEAR(pVM, VM_FF_DBGF);
332 }
333 }
334}
335
336
337/**
338 * Applies relocations to data and code managed by this
339 * component. This function will be called at init and
340 * whenever the VMM need to relocate it self inside the GC.
341 *
342 * @param pVM The cross context VM structure.
343 * @param offDelta Relocation delta relative to old location.
344 */
345VMMR3_INT_DECL(void) DBGFR3Relocate(PVM pVM, RTGCINTPTR offDelta)
346{
347 dbgfR3TraceRelocate(pVM);
348 dbgfR3AsRelocate(pVM->pUVM, offDelta);
349}
350
351
352/**
353 * Waits a little while for a debuggger to attach.
354 *
355 * @returns True is a debugger have attached.
356 * @param pVM The cross context VM structure.
357 * @param pVCpu The cross context per CPU structure.
358 * @param enmEvent Event.
359 *
360 * @thread EMT(pVCpu)
361 */
362bool dbgfR3WaitForAttach(PVM pVM, PVMCPU pVCpu, DBGFEVENTTYPE enmEvent)
363{
364 /*
365 * First a message.
366 */
367#ifndef RT_OS_L4
368
369# if !defined(DEBUG) || defined(DEBUG_sandervl) || defined(DEBUG_frank)
370 int cWait = 10;
371# else
372 int cWait = !VM_IS_RAW_MODE_ENABLED(pVM)
373 && ( enmEvent == DBGFEVENT_ASSERTION_HYPER
374 || enmEvent == DBGFEVENT_FATAL_ERROR)
375 && !RTEnvExist("VBOX_DBGF_WAIT_FOR_ATTACH")
376 ? 10
377 : 150;
378# endif
379 RTStrmPrintf(g_pStdErr, "DBGF: No debugger attached, waiting %d second%s for one to attach (event=%d)\n",
380 cWait / 10, cWait != 10 ? "s" : "", enmEvent);
381 RTStrmFlush(g_pStdErr);
382 while (cWait > 0)
383 {
384 RTThreadSleep(100);
385 if (pVM->dbgf.s.fAttached)
386 {
387 RTStrmPrintf(g_pStdErr, "Attached!\n");
388 RTStrmFlush(g_pStdErr);
389 return true;
390 }
391
392 /* Process priority stuff. */
393 if ( VM_FF_IS_SET(pVM, VM_FF_REQUEST)
394 || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_REQUEST))
395 {
396 int rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
397 if (rc == VINF_SUCCESS)
398 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, true /*fPriorityOnly*/);
399 if (rc != VINF_SUCCESS)
400 {
401 RTStrmPrintf(g_pStdErr, "[rcReq=%Rrc, ignored!]", rc);
402 RTStrmFlush(g_pStdErr);
403 }
404 }
405
406 /* next */
407 if (!(cWait % 10))
408 {
409 RTStrmPrintf(g_pStdErr, "%d.", cWait / 10);
410 RTStrmFlush(g_pStdErr);
411 }
412 cWait--;
413 }
414#endif
415
416 RTStrmPrintf(g_pStdErr, "Stopping the VM!\n");
417 RTStrmFlush(g_pStdErr);
418 return false;
419}
420
421
422/**
423 * Forced action callback.
424 *
425 * The VMM will call this from it's main loop when either VM_FF_DBGF or
426 * VMCPU_FF_DBGF are set.
427 *
428 * The function checks for and executes pending commands from the debugger.
429 * Then it checks for pending debug events and serves these.
430 *
431 * @returns VINF_SUCCESS normally.
432 * @returns VERR_DBGF_RAISE_FATAL_ERROR to pretend a fatal error happened.
433 * @param pVM The cross context VM structure.
434 * @param pVCpu The cross context per CPU structure.
435 */
436VMMR3_INT_DECL(int) DBGFR3VMMForcedAction(PVM pVM, PVMCPU pVCpu)
437{
438 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
439
440 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_DBGF))
441 {
442 /*
443 * Command pending? Process it.
444 */
445 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
446 {
447 bool fResumeExecution;
448 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
449 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
450 rcStrict = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
451 if (!fResumeExecution)
452 rcStrict = dbgfR3VMMWait(pVM);
453 }
454 }
455
456 /*
457 * Dispatch pending events.
458 */
459 if (VMCPU_FF_TEST_AND_CLEAR(pVCpu, VMCPU_FF_DBGF))
460 {
461 if ( pVCpu->dbgf.s.cEvents > 0
462 && pVCpu->dbgf.s.aEvents[pVCpu->dbgf.s.cEvents - 1].enmState == DBGFEVENTSTATE_CURRENT)
463 {
464 VBOXSTRICTRC rcStrict2 = DBGFR3EventHandlePending(pVM, pVCpu);
465 if ( rcStrict2 != VINF_SUCCESS
466 && ( rcStrict == VINF_SUCCESS
467 || RT_FAILURE(rcStrict2)
468 || rcStrict2 < rcStrict) ) /** @todo oversimplified? */
469 rcStrict = rcStrict2;
470 }
471 }
472
473 return VBOXSTRICTRC_TODO(rcStrict);
474}
475
476
477/**
478 * Flag whether the event implies that we're stopped in the hypervisor code
479 * and have to block certain operations.
480 *
481 * @param pVM The cross context VM structure.
482 * @param enmEvent The event.
483 */
484static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
485{
486 switch (enmEvent)
487 {
488 case DBGFEVENT_STEPPED_HYPER:
489 case DBGFEVENT_ASSERTION_HYPER:
490 case DBGFEVENT_BREAKPOINT_HYPER:
491 pVM->dbgf.s.fStoppedInHyper = true;
492 break;
493 default:
494 pVM->dbgf.s.fStoppedInHyper = false;
495 break;
496 }
497}
498
499
500/**
501 * Try to determine the event context.
502 *
503 * @returns debug event context.
504 * @param pVM The cross context VM structure.
505 */
506static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
507{
508 /** @todo SMP support! */
509 PVMCPU pVCpu = &pVM->aCpus[0];
510
511 switch (EMGetState(pVCpu))
512 {
513 case EMSTATE_RAW:
514 case EMSTATE_DEBUG_GUEST_RAW:
515 return DBGFEVENTCTX_RAW;
516
517 case EMSTATE_REM:
518 case EMSTATE_DEBUG_GUEST_REM:
519 return DBGFEVENTCTX_REM;
520
521 case EMSTATE_DEBUG_HYPER:
522 case EMSTATE_GURU_MEDITATION:
523 return DBGFEVENTCTX_HYPER;
524
525 default:
526 return DBGFEVENTCTX_OTHER;
527 }
528}
529
530/**
531 * The common event prologue code.
532 * It will set the 'stopped-in-hyper' flag, make sure someone is attached,
533 * and perhaps process any high priority pending actions (none yet).
534 *
535 * @returns VBox status code.
536 * @param pVM The cross context VM structure.
537 * @param enmEvent The event to be sent.
538 */
539static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
540{
541 /** @todo SMP */
542 PVMCPU pVCpu = VMMGetCpu(pVM);
543
544 /*
545 * Check if a debugger is attached.
546 */
547 if ( !pVM->dbgf.s.fAttached
548 && !dbgfR3WaitForAttach(pVM, pVCpu, enmEvent))
549 {
550 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
551 return VERR_DBGF_NOT_ATTACHED;
552 }
553
554 /*
555 * Sync back the state from the REM.
556 */
557 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
558#ifdef VBOX_WITH_REM
559 if (!pVM->dbgf.s.fStoppedInHyper)
560 REMR3StateUpdate(pVM, pVCpu);
561#endif
562
563 /*
564 * Look thru pending commands and finish those which make sense now.
565 */
566 /** @todo Process/purge pending commands. */
567 //int rc = DBGFR3VMMForcedAction(pVM);
568 return VINF_SUCCESS;
569}
570
571
572/**
573 * Sends the event in the event buffer.
574 *
575 * @returns VBox status code.
576 * @param pVM The cross context VM structure.
577 */
578static int dbgfR3SendEvent(PVM pVM)
579{
580 pVM->dbgf.s.SteppingFilter.idCpu = NIL_VMCPUID;
581
582 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
583 if (RT_SUCCESS(rc))
584 rc = dbgfR3VMMWait(pVM);
585
586 pVM->dbgf.s.fStoppedInHyper = false;
587 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
588 return rc;
589}
590
591
592/**
593 * Processes a pending event on the current CPU.
594 *
595 * This is called by EM in response to VINF_EM_DBG_EVENT.
596 *
597 * @returns Strict VBox status code.
598 * @param pVM The cross context VM structure.
599 * @param pVCpu The cross context per CPU structure.
600 *
601 * @thread EMT(pVCpu)
602 */
603VMMR3_INT_DECL(VBOXSTRICTRC) DBGFR3EventHandlePending(PVM pVM, PVMCPU pVCpu)
604{
605 VMCPU_ASSERT_EMT(pVCpu);
606 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_DBGF);
607
608 /*
609 * Check that we've got an event first.
610 */
611 AssertReturn(pVCpu->dbgf.s.cEvents > 0, VINF_SUCCESS);
612 AssertReturn(pVCpu->dbgf.s.aEvents[pVCpu->dbgf.s.cEvents - 1].enmState == DBGFEVENTSTATE_CURRENT, VINF_SUCCESS);
613 PDBGFEVENT pEvent = &pVCpu->dbgf.s.aEvents[pVCpu->dbgf.s.cEvents - 1].Event;
614
615 /*
616 * Make sure we've got a debugger and is allowed to speak to it.
617 */
618 int rc = dbgfR3EventPrologue(pVM, pEvent->enmType);
619 if (RT_FAILURE(rc))
620 {
621 /** @todo drop them events? */
622 return rc;
623 }
624
625/** @todo SMP + debugger speaker logic */
626 /*
627 * Copy the event over and mark it as ignore.
628 */
629 pVM->dbgf.s.DbgEvent = *pEvent;
630 pVCpu->dbgf.s.aEvents[pVCpu->dbgf.s.cEvents - 1].enmState = DBGFEVENTSTATE_IGNORE;
631 return dbgfR3SendEvent(pVM);
632}
633
634
635/**
636 * Send a generic debugger event which takes no data.
637 *
638 * @returns VBox status code.
639 * @param pVM The cross context VM structure.
640 * @param enmEvent The event to send.
641 * @internal
642 */
643VMMR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
644{
645 /*
646 * Do stepping filtering.
647 */
648 /** @todo Would be better if we did some of this inside the execution
649 * engines. */
650 if ( enmEvent == DBGFEVENT_STEPPED
651 || enmEvent == DBGFEVENT_STEPPED_HYPER)
652 {
653 if (!dbgfStepAreWeThereYet(pVM, VMMGetCpu(pVM)))
654 return VINF_EM_DBG_STEP;
655 }
656
657 int rc = dbgfR3EventPrologue(pVM, enmEvent);
658 if (RT_FAILURE(rc))
659 return rc;
660
661 /*
662 * Send the event and process the reply communication.
663 */
664 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
665 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
666 return dbgfR3SendEvent(pVM);
667}
668
669
670/**
671 * Send a debugger event which takes the full source file location.
672 *
673 * @returns VBox status code.
674 * @param pVM The cross context VM structure.
675 * @param enmEvent The event to send.
676 * @param pszFile Source file.
677 * @param uLine Line number in source file.
678 * @param pszFunction Function name.
679 * @param pszFormat Message which accompanies the event.
680 * @param ... Message arguments.
681 * @internal
682 */
683VMMR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
684{
685 va_list args;
686 va_start(args, pszFormat);
687 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
688 va_end(args);
689 return rc;
690}
691
692
693/**
694 * Send a debugger event which takes the full source file location.
695 *
696 * @returns VBox status code.
697 * @param pVM The cross context VM structure.
698 * @param enmEvent The event to send.
699 * @param pszFile Source file.
700 * @param uLine Line number in source file.
701 * @param pszFunction Function name.
702 * @param pszFormat Message which accompanies the event.
703 * @param args Message arguments.
704 * @internal
705 */
706VMMR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
707{
708 int rc = dbgfR3EventPrologue(pVM, enmEvent);
709 if (RT_FAILURE(rc))
710 return rc;
711
712 /*
713 * Format the message.
714 */
715 char *pszMessage = NULL;
716 char szMessage[8192];
717 if (pszFormat && *pszFormat)
718 {
719 pszMessage = &szMessage[0];
720 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
721 }
722
723 /*
724 * Send the event and process the reply communication.
725 */
726 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
727 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
728 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
729 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
730 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
731 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
732 return dbgfR3SendEvent(pVM);
733}
734
735
736/**
737 * Send a debugger event which takes the two assertion messages.
738 *
739 * @returns VBox status code.
740 * @param pVM The cross context VM structure.
741 * @param enmEvent The event to send.
742 * @param pszMsg1 First assertion message.
743 * @param pszMsg2 Second assertion message.
744 */
745VMMR3_INT_DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
746{
747 int rc = dbgfR3EventPrologue(pVM, enmEvent);
748 if (RT_FAILURE(rc))
749 return rc;
750
751 /*
752 * Send the event and process the reply communication.
753 */
754 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
755 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
756 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg1 = pszMsg1;
757 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
758 return dbgfR3SendEvent(pVM);
759}
760
761
762/**
763 * Breakpoint was hit somewhere.
764 * Figure out which breakpoint it is and notify the debugger.
765 *
766 * @returns VBox status code.
767 * @param pVM The cross context VM structure.
768 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
769 */
770VMMR3_INT_DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
771{
772 int rc = dbgfR3EventPrologue(pVM, enmEvent);
773 if (RT_FAILURE(rc))
774 return rc;
775
776 /*
777 * Send the event and process the reply communication.
778 */
779 /** @todo SMP */
780 PVMCPU pVCpu = VMMGetCpu0(pVM);
781
782 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
783 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVCpu->dbgf.s.iActiveBp;
784 pVCpu->dbgf.s.iActiveBp = ~0U;
785 if (iBp != ~0U)
786 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
787 else
788 {
789 /* REM breakpoints has be been searched for. */
790#if 0 /** @todo get flat PC api! */
791 uint32_t eip = CPUMGetGuestEIP(pVM);
792#else
793 /** @todo SMP support!! */
794 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(VMMGetCpu(pVM));
795 RTGCPTR eip = pCtx->rip + pCtx->cs.u64Base;
796#endif
797 for (size_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.aBreakpoints); i++)
798 if ( pVM->dbgf.s.aBreakpoints[i].enmType == DBGFBPTYPE_REM
799 && pVM->dbgf.s.aBreakpoints[i].u.Rem.GCPtr == eip)
800 {
801 pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVM->dbgf.s.aBreakpoints[i].iBp;
802 break;
803 }
804 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
805 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
806 }
807 return dbgfR3SendEvent(pVM);
808}
809
810
811/**
812 * Waits for the debugger to respond.
813 *
814 * @returns VBox status code. (clearify)
815 * @param pVM The cross context VM structure.
816 */
817static int dbgfR3VMMWait(PVM pVM)
818{
819 PVMCPU pVCpu = VMMGetCpu(pVM);
820
821 LogFlow(("dbgfR3VMMWait:\n"));
822 int rcRet = VINF_SUCCESS;
823
824 /*
825 * Waits for the debugger to reply (i.e. issue an command).
826 */
827 for (;;)
828 {
829 /*
830 * Wait.
831 */
832 uint32_t cPollHack = 1; /** @todo this interface is horrible now that we're using lots of VMR3ReqCall stuff all over DBGF. */
833 for (;;)
834 {
835 int rc;
836 if ( !VM_FF_IS_ANY_SET(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_REQUEST)
837 && !VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_REQUEST))
838 {
839 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
840 if (RT_SUCCESS(rc))
841 break;
842 if (rc != VERR_TIMEOUT)
843 {
844 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
845 return rc;
846 }
847 }
848
849 if (VM_FF_IS_SET(pVM, VM_FF_EMT_RENDEZVOUS))
850 {
851 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
852 cPollHack = 1;
853 }
854 else if ( VM_FF_IS_SET(pVM, VM_FF_REQUEST)
855 || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_REQUEST))
856 {
857 LogFlow(("dbgfR3VMMWait: Processes requests...\n"));
858 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
859 if (rc == VINF_SUCCESS)
860 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, false /*fPriorityOnly*/);
861 LogFlow(("dbgfR3VMMWait: VMR3ReqProcess -> %Rrc rcRet=%Rrc\n", rc, rcRet));
862 cPollHack = 1;
863 }
864 else
865 {
866 rc = VINF_SUCCESS;
867 if (cPollHack < 120)
868 cPollHack++;
869 }
870
871 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
872 {
873 switch (rc)
874 {
875 case VINF_EM_DBG_BREAKPOINT:
876 case VINF_EM_DBG_STEPPED:
877 case VINF_EM_DBG_STEP:
878 case VINF_EM_DBG_STOP:
879 case VINF_EM_DBG_EVENT:
880 AssertMsgFailed(("rc=%Rrc\n", rc));
881 break;
882
883 /* return straight away */
884 case VINF_EM_TERMINATE:
885 case VINF_EM_OFF:
886 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
887 return rc;
888
889 /* remember return code. */
890 default:
891 AssertReleaseMsgFailed(("rc=%Rrc is not in the switch!\n", rc));
892 RT_FALL_THRU();
893 case VINF_EM_RESET:
894 case VINF_EM_SUSPEND:
895 case VINF_EM_HALT:
896 case VINF_EM_RESUME:
897 case VINF_EM_RESCHEDULE:
898 case VINF_EM_RESCHEDULE_REM:
899 case VINF_EM_RESCHEDULE_RAW:
900 if (rc < rcRet || rcRet == VINF_SUCCESS)
901 rcRet = rc;
902 break;
903 }
904 }
905 else if (RT_FAILURE(rc))
906 {
907 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
908 return rc;
909 }
910 }
911
912 /*
913 * Process the command.
914 */
915 bool fResumeExecution;
916 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
917 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
918 int rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
919 if (fResumeExecution)
920 {
921 if (RT_FAILURE(rc))
922 rcRet = rc;
923 else if ( rc >= VINF_EM_FIRST
924 && rc <= VINF_EM_LAST
925 && (rc < rcRet || rcRet == VINF_SUCCESS))
926 rcRet = rc;
927 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rcRet));
928 return rcRet;
929 }
930 }
931}
932
933
934/**
935 * Executes command from debugger.
936 *
937 * The caller is responsible for waiting or resuming execution based on the
938 * value returned in the *pfResumeExecution indicator.
939 *
940 * @returns VBox status code. (clearify!)
941 * @param pVM The cross context VM structure.
942 * @param enmCmd The command in question.
943 * @param pCmdData Pointer to the command data.
944 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
945 */
946static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
947{
948 bool fSendEvent;
949 bool fResume;
950 int rc = VINF_SUCCESS;
951
952 NOREF(pCmdData); /* for later */
953
954 switch (enmCmd)
955 {
956 /*
957 * Halt is answered by an event say that we've halted.
958 */
959 case DBGFCMD_HALT:
960 {
961 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
962 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
963 fSendEvent = true;
964 fResume = false;
965 break;
966 }
967
968
969 /*
970 * Resume is not answered we'll just resume execution.
971 */
972 case DBGFCMD_GO:
973 {
974 /** @todo SMP */
975 PVMCPU pVCpu = VMMGetCpu0(pVM);
976 pVCpu->dbgf.s.fSingleSteppingRaw = false;
977 fSendEvent = false;
978 fResume = true;
979 break;
980 }
981
982 /** @todo implement (and define) the rest of the commands. */
983
984 /*
985 * Disable breakpoints and stuff.
986 * Send an everythings cool event to the debugger thread and resume execution.
987 */
988 case DBGFCMD_DETACH_DEBUGGER:
989 {
990 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
991 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
992 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
993 pVM->dbgf.s.SteppingFilter.idCpu = NIL_VMCPUID;
994 fSendEvent = true;
995 fResume = true;
996 break;
997 }
998
999 /*
1000 * The debugger has detached successfully.
1001 * There is no reply to this event.
1002 */
1003 case DBGFCMD_DETACHED_DEBUGGER:
1004 {
1005 fSendEvent = false;
1006 fResume = true;
1007 break;
1008 }
1009
1010 /*
1011 * Single step, with trace into.
1012 */
1013 case DBGFCMD_SINGLE_STEP:
1014 {
1015 Log2(("Single step\n"));
1016 /** @todo SMP */
1017 PVMCPU pVCpu = VMMGetCpu0(pVM);
1018 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_OVER)
1019 {
1020 if (dbgfStepGetCurInstrType(pVM, pVCpu) == DBGFSTEPINSTRTYPE_CALL)
1021 pVM->dbgf.s.SteppingFilter.uCallDepth++;
1022 }
1023 if (pVM->dbgf.s.SteppingFilter.cMaxSteps > 0)
1024 {
1025 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1026 fSendEvent = false;
1027 fResume = true;
1028 rc = VINF_EM_DBG_STEP;
1029 }
1030 else
1031 {
1032 /* Stop after zero steps. Nonsense, but whatever. */
1033 pVM->dbgf.s.SteppingFilter.idCpu = NIL_VMCPUID;
1034 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
1035 pVM->dbgf.s.DbgEvent.enmType = pVM->dbgf.s.DbgEvent.enmCtx != DBGFEVENTCTX_HYPER
1036 ? DBGFEVENT_STEPPED : DBGFEVENT_STEPPED_HYPER;
1037 fSendEvent = false;
1038 fResume = false;
1039 }
1040 break;
1041 }
1042
1043 /*
1044 * Default is to send an invalid command event.
1045 */
1046 default:
1047 {
1048 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
1049 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
1050 fSendEvent = true;
1051 fResume = false;
1052 break;
1053 }
1054 }
1055
1056 /*
1057 * Send pending event.
1058 */
1059 if (fSendEvent)
1060 {
1061 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
1062 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
1063 if (RT_FAILURE(rc2))
1064 {
1065 AssertRC(rc2);
1066 *pfResumeExecution = true;
1067 return rc2;
1068 }
1069 }
1070
1071 /*
1072 * Return.
1073 */
1074 *pfResumeExecution = fResume;
1075 return rc;
1076}
1077
1078
1079/**
1080 * Attaches a debugger to the specified VM.
1081 *
1082 * Only one debugger at a time.
1083 *
1084 * @returns VBox status code.
1085 * @param pUVM The user mode VM handle.
1086 */
1087VMMR3DECL(int) DBGFR3Attach(PUVM pUVM)
1088{
1089 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1090 PVM pVM = pUVM->pVM;
1091 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1092
1093 /*
1094 * Call the VM, use EMT for serialization.
1095 *
1096 * Using a priority call here so we can actually attach a debugger during
1097 * the countdown in dbgfR3WaitForAttach.
1098 */
1099 /** @todo SMP */
1100 return VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY, (PFNRT)dbgfR3Attach, 1, pVM);
1101}
1102
1103
1104/**
1105 * EMT worker for DBGFR3Attach.
1106 *
1107 * @returns VBox status code.
1108 * @param pVM The cross context VM structure.
1109 */
1110static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
1111{
1112 if (pVM->dbgf.s.fAttached)
1113 {
1114 Log(("dbgR3Attach: Debugger already attached\n"));
1115 return VERR_DBGF_ALREADY_ATTACHED;
1116 }
1117
1118 /*
1119 * Create the Ping-Pong structure.
1120 */
1121 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
1122 AssertRCReturn(rc, rc);
1123
1124 /*
1125 * Set the attached flag.
1126 */
1127 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
1128 return VINF_SUCCESS;
1129}
1130
1131
1132/**
1133 * Detaches a debugger from the specified VM.
1134 *
1135 * Caller must be attached to the VM.
1136 *
1137 * @returns VBox status code.
1138 * @param pUVM The user mode VM handle.
1139 */
1140VMMR3DECL(int) DBGFR3Detach(PUVM pUVM)
1141{
1142 LogFlow(("DBGFR3Detach:\n"));
1143 int rc;
1144
1145 /*
1146 * Validate input. The UVM handle shall be valid, the VM handle might be
1147 * in the processes of being destroyed already, so deal quietly with that.
1148 */
1149 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1150 PVM pVM = pUVM->pVM;
1151 if (!VM_IS_VALID_EXT(pVM))
1152 return VERR_INVALID_VM_HANDLE;
1153
1154 /*
1155 * Check if attached.
1156 */
1157 if (!pVM->dbgf.s.fAttached)
1158 return VERR_DBGF_NOT_ATTACHED;
1159
1160 /*
1161 * Try send the detach command.
1162 * Keep in mind that we might be racing EMT, so, be extra careful.
1163 */
1164 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
1165 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
1166 {
1167 rc = RTSemPong(&pVM->dbgf.s.PingPong);
1168 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
1169 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
1170 }
1171
1172 /*
1173 * Wait for the OK event.
1174 */
1175 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
1176 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
1177
1178 /*
1179 * Send the notification command indicating that we're really done.
1180 */
1181 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
1182 rc = RTSemPong(&pVM->dbgf.s.PingPong);
1183 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
1184
1185 LogFlowFunc(("returns VINF_SUCCESS\n"));
1186 return VINF_SUCCESS;
1187}
1188
1189
1190/**
1191 * Wait for a debug event.
1192 *
1193 * @returns VBox status code. Will not return VBOX_INTERRUPTED.
1194 * @param pUVM The user mode VM handle.
1195 * @param cMillies Number of millis to wait.
1196 * @param ppEvent Where to store the event pointer.
1197 */
1198VMMR3DECL(int) DBGFR3EventWait(PUVM pUVM, RTMSINTERVAL cMillies, PCDBGFEVENT *ppEvent)
1199{
1200 /*
1201 * Check state.
1202 */
1203 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1204 PVM pVM = pUVM->pVM;
1205 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1206 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1207 *ppEvent = NULL;
1208
1209 /*
1210 * Wait.
1211 */
1212 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
1213 if (RT_SUCCESS(rc))
1214 {
1215 *ppEvent = &pVM->dbgf.s.DbgEvent;
1216 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
1217 return VINF_SUCCESS;
1218 }
1219
1220 return rc;
1221}
1222
1223
1224/**
1225 * Halts VM execution.
1226 *
1227 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
1228 * arrives. Until that time it's not possible to issue any new commands.
1229 *
1230 * @returns VBox status code.
1231 * @param pUVM The user mode VM handle.
1232 */
1233VMMR3DECL(int) DBGFR3Halt(PUVM pUVM)
1234{
1235 /*
1236 * Check state.
1237 */
1238 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1239 PVM pVM = pUVM->pVM;
1240 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1241 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1242 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1243 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
1244 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
1245 return VWRN_DBGF_ALREADY_HALTED;
1246
1247 /*
1248 * Send command.
1249 */
1250 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
1251
1252 return VINF_SUCCESS;
1253}
1254
1255
1256/**
1257 * Checks if the VM is halted by the debugger.
1258 *
1259 * @returns True if halted.
1260 * @returns False if not halted.
1261 * @param pUVM The user mode VM handle.
1262 */
1263VMMR3DECL(bool) DBGFR3IsHalted(PUVM pUVM)
1264{
1265 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1266 PVM pVM = pUVM->pVM;
1267 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1268 AssertReturn(pVM->dbgf.s.fAttached, false);
1269
1270 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1271 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1272 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1273}
1274
1275
1276/**
1277 * Checks if the debugger can wait for events or not.
1278 *
1279 * This function is only used by lazy, multiplexing debuggers. :-)
1280 *
1281 * @returns VBox status code.
1282 * @retval VINF_SUCCESS if waitable.
1283 * @retval VERR_SEM_OUT_OF_TURN if not waitable.
1284 * @retval VERR_INVALID_VM_HANDLE if the VM is being (/ has been) destroyed
1285 * (not asserted) or if the handle is invalid (asserted).
1286 * @retval VERR_DBGF_NOT_ATTACHED if not attached.
1287 *
1288 * @param pUVM The user mode VM handle.
1289 */
1290VMMR3DECL(int) DBGFR3QueryWaitable(PUVM pUVM)
1291{
1292 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1293
1294 /* Note! There is a slight race here, unfortunately. */
1295 PVM pVM = pUVM->pVM;
1296 if (!RT_VALID_PTR(pVM))
1297 return VERR_INVALID_VM_HANDLE;
1298 if (pVM->enmVMState >= VMSTATE_DESTROYING)
1299 return VERR_INVALID_VM_HANDLE;
1300 if (!pVM->dbgf.s.fAttached)
1301 return VERR_DBGF_NOT_ATTACHED;
1302
1303 if (!RTSemPongShouldWait(&pVM->dbgf.s.PingPong))
1304 return VERR_SEM_OUT_OF_TURN;
1305
1306 return VINF_SUCCESS;
1307}
1308
1309
1310/**
1311 * Resumes VM execution.
1312 *
1313 * There is no receipt event on this command.
1314 *
1315 * @returns VBox status code.
1316 * @param pUVM The user mode VM handle.
1317 */
1318VMMR3DECL(int) DBGFR3Resume(PUVM pUVM)
1319{
1320 /*
1321 * Check state.
1322 */
1323 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1324 PVM pVM = pUVM->pVM;
1325 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1326 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1327 if (RT_LIKELY(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong)))
1328 { /* likely */ }
1329 else
1330 return VERR_SEM_OUT_OF_TURN;
1331
1332 /*
1333 * Send the ping back to the emulation thread telling it to run.
1334 */
1335 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1336 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1337 AssertRC(rc);
1338
1339 return rc;
1340}
1341
1342
1343/**
1344 * Classifies the current instruction.
1345 *
1346 * @returns Type of instruction.
1347 * @param pVM The cross context VM structure.
1348 * @param pVCpu The current CPU.
1349 * @thread EMT(pVCpu)
1350 */
1351static DBGFSTEPINSTRTYPE dbgfStepGetCurInstrType(PVM pVM, PVMCPU pVCpu)
1352{
1353 /*
1354 * Read the instruction.
1355 */
1356 bool fIsHyper = dbgfR3FigureEventCtx(pVM) == DBGFEVENTCTX_HYPER;
1357 size_t cbRead = 0;
1358 uint8_t abOpcode[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1359 int rc = PGMR3DbgReadGCPtr(pVM, abOpcode, !fIsHyper ? CPUMGetGuestFlatPC(pVCpu) : CPUMGetHyperRIP(pVCpu),
1360 sizeof(abOpcode) - 1, 0 /*fFlags*/, &cbRead);
1361 if (RT_SUCCESS(rc))
1362 {
1363 /*
1364 * Do minimal parsing. No real need to involve the disassembler here.
1365 */
1366 uint8_t *pb = abOpcode;
1367 for (;;)
1368 {
1369 switch (*pb++)
1370 {
1371 default:
1372 return DBGFSTEPINSTRTYPE_OTHER;
1373
1374 case 0xe8: /* call rel16/32 */
1375 case 0x9a: /* call farptr */
1376 case 0xcc: /* int3 */
1377 case 0xcd: /* int xx */
1378 // case 0xce: /* into */
1379 return DBGFSTEPINSTRTYPE_CALL;
1380
1381 case 0xc2: /* ret xx */
1382 case 0xc3: /* ret */
1383 case 0xca: /* retf xx */
1384 case 0xcb: /* retf */
1385 case 0xcf: /* iret */
1386 return DBGFSTEPINSTRTYPE_RET;
1387
1388 case 0xff:
1389 if ( ((*pb >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) == 2 /* call indir */
1390 || ((*pb >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) == 3) /* call indir-farptr */
1391 return DBGFSTEPINSTRTYPE_CALL;
1392 return DBGFSTEPINSTRTYPE_OTHER;
1393
1394 case 0x0f:
1395 switch (*pb++)
1396 {
1397 case 0x05: /* syscall */
1398 case 0x34: /* sysenter */
1399 return DBGFSTEPINSTRTYPE_CALL;
1400 case 0x07: /* sysret */
1401 case 0x35: /* sysexit */
1402 return DBGFSTEPINSTRTYPE_RET;
1403 }
1404 break;
1405
1406 /* Must handle some REX prefixes. So we do all normal prefixes. */
1407 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
1408 case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
1409 if (fIsHyper) /* ASSUMES 32-bit raw-mode! */
1410 return DBGFSTEPINSTRTYPE_OTHER;
1411 if (!CPUMIsGuestIn64BitCode(pVCpu))
1412 return DBGFSTEPINSTRTYPE_OTHER;
1413 break;
1414
1415 case 0x2e: /* CS */
1416 case 0x36: /* SS */
1417 case 0x3e: /* DS */
1418 case 0x26: /* ES */
1419 case 0x64: /* FS */
1420 case 0x65: /* GS */
1421 case 0x66: /* op size */
1422 case 0x67: /* addr size */
1423 case 0xf0: /* lock */
1424 case 0xf2: /* REPNZ */
1425 case 0xf3: /* REPZ */
1426 break;
1427 }
1428 }
1429 }
1430
1431 return DBGFSTEPINSTRTYPE_INVALID;
1432}
1433
1434
1435/**
1436 * Checks if the stepping has reached a stop point.
1437 *
1438 * Called when raising a stepped event.
1439 *
1440 * @returns true if the event should be raised, false if we should take one more
1441 * step first.
1442 * @param pVM The cross context VM structure.
1443 * @param pVCpu The cross context per CPU structure of the calling EMT.
1444 * @thread EMT(pVCpu)
1445 */
1446static bool dbgfStepAreWeThereYet(PVM pVM, PVMCPU pVCpu)
1447{
1448 /*
1449 * Check valid pVCpu and that it matches the CPU one stepping.
1450 */
1451 if (pVCpu)
1452 {
1453 if (pVCpu->idCpu == pVM->dbgf.s.SteppingFilter.idCpu)
1454 {
1455 /*
1456 * Increase the number of steps and see if we've reached the max.
1457 */
1458 pVM->dbgf.s.SteppingFilter.cSteps++;
1459 if (pVM->dbgf.s.SteppingFilter.cSteps < pVM->dbgf.s.SteppingFilter.cMaxSteps)
1460 {
1461 /*
1462 * Check PC and SP address filtering.
1463 */
1464 if (pVM->dbgf.s.SteppingFilter.fFlags & (DBGF_STEP_F_STOP_ON_ADDRESS | DBGF_STEP_F_STOP_ON_STACK_POP))
1465 {
1466 bool fIsHyper = dbgfR3FigureEventCtx(pVM) == DBGFEVENTCTX_HYPER;
1467 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1468 && pVM->dbgf.s.SteppingFilter.AddrPc == (!fIsHyper ? CPUMGetGuestFlatPC(pVCpu) : CPUMGetHyperRIP(pVCpu)))
1469 return true;
1470 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1471 && (!fIsHyper ? CPUMGetGuestFlatSP(pVCpu) : (uint64_t)CPUMGetHyperESP(pVCpu))
1472 - pVM->dbgf.s.SteppingFilter.AddrStackPop
1473 < pVM->dbgf.s.SteppingFilter.cbStackPop)
1474 return true;
1475 }
1476
1477 /*
1478 * Do step-over filtering separate from the step-into one.
1479 */
1480 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_OVER)
1481 {
1482 DBGFSTEPINSTRTYPE enmType = dbgfStepGetCurInstrType(pVM, pVCpu);
1483 switch (enmType)
1484 {
1485 default:
1486 if ( pVM->dbgf.s.SteppingFilter.uCallDepth != 0
1487 || (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_FILTER_MASK))
1488 break;
1489 return true;
1490 case DBGFSTEPINSTRTYPE_CALL:
1491 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_CALL)
1492 && pVM->dbgf.s.SteppingFilter.uCallDepth == 0)
1493 return true;
1494 pVM->dbgf.s.SteppingFilter.uCallDepth++;
1495 break;
1496 case DBGFSTEPINSTRTYPE_RET:
1497 if (pVM->dbgf.s.SteppingFilter.uCallDepth == 0)
1498 {
1499 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_RET)
1500 return true;
1501 /* If after return, we use the cMaxStep limit to stop the next time. */
1502 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_AFTER_RET)
1503 pVM->dbgf.s.SteppingFilter.cMaxSteps = pVM->dbgf.s.SteppingFilter.cSteps + 1;
1504 }
1505 else if (pVM->dbgf.s.SteppingFilter.uCallDepth > 0)
1506 pVM->dbgf.s.SteppingFilter.uCallDepth--;
1507 break;
1508 }
1509 return false;
1510 }
1511 /*
1512 * Filtered step-into.
1513 */
1514 else if ( pVM->dbgf.s.SteppingFilter.fFlags
1515 & (DBGF_STEP_F_STOP_ON_CALL | DBGF_STEP_F_STOP_ON_RET | DBGF_STEP_F_STOP_AFTER_RET))
1516 {
1517 DBGFSTEPINSTRTYPE enmType = dbgfStepGetCurInstrType(pVM, pVCpu);
1518 switch (enmType)
1519 {
1520 default:
1521 break;
1522 case DBGFSTEPINSTRTYPE_CALL:
1523 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_CALL)
1524 return true;
1525 break;
1526 case DBGFSTEPINSTRTYPE_RET:
1527 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_RET)
1528 return true;
1529 /* If after return, we use the cMaxStep limit to stop the next time. */
1530 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_AFTER_RET)
1531 pVM->dbgf.s.SteppingFilter.cMaxSteps = pVM->dbgf.s.SteppingFilter.cSteps + 1;
1532 break;
1533 }
1534 return false;
1535 }
1536 }
1537 }
1538 }
1539
1540 return true;
1541}
1542
1543
1544/**
1545 * Step Into.
1546 *
1547 * A single step event is generated from this command.
1548 * The current implementation is not reliable, so don't rely on the event coming.
1549 *
1550 * @returns VBox status code.
1551 * @param pUVM The user mode VM handle.
1552 * @param idCpu The ID of the CPU to single step on.
1553 */
1554VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
1555{
1556 return DBGFR3StepEx(pUVM, idCpu, DBGF_STEP_F_INTO, NULL, NULL, 0, 1);
1557}
1558
1559
1560/**
1561 * Full fleged step.
1562 *
1563 * This extended stepping API allows for doing multiple steps before raising an
1564 * event, helping implementing step over, step out and other more advanced
1565 * features.
1566 *
1567 * Like the DBGFR3Step() API, this will normally generate a DBGFEVENT_STEPPED or
1568 * DBGFEVENT_STEPPED_EVENT. However the stepping may be interrupted by other
1569 * events, which will abort the stepping.
1570 *
1571 * The stop on pop area feature is for safeguarding step out.
1572 *
1573 * Please note though, that it will always use stepping and never breakpoints.
1574 * While this allows for a much greater flexibility it can at times be rather
1575 * slow.
1576 *
1577 * @returns VBox status code.
1578 * @param pUVM The user mode VM handle.
1579 * @param idCpu The ID of the CPU to single step on.
1580 * @param fFlags Flags controlling the stepping, DBGF_STEP_F_XXX.
1581 * Either DBGF_STEP_F_INTO or DBGF_STEP_F_OVER must
1582 * always be specified.
1583 * @param pStopPcAddr Address to stop executing at. Completely ignored
1584 * unless DBGF_STEP_F_STOP_ON_ADDRESS is specified.
1585 * @param pStopPopAddr Stack address that SP must be lower than when
1586 * performing DBGF_STEP_F_STOP_ON_STACK_POP filtering.
1587 * @param cbStopPop The range starting at @a pStopPopAddr which is
1588 * considered to be within the same thread stack. Note
1589 * that the API allows @a pStopPopAddr and @a cbStopPop
1590 * to form an area that wraps around and it will
1591 * consider the part starting at 0 as included.
1592 * @param cMaxSteps The maximum number of steps to take. This is to
1593 * prevent stepping for ever, so passing UINT32_MAX is
1594 * not recommended.
1595 *
1596 * @remarks The two address arguments must be guest context virtual addresses,
1597 * or HMA. The code doesn't make much of a point of out HMA, though.
1598 */
1599VMMR3DECL(int) DBGFR3StepEx(PUVM pUVM, VMCPUID idCpu, uint32_t fFlags, PCDBGFADDRESS pStopPcAddr,
1600 PCDBGFADDRESS pStopPopAddr, RTGCUINTPTR cbStopPop, uint32_t cMaxSteps)
1601{
1602 /*
1603 * Check state.
1604 */
1605 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1606 PVM pVM = pUVM->pVM;
1607 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1608 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1609 AssertReturn(!(fFlags & ~DBGF_STEP_F_VALID_MASK), VERR_INVALID_FLAGS);
1610 AssertReturn(RT_BOOL(fFlags & DBGF_STEP_F_INTO) != RT_BOOL(fFlags & DBGF_STEP_F_OVER), VERR_INVALID_FLAGS);
1611 if (fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1612 {
1613 AssertReturn(RT_VALID_PTR(pStopPcAddr), VERR_INVALID_POINTER);
1614 AssertReturn(DBGFADDRESS_IS_VALID(pStopPcAddr), VERR_INVALID_PARAMETER);
1615 AssertReturn(DBGFADDRESS_IS_VIRT_GC(pStopPcAddr), VERR_INVALID_PARAMETER);
1616 }
1617 AssertReturn(!(fFlags & DBGF_STEP_F_STOP_ON_STACK_POP) || RT_VALID_PTR(pStopPopAddr), VERR_INVALID_POINTER);
1618 if (fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1619 {
1620 AssertReturn(RT_VALID_PTR(pStopPopAddr), VERR_INVALID_POINTER);
1621 AssertReturn(DBGFADDRESS_IS_VALID(pStopPopAddr), VERR_INVALID_PARAMETER);
1622 AssertReturn(DBGFADDRESS_IS_VIRT_GC(pStopPopAddr), VERR_INVALID_PARAMETER);
1623 AssertReturn(cbStopPop > 0, VERR_INVALID_PARAMETER);
1624 }
1625
1626 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1627 if (RT_LIKELY(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong)))
1628 { /* likely */ }
1629 else
1630 return VERR_SEM_OUT_OF_TURN;
1631 Assert(pVM->dbgf.s.SteppingFilter.idCpu == NIL_VMCPUID);
1632
1633 /*
1634 * Send the ping back to the emulation thread telling it to run.
1635 */
1636 if (fFlags == DBGF_STEP_F_INTO)
1637 pVM->dbgf.s.SteppingFilter.idCpu = NIL_VMCPUID;
1638 else
1639 pVM->dbgf.s.SteppingFilter.idCpu = idCpu;
1640 pVM->dbgf.s.SteppingFilter.fFlags = fFlags;
1641 if (fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1642 pVM->dbgf.s.SteppingFilter.AddrPc = pStopPcAddr->FlatPtr;
1643 else
1644 pVM->dbgf.s.SteppingFilter.AddrPc = 0;
1645 if (fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1646 {
1647 pVM->dbgf.s.SteppingFilter.AddrStackPop = pStopPopAddr->FlatPtr;
1648 pVM->dbgf.s.SteppingFilter.cbStackPop = cbStopPop;
1649 }
1650 else
1651 {
1652 pVM->dbgf.s.SteppingFilter.AddrStackPop = 0;
1653 pVM->dbgf.s.SteppingFilter.cbStackPop = RTGCPTR_MAX;
1654 }
1655
1656 pVM->dbgf.s.SteppingFilter.cMaxSteps = cMaxSteps;
1657 pVM->dbgf.s.SteppingFilter.cSteps = 0;
1658 pVM->dbgf.s.SteppingFilter.uCallDepth = 0;
1659
1660/** @todo SMP (idCpu) */
1661 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1662 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1663 AssertRC(rc);
1664 return rc;
1665}
1666
1667
1668
1669/**
1670 * dbgfR3EventConfigEx argument packet.
1671 */
1672typedef struct DBGFR3EVENTCONFIGEXARGS
1673{
1674 PCDBGFEVENTCONFIG paConfigs;
1675 size_t cConfigs;
1676 int rc;
1677} DBGFR3EVENTCONFIGEXARGS;
1678/** Pointer to a dbgfR3EventConfigEx argument packet. */
1679typedef DBGFR3EVENTCONFIGEXARGS *PDBGFR3EVENTCONFIGEXARGS;
1680
1681
1682/**
1683 * @callback_method_impl{FNVMMEMTRENDEZVOUS, Worker for DBGFR3EventConfigEx.}
1684 */
1685static DECLCALLBACK(VBOXSTRICTRC) dbgfR3EventConfigEx(PVM pVM, PVMCPU pVCpu, void *pvUser)
1686{
1687 if (pVCpu->idCpu == 0)
1688 {
1689 PDBGFR3EVENTCONFIGEXARGS pArgs = (PDBGFR3EVENTCONFIGEXARGS)pvUser;
1690 DBGFEVENTCONFIG volatile const *paConfigs = pArgs->paConfigs;
1691 size_t cConfigs = pArgs->cConfigs;
1692
1693 /*
1694 * Apply the changes.
1695 */
1696 unsigned cChanges = 0;
1697 for (uint32_t i = 0; i < cConfigs; i++)
1698 {
1699 DBGFEVENTTYPE enmType = paConfigs[i].enmType;
1700 AssertReturn(enmType >= DBGFEVENT_FIRST_SELECTABLE && enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1701 if (paConfigs[i].fEnabled)
1702 cChanges += ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, enmType) == false;
1703 else
1704 cChanges += ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, enmType) == true;
1705 }
1706
1707 /*
1708 * Inform HM about changes.
1709 */
1710 if (cChanges > 0 && HMIsEnabled(pVM))
1711 {
1712 HMR3NotifyDebugEventChanged(pVM);
1713 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1714 }
1715 }
1716 else if (HMIsEnabled(pVM))
1717 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1718
1719 return VINF_SUCCESS;
1720}
1721
1722
1723/**
1724 * Configures (enables/disables) multiple selectable debug events.
1725 *
1726 * @returns VBox status code.
1727 * @param pUVM The user mode VM handle.
1728 * @param paConfigs The event to configure and their new state.
1729 * @param cConfigs Number of entries in @a paConfigs.
1730 */
1731VMMR3DECL(int) DBGFR3EventConfigEx(PUVM pUVM, PCDBGFEVENTCONFIG paConfigs, size_t cConfigs)
1732{
1733 /*
1734 * Validate input.
1735 */
1736 size_t i = cConfigs;
1737 while (i-- > 0)
1738 {
1739 AssertReturn(paConfigs[i].enmType >= DBGFEVENT_FIRST_SELECTABLE, VERR_INVALID_PARAMETER);
1740 AssertReturn(paConfigs[i].enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1741 }
1742 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1743 PVM pVM = pUVM->pVM;
1744 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1745
1746 /*
1747 * Apply the changes in EMT(0) and rendezvous with the other CPUs so they
1748 * can sync their data and execution with new debug state.
1749 */
1750 DBGFR3EVENTCONFIGEXARGS Args = { paConfigs, cConfigs, VINF_SUCCESS };
1751 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING | VMMEMTRENDEZVOUS_FLAGS_PRIORITY,
1752 dbgfR3EventConfigEx, &Args);
1753 if (RT_SUCCESS(rc))
1754 rc = Args.rc;
1755 return rc;
1756}
1757
1758
1759/**
1760 * Enables or disables a selectable debug event.
1761 *
1762 * @returns VBox status code.
1763 * @param pUVM The user mode VM handle.
1764 * @param enmEvent The selectable debug event.
1765 * @param fEnabled The new state.
1766 */
1767VMMR3DECL(int) DBGFR3EventConfig(PUVM pUVM, DBGFEVENTTYPE enmEvent, bool fEnabled)
1768{
1769 /*
1770 * Convert to an array call.
1771 */
1772 DBGFEVENTCONFIG EvtCfg = { enmEvent, fEnabled };
1773 return DBGFR3EventConfigEx(pUVM, &EvtCfg, 1);
1774}
1775
1776
1777/**
1778 * Checks if the given selectable event is enabled.
1779 *
1780 * @returns true if enabled, false if not or invalid input.
1781 * @param pUVM The user mode VM handle.
1782 * @param enmEvent The selectable debug event.
1783 * @sa DBGFR3EventQuery
1784 */
1785VMMR3DECL(bool) DBGFR3EventIsEnabled(PUVM pUVM, DBGFEVENTTYPE enmEvent)
1786{
1787 /*
1788 * Validate input.
1789 */
1790 AssertReturn( enmEvent >= DBGFEVENT_HALT_DONE
1791 && enmEvent < DBGFEVENT_END, false);
1792 Assert( enmEvent >= DBGFEVENT_FIRST_SELECTABLE
1793 || enmEvent == DBGFEVENT_BREAKPOINT
1794 || enmEvent == DBGFEVENT_BREAKPOINT_IO
1795 || enmEvent == DBGFEVENT_BREAKPOINT_MMIO);
1796
1797 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1798 PVM pVM = pUVM->pVM;
1799 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1800
1801 /*
1802 * Check the event status.
1803 */
1804 return ASMBitTest(&pVM->dbgf.s.bmSelectedEvents, enmEvent);
1805}
1806
1807
1808/**
1809 * Queries the status of a set of events.
1810 *
1811 * @returns VBox status code.
1812 * @param pUVM The user mode VM handle.
1813 * @param paConfigs The events to query and where to return the state.
1814 * @param cConfigs The number of elements in @a paConfigs.
1815 * @sa DBGFR3EventIsEnabled, DBGF_IS_EVENT_ENABLED
1816 */
1817VMMR3DECL(int) DBGFR3EventQuery(PUVM pUVM, PDBGFEVENTCONFIG paConfigs, size_t cConfigs)
1818{
1819 /*
1820 * Validate input.
1821 */
1822 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1823 PVM pVM = pUVM->pVM;
1824 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1825
1826 for (size_t i = 0; i < cConfigs; i++)
1827 {
1828 DBGFEVENTTYPE enmType = paConfigs[i].enmType;
1829 AssertReturn( enmType >= DBGFEVENT_HALT_DONE
1830 && enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1831 Assert( enmType >= DBGFEVENT_FIRST_SELECTABLE
1832 || enmType == DBGFEVENT_BREAKPOINT
1833 || enmType == DBGFEVENT_BREAKPOINT_IO
1834 || enmType == DBGFEVENT_BREAKPOINT_MMIO);
1835 paConfigs[i].fEnabled = ASMBitTest(&pVM->dbgf.s.bmSelectedEvents, paConfigs[i].enmType);
1836 }
1837
1838 return VINF_SUCCESS;
1839}
1840
1841
1842/**
1843 * dbgfR3InterruptConfigEx argument packet.
1844 */
1845typedef struct DBGFR3INTERRUPTCONFIGEXARGS
1846{
1847 PCDBGFINTERRUPTCONFIG paConfigs;
1848 size_t cConfigs;
1849 int rc;
1850} DBGFR3INTERRUPTCONFIGEXARGS;
1851/** Pointer to a dbgfR3InterruptConfigEx argument packet. */
1852typedef DBGFR3INTERRUPTCONFIGEXARGS *PDBGFR3INTERRUPTCONFIGEXARGS;
1853
1854/**
1855 * @callback_method_impl{FNVMMEMTRENDEZVOUS,
1856 * Worker for DBGFR3InterruptConfigEx.}
1857 */
1858static DECLCALLBACK(VBOXSTRICTRC) dbgfR3InterruptConfigEx(PVM pVM, PVMCPU pVCpu, void *pvUser)
1859{
1860 if (pVCpu->idCpu == 0)
1861 {
1862 PDBGFR3INTERRUPTCONFIGEXARGS pArgs = (PDBGFR3INTERRUPTCONFIGEXARGS)pvUser;
1863 PCDBGFINTERRUPTCONFIG paConfigs = pArgs->paConfigs;
1864 size_t cConfigs = pArgs->cConfigs;
1865
1866 /*
1867 * Apply the changes.
1868 */
1869 bool fChanged = false;
1870 bool fThis;
1871 for (uint32_t i = 0; i < cConfigs; i++)
1872 {
1873 /*
1874 * Hardware interrupts.
1875 */
1876 if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_ENABLED)
1877 {
1878 fChanged |= fThis = ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmHardIntBreakpoints, paConfigs[i].iInterrupt) == false;
1879 if (fThis)
1880 {
1881 Assert(pVM->dbgf.s.cHardIntBreakpoints < 256);
1882 pVM->dbgf.s.cHardIntBreakpoints++;
1883 }
1884 }
1885 else if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_DISABLED)
1886 {
1887 fChanged |= fThis = ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmHardIntBreakpoints, paConfigs[i].iInterrupt) == true;
1888 if (fThis)
1889 {
1890 Assert(pVM->dbgf.s.cHardIntBreakpoints > 0);
1891 pVM->dbgf.s.cHardIntBreakpoints--;
1892 }
1893 }
1894
1895 /*
1896 * Software interrupts.
1897 */
1898 if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_ENABLED)
1899 {
1900 fChanged |= fThis = ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSoftIntBreakpoints, paConfigs[i].iInterrupt) == false;
1901 if (fThis)
1902 {
1903 Assert(pVM->dbgf.s.cSoftIntBreakpoints < 256);
1904 pVM->dbgf.s.cSoftIntBreakpoints++;
1905 }
1906 }
1907 else if (paConfigs[i].enmSoftState == DBGFINTERRUPTSTATE_DISABLED)
1908 {
1909 fChanged |= fThis = ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSoftIntBreakpoints, paConfigs[i].iInterrupt) == true;
1910 if (fThis)
1911 {
1912 Assert(pVM->dbgf.s.cSoftIntBreakpoints > 0);
1913 pVM->dbgf.s.cSoftIntBreakpoints--;
1914 }
1915 }
1916 }
1917
1918 /*
1919 * Update the event bitmap entries.
1920 */
1921 if (pVM->dbgf.s.cHardIntBreakpoints > 0)
1922 fChanged |= ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_HARDWARE) == false;
1923 else
1924 fChanged |= ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_HARDWARE) == true;
1925
1926 if (pVM->dbgf.s.cSoftIntBreakpoints > 0)
1927 fChanged |= ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_SOFTWARE) == false;
1928 else
1929 fChanged |= ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_SOFTWARE) == true;
1930
1931 /*
1932 * Inform HM about changes.
1933 */
1934 if (fChanged && HMIsEnabled(pVM))
1935 {
1936 HMR3NotifyDebugEventChanged(pVM);
1937 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1938 }
1939 }
1940 else if (HMIsEnabled(pVM))
1941 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1942
1943 return VINF_SUCCESS;
1944}
1945
1946
1947/**
1948 * Changes
1949 *
1950 * @returns VBox status code.
1951 * @param pUVM The user mode VM handle.
1952 * @param paConfigs The events to query and where to return the state.
1953 * @param cConfigs The number of elements in @a paConfigs.
1954 * @sa DBGFR3InterruptConfigHardware, DBGFR3InterruptConfigSoftware
1955 */
1956VMMR3DECL(int) DBGFR3InterruptConfigEx(PUVM pUVM, PCDBGFINTERRUPTCONFIG paConfigs, size_t cConfigs)
1957{
1958 /*
1959 * Validate input.
1960 */
1961 size_t i = cConfigs;
1962 while (i-- > 0)
1963 {
1964 AssertReturn(paConfigs[i].enmHardState <= DBGFINTERRUPTSTATE_DONT_TOUCH, VERR_INVALID_PARAMETER);
1965 AssertReturn(paConfigs[i].enmSoftState <= DBGFINTERRUPTSTATE_DONT_TOUCH, VERR_INVALID_PARAMETER);
1966 }
1967
1968 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1969 PVM pVM = pUVM->pVM;
1970 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1971
1972 /*
1973 * Apply the changes in EMT(0) and rendezvous with the other CPUs so they
1974 * can sync their data and execution with new debug state.
1975 */
1976 DBGFR3INTERRUPTCONFIGEXARGS Args = { paConfigs, cConfigs, VINF_SUCCESS };
1977 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING | VMMEMTRENDEZVOUS_FLAGS_PRIORITY,
1978 dbgfR3InterruptConfigEx, &Args);
1979 if (RT_SUCCESS(rc))
1980 rc = Args.rc;
1981 return rc;
1982}
1983
1984
1985/**
1986 * Configures interception of a hardware interrupt.
1987 *
1988 * @returns VBox status code.
1989 * @param pUVM The user mode VM handle.
1990 * @param iInterrupt The interrupt number.
1991 * @param fEnabled Whether interception is enabled or not.
1992 * @sa DBGFR3InterruptSoftwareConfig, DBGFR3InterruptConfigEx
1993 */
1994VMMR3DECL(int) DBGFR3InterruptHardwareConfig(PUVM pUVM, uint8_t iInterrupt, bool fEnabled)
1995{
1996 /*
1997 * Convert to DBGFR3InterruptConfigEx call.
1998 */
1999 DBGFINTERRUPTCONFIG IntCfg = { iInterrupt, (uint8_t)fEnabled, DBGFINTERRUPTSTATE_DONT_TOUCH };
2000 return DBGFR3InterruptConfigEx(pUVM, &IntCfg, 1);
2001}
2002
2003
2004/**
2005 * Configures interception of a software interrupt.
2006 *
2007 * @returns VBox status code.
2008 * @param pUVM The user mode VM handle.
2009 * @param iInterrupt The interrupt number.
2010 * @param fEnabled Whether interception is enabled or not.
2011 * @sa DBGFR3InterruptHardwareConfig, DBGFR3InterruptConfigEx
2012 */
2013VMMR3DECL(int) DBGFR3InterruptSoftwareConfig(PUVM pUVM, uint8_t iInterrupt, bool fEnabled)
2014{
2015 /*
2016 * Convert to DBGFR3InterruptConfigEx call.
2017 */
2018 DBGFINTERRUPTCONFIG IntCfg = { iInterrupt, DBGFINTERRUPTSTATE_DONT_TOUCH, (uint8_t)fEnabled };
2019 return DBGFR3InterruptConfigEx(pUVM, &IntCfg, 1);
2020}
2021
2022
2023/**
2024 * Checks whether interception is enabled for a hardware interrupt.
2025 *
2026 * @returns true if enabled, false if not or invalid input.
2027 * @param pUVM The user mode VM handle.
2028 * @param iInterrupt The interrupt number.
2029 * @sa DBGFR3InterruptSoftwareIsEnabled, DBGF_IS_HARDWARE_INT_ENABLED,
2030 * DBGF_IS_SOFTWARE_INT_ENABLED
2031 */
2032VMMR3DECL(int) DBGFR3InterruptHardwareIsEnabled(PUVM pUVM, uint8_t iInterrupt)
2033{
2034 /*
2035 * Validate input.
2036 */
2037 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2038 PVM pVM = pUVM->pVM;
2039 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2040
2041 /*
2042 * Check it.
2043 */
2044 return ASMBitTest(&pVM->dbgf.s.bmHardIntBreakpoints, iInterrupt);
2045}
2046
2047
2048/**
2049 * Checks whether interception is enabled for a software interrupt.
2050 *
2051 * @returns true if enabled, false if not or invalid input.
2052 * @param pUVM The user mode VM handle.
2053 * @param iInterrupt The interrupt number.
2054 * @sa DBGFR3InterruptHardwareIsEnabled, DBGF_IS_SOFTWARE_INT_ENABLED,
2055 * DBGF_IS_HARDWARE_INT_ENABLED,
2056 */
2057VMMR3DECL(int) DBGFR3InterruptSoftwareIsEnabled(PUVM pUVM, uint8_t iInterrupt)
2058{
2059 /*
2060 * Validate input.
2061 */
2062 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2063 PVM pVM = pUVM->pVM;
2064 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2065
2066 /*
2067 * Check it.
2068 */
2069 return ASMBitTest(&pVM->dbgf.s.bmSoftIntBreakpoints, iInterrupt);
2070}
2071
2072
2073
2074/**
2075 * Call this to single step programmatically.
2076 *
2077 * You must pass down the return code to the EM loop! That's
2078 * where the actual single stepping take place (at least in the
2079 * current implementation).
2080 *
2081 * @returns VINF_EM_DBG_STEP
2082 *
2083 * @param pVCpu The cross context virtual CPU structure.
2084 *
2085 * @thread VCpu EMT
2086 * @internal
2087 */
2088VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
2089{
2090 VMCPU_ASSERT_EMT(pVCpu);
2091
2092 pVCpu->dbgf.s.fSingleSteppingRaw = true;
2093 return VINF_EM_DBG_STEP;
2094}
2095
2096
2097/**
2098 * Inject an NMI into a running VM (only VCPU 0!)
2099 *
2100 * @returns VBox status code.
2101 * @param pUVM The user mode VM structure.
2102 * @param idCpu The ID of the CPU to inject the NMI on.
2103 */
2104VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
2105{
2106 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2107 PVM pVM = pUVM->pVM;
2108 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2109 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
2110
2111 /** @todo Implement generic NMI injection. */
2112 /** @todo NEM: NMI injection */
2113 if (!HMIsEnabled(pVM))
2114 return VERR_NOT_SUP_IN_RAW_MODE;
2115
2116 VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_INTERRUPT_NMI);
2117 return VINF_SUCCESS;
2118}
2119
Note: See TracBrowser for help on using the repository browser.

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