VirtualBox

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

Last change on this file since 80386 was 80333, checked in by vboxsync, 5 years ago

VMM: Eliminating the VBOX_BUGREF_9217_PART_I preprocessor macro. bugref:9217

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 68.7 KB
Line 
1/* $Id: DBGF.cpp 80333 2019-08-16 20:28:38Z 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->apCpusR3[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 size_t cbRead = 0;
1357 uint8_t abOpcode[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1358 int rc = PGMR3DbgReadGCPtr(pVM, abOpcode, CPUMGetGuestFlatPC(pVCpu), sizeof(abOpcode) - 1, 0 /*fFlags*/, &cbRead);
1359 if (RT_SUCCESS(rc))
1360 {
1361 /*
1362 * Do minimal parsing. No real need to involve the disassembler here.
1363 */
1364 uint8_t *pb = abOpcode;
1365 for (;;)
1366 {
1367 switch (*pb++)
1368 {
1369 default:
1370 return DBGFSTEPINSTRTYPE_OTHER;
1371
1372 case 0xe8: /* call rel16/32 */
1373 case 0x9a: /* call farptr */
1374 case 0xcc: /* int3 */
1375 case 0xcd: /* int xx */
1376 // case 0xce: /* into */
1377 return DBGFSTEPINSTRTYPE_CALL;
1378
1379 case 0xc2: /* ret xx */
1380 case 0xc3: /* ret */
1381 case 0xca: /* retf xx */
1382 case 0xcb: /* retf */
1383 case 0xcf: /* iret */
1384 return DBGFSTEPINSTRTYPE_RET;
1385
1386 case 0xff:
1387 if ( ((*pb >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) == 2 /* call indir */
1388 || ((*pb >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) == 3) /* call indir-farptr */
1389 return DBGFSTEPINSTRTYPE_CALL;
1390 return DBGFSTEPINSTRTYPE_OTHER;
1391
1392 case 0x0f:
1393 switch (*pb++)
1394 {
1395 case 0x05: /* syscall */
1396 case 0x34: /* sysenter */
1397 return DBGFSTEPINSTRTYPE_CALL;
1398 case 0x07: /* sysret */
1399 case 0x35: /* sysexit */
1400 return DBGFSTEPINSTRTYPE_RET;
1401 }
1402 break;
1403
1404 /* Must handle some REX prefixes. So we do all normal prefixes. */
1405 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
1406 case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
1407 if (!CPUMIsGuestIn64BitCode(pVCpu))
1408 return DBGFSTEPINSTRTYPE_OTHER;
1409 break;
1410
1411 case 0x2e: /* CS */
1412 case 0x36: /* SS */
1413 case 0x3e: /* DS */
1414 case 0x26: /* ES */
1415 case 0x64: /* FS */
1416 case 0x65: /* GS */
1417 case 0x66: /* op size */
1418 case 0x67: /* addr size */
1419 case 0xf0: /* lock */
1420 case 0xf2: /* REPNZ */
1421 case 0xf3: /* REPZ */
1422 break;
1423 }
1424 }
1425 }
1426
1427 return DBGFSTEPINSTRTYPE_INVALID;
1428}
1429
1430
1431/**
1432 * Checks if the stepping has reached a stop point.
1433 *
1434 * Called when raising a stepped event.
1435 *
1436 * @returns true if the event should be raised, false if we should take one more
1437 * step first.
1438 * @param pVM The cross context VM structure.
1439 * @param pVCpu The cross context per CPU structure of the calling EMT.
1440 * @thread EMT(pVCpu)
1441 */
1442static bool dbgfStepAreWeThereYet(PVM pVM, PVMCPU pVCpu)
1443{
1444 /*
1445 * Check valid pVCpu and that it matches the CPU one stepping.
1446 */
1447 if (pVCpu)
1448 {
1449 if (pVCpu->idCpu == pVM->dbgf.s.SteppingFilter.idCpu)
1450 {
1451 /*
1452 * Increase the number of steps and see if we've reached the max.
1453 */
1454 pVM->dbgf.s.SteppingFilter.cSteps++;
1455 if (pVM->dbgf.s.SteppingFilter.cSteps < pVM->dbgf.s.SteppingFilter.cMaxSteps)
1456 {
1457 /*
1458 * Check PC and SP address filtering.
1459 */
1460 if (pVM->dbgf.s.SteppingFilter.fFlags & (DBGF_STEP_F_STOP_ON_ADDRESS | DBGF_STEP_F_STOP_ON_STACK_POP))
1461 {
1462 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1463 && pVM->dbgf.s.SteppingFilter.AddrPc == CPUMGetGuestFlatPC(pVCpu))
1464 return true;
1465 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1466 && CPUMGetGuestFlatSP(pVCpu) - pVM->dbgf.s.SteppingFilter.AddrStackPop
1467 < pVM->dbgf.s.SteppingFilter.cbStackPop)
1468 return true;
1469 }
1470
1471 /*
1472 * Do step-over filtering separate from the step-into one.
1473 */
1474 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_OVER)
1475 {
1476 DBGFSTEPINSTRTYPE enmType = dbgfStepGetCurInstrType(pVM, pVCpu);
1477 switch (enmType)
1478 {
1479 default:
1480 if ( pVM->dbgf.s.SteppingFilter.uCallDepth != 0
1481 || (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_FILTER_MASK))
1482 break;
1483 return true;
1484 case DBGFSTEPINSTRTYPE_CALL:
1485 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_CALL)
1486 && pVM->dbgf.s.SteppingFilter.uCallDepth == 0)
1487 return true;
1488 pVM->dbgf.s.SteppingFilter.uCallDepth++;
1489 break;
1490 case DBGFSTEPINSTRTYPE_RET:
1491 if (pVM->dbgf.s.SteppingFilter.uCallDepth == 0)
1492 {
1493 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_RET)
1494 return true;
1495 /* If after return, we use the cMaxStep limit to stop the next time. */
1496 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_AFTER_RET)
1497 pVM->dbgf.s.SteppingFilter.cMaxSteps = pVM->dbgf.s.SteppingFilter.cSteps + 1;
1498 }
1499 else if (pVM->dbgf.s.SteppingFilter.uCallDepth > 0)
1500 pVM->dbgf.s.SteppingFilter.uCallDepth--;
1501 break;
1502 }
1503 return false;
1504 }
1505 /*
1506 * Filtered step-into.
1507 */
1508 else if ( pVM->dbgf.s.SteppingFilter.fFlags
1509 & (DBGF_STEP_F_STOP_ON_CALL | DBGF_STEP_F_STOP_ON_RET | DBGF_STEP_F_STOP_AFTER_RET))
1510 {
1511 DBGFSTEPINSTRTYPE enmType = dbgfStepGetCurInstrType(pVM, pVCpu);
1512 switch (enmType)
1513 {
1514 default:
1515 break;
1516 case DBGFSTEPINSTRTYPE_CALL:
1517 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_CALL)
1518 return true;
1519 break;
1520 case DBGFSTEPINSTRTYPE_RET:
1521 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_RET)
1522 return true;
1523 /* If after return, we use the cMaxStep limit to stop the next time. */
1524 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_AFTER_RET)
1525 pVM->dbgf.s.SteppingFilter.cMaxSteps = pVM->dbgf.s.SteppingFilter.cSteps + 1;
1526 break;
1527 }
1528 return false;
1529 }
1530 }
1531 }
1532 }
1533
1534 return true;
1535}
1536
1537
1538/**
1539 * Step Into.
1540 *
1541 * A single step event is generated from this command.
1542 * The current implementation is not reliable, so don't rely on the event coming.
1543 *
1544 * @returns VBox status code.
1545 * @param pUVM The user mode VM handle.
1546 * @param idCpu The ID of the CPU to single step on.
1547 */
1548VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
1549{
1550 return DBGFR3StepEx(pUVM, idCpu, DBGF_STEP_F_INTO, NULL, NULL, 0, 1);
1551}
1552
1553
1554/**
1555 * Full fleged step.
1556 *
1557 * This extended stepping API allows for doing multiple steps before raising an
1558 * event, helping implementing step over, step out and other more advanced
1559 * features.
1560 *
1561 * Like the DBGFR3Step() API, this will normally generate a DBGFEVENT_STEPPED or
1562 * DBGFEVENT_STEPPED_EVENT. However the stepping may be interrupted by other
1563 * events, which will abort the stepping.
1564 *
1565 * The stop on pop area feature is for safeguarding step out.
1566 *
1567 * Please note though, that it will always use stepping and never breakpoints.
1568 * While this allows for a much greater flexibility it can at times be rather
1569 * slow.
1570 *
1571 * @returns VBox status code.
1572 * @param pUVM The user mode VM handle.
1573 * @param idCpu The ID of the CPU to single step on.
1574 * @param fFlags Flags controlling the stepping, DBGF_STEP_F_XXX.
1575 * Either DBGF_STEP_F_INTO or DBGF_STEP_F_OVER must
1576 * always be specified.
1577 * @param pStopPcAddr Address to stop executing at. Completely ignored
1578 * unless DBGF_STEP_F_STOP_ON_ADDRESS is specified.
1579 * @param pStopPopAddr Stack address that SP must be lower than when
1580 * performing DBGF_STEP_F_STOP_ON_STACK_POP filtering.
1581 * @param cbStopPop The range starting at @a pStopPopAddr which is
1582 * considered to be within the same thread stack. Note
1583 * that the API allows @a pStopPopAddr and @a cbStopPop
1584 * to form an area that wraps around and it will
1585 * consider the part starting at 0 as included.
1586 * @param cMaxSteps The maximum number of steps to take. This is to
1587 * prevent stepping for ever, so passing UINT32_MAX is
1588 * not recommended.
1589 *
1590 * @remarks The two address arguments must be guest context virtual addresses,
1591 * or HMA. The code doesn't make much of a point of out HMA, though.
1592 */
1593VMMR3DECL(int) DBGFR3StepEx(PUVM pUVM, VMCPUID idCpu, uint32_t fFlags, PCDBGFADDRESS pStopPcAddr,
1594 PCDBGFADDRESS pStopPopAddr, RTGCUINTPTR cbStopPop, uint32_t cMaxSteps)
1595{
1596 /*
1597 * Check state.
1598 */
1599 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1600 PVM pVM = pUVM->pVM;
1601 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1602 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1603 AssertReturn(!(fFlags & ~DBGF_STEP_F_VALID_MASK), VERR_INVALID_FLAGS);
1604 AssertReturn(RT_BOOL(fFlags & DBGF_STEP_F_INTO) != RT_BOOL(fFlags & DBGF_STEP_F_OVER), VERR_INVALID_FLAGS);
1605 if (fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1606 {
1607 AssertReturn(RT_VALID_PTR(pStopPcAddr), VERR_INVALID_POINTER);
1608 AssertReturn(DBGFADDRESS_IS_VALID(pStopPcAddr), VERR_INVALID_PARAMETER);
1609 AssertReturn(DBGFADDRESS_IS_VIRT_GC(pStopPcAddr), VERR_INVALID_PARAMETER);
1610 }
1611 AssertReturn(!(fFlags & DBGF_STEP_F_STOP_ON_STACK_POP) || RT_VALID_PTR(pStopPopAddr), VERR_INVALID_POINTER);
1612 if (fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1613 {
1614 AssertReturn(RT_VALID_PTR(pStopPopAddr), VERR_INVALID_POINTER);
1615 AssertReturn(DBGFADDRESS_IS_VALID(pStopPopAddr), VERR_INVALID_PARAMETER);
1616 AssertReturn(DBGFADDRESS_IS_VIRT_GC(pStopPopAddr), VERR_INVALID_PARAMETER);
1617 AssertReturn(cbStopPop > 0, VERR_INVALID_PARAMETER);
1618 }
1619
1620 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1621 if (RT_LIKELY(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong)))
1622 { /* likely */ }
1623 else
1624 return VERR_SEM_OUT_OF_TURN;
1625 Assert(pVM->dbgf.s.SteppingFilter.idCpu == NIL_VMCPUID);
1626
1627 /*
1628 * Send the ping back to the emulation thread telling it to run.
1629 */
1630 if (fFlags == DBGF_STEP_F_INTO)
1631 pVM->dbgf.s.SteppingFilter.idCpu = NIL_VMCPUID;
1632 else
1633 pVM->dbgf.s.SteppingFilter.idCpu = idCpu;
1634 pVM->dbgf.s.SteppingFilter.fFlags = fFlags;
1635 if (fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1636 pVM->dbgf.s.SteppingFilter.AddrPc = pStopPcAddr->FlatPtr;
1637 else
1638 pVM->dbgf.s.SteppingFilter.AddrPc = 0;
1639 if (fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1640 {
1641 pVM->dbgf.s.SteppingFilter.AddrStackPop = pStopPopAddr->FlatPtr;
1642 pVM->dbgf.s.SteppingFilter.cbStackPop = cbStopPop;
1643 }
1644 else
1645 {
1646 pVM->dbgf.s.SteppingFilter.AddrStackPop = 0;
1647 pVM->dbgf.s.SteppingFilter.cbStackPop = RTGCPTR_MAX;
1648 }
1649
1650 pVM->dbgf.s.SteppingFilter.cMaxSteps = cMaxSteps;
1651 pVM->dbgf.s.SteppingFilter.cSteps = 0;
1652 pVM->dbgf.s.SteppingFilter.uCallDepth = 0;
1653
1654/** @todo SMP (idCpu) */
1655 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1656 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1657 AssertRC(rc);
1658 return rc;
1659}
1660
1661
1662
1663/**
1664 * dbgfR3EventConfigEx argument packet.
1665 */
1666typedef struct DBGFR3EVENTCONFIGEXARGS
1667{
1668 PCDBGFEVENTCONFIG paConfigs;
1669 size_t cConfigs;
1670 int rc;
1671} DBGFR3EVENTCONFIGEXARGS;
1672/** Pointer to a dbgfR3EventConfigEx argument packet. */
1673typedef DBGFR3EVENTCONFIGEXARGS *PDBGFR3EVENTCONFIGEXARGS;
1674
1675
1676/**
1677 * @callback_method_impl{FNVMMEMTRENDEZVOUS, Worker for DBGFR3EventConfigEx.}
1678 */
1679static DECLCALLBACK(VBOXSTRICTRC) dbgfR3EventConfigEx(PVM pVM, PVMCPU pVCpu, void *pvUser)
1680{
1681 if (pVCpu->idCpu == 0)
1682 {
1683 PDBGFR3EVENTCONFIGEXARGS pArgs = (PDBGFR3EVENTCONFIGEXARGS)pvUser;
1684 DBGFEVENTCONFIG volatile const *paConfigs = pArgs->paConfigs;
1685 size_t cConfigs = pArgs->cConfigs;
1686
1687 /*
1688 * Apply the changes.
1689 */
1690 unsigned cChanges = 0;
1691 for (uint32_t i = 0; i < cConfigs; i++)
1692 {
1693 DBGFEVENTTYPE enmType = paConfigs[i].enmType;
1694 AssertReturn(enmType >= DBGFEVENT_FIRST_SELECTABLE && enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1695 if (paConfigs[i].fEnabled)
1696 cChanges += ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, enmType) == false;
1697 else
1698 cChanges += ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, enmType) == true;
1699 }
1700
1701 /*
1702 * Inform HM about changes.
1703 */
1704 if (cChanges > 0 && HMIsEnabled(pVM))
1705 {
1706 HMR3NotifyDebugEventChanged(pVM);
1707 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1708 }
1709 }
1710 else if (HMIsEnabled(pVM))
1711 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1712
1713 return VINF_SUCCESS;
1714}
1715
1716
1717/**
1718 * Configures (enables/disables) multiple selectable debug events.
1719 *
1720 * @returns VBox status code.
1721 * @param pUVM The user mode VM handle.
1722 * @param paConfigs The event to configure and their new state.
1723 * @param cConfigs Number of entries in @a paConfigs.
1724 */
1725VMMR3DECL(int) DBGFR3EventConfigEx(PUVM pUVM, PCDBGFEVENTCONFIG paConfigs, size_t cConfigs)
1726{
1727 /*
1728 * Validate input.
1729 */
1730 size_t i = cConfigs;
1731 while (i-- > 0)
1732 {
1733 AssertReturn(paConfigs[i].enmType >= DBGFEVENT_FIRST_SELECTABLE, VERR_INVALID_PARAMETER);
1734 AssertReturn(paConfigs[i].enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1735 }
1736 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1737 PVM pVM = pUVM->pVM;
1738 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1739
1740 /*
1741 * Apply the changes in EMT(0) and rendezvous with the other CPUs so they
1742 * can sync their data and execution with new debug state.
1743 */
1744 DBGFR3EVENTCONFIGEXARGS Args = { paConfigs, cConfigs, VINF_SUCCESS };
1745 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING | VMMEMTRENDEZVOUS_FLAGS_PRIORITY,
1746 dbgfR3EventConfigEx, &Args);
1747 if (RT_SUCCESS(rc))
1748 rc = Args.rc;
1749 return rc;
1750}
1751
1752
1753/**
1754 * Enables or disables a selectable debug event.
1755 *
1756 * @returns VBox status code.
1757 * @param pUVM The user mode VM handle.
1758 * @param enmEvent The selectable debug event.
1759 * @param fEnabled The new state.
1760 */
1761VMMR3DECL(int) DBGFR3EventConfig(PUVM pUVM, DBGFEVENTTYPE enmEvent, bool fEnabled)
1762{
1763 /*
1764 * Convert to an array call.
1765 */
1766 DBGFEVENTCONFIG EvtCfg = { enmEvent, fEnabled };
1767 return DBGFR3EventConfigEx(pUVM, &EvtCfg, 1);
1768}
1769
1770
1771/**
1772 * Checks if the given selectable event is enabled.
1773 *
1774 * @returns true if enabled, false if not or invalid input.
1775 * @param pUVM The user mode VM handle.
1776 * @param enmEvent The selectable debug event.
1777 * @sa DBGFR3EventQuery
1778 */
1779VMMR3DECL(bool) DBGFR3EventIsEnabled(PUVM pUVM, DBGFEVENTTYPE enmEvent)
1780{
1781 /*
1782 * Validate input.
1783 */
1784 AssertReturn( enmEvent >= DBGFEVENT_HALT_DONE
1785 && enmEvent < DBGFEVENT_END, false);
1786 Assert( enmEvent >= DBGFEVENT_FIRST_SELECTABLE
1787 || enmEvent == DBGFEVENT_BREAKPOINT
1788 || enmEvent == DBGFEVENT_BREAKPOINT_IO
1789 || enmEvent == DBGFEVENT_BREAKPOINT_MMIO);
1790
1791 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1792 PVM pVM = pUVM->pVM;
1793 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1794
1795 /*
1796 * Check the event status.
1797 */
1798 return ASMBitTest(&pVM->dbgf.s.bmSelectedEvents, enmEvent);
1799}
1800
1801
1802/**
1803 * Queries the status of a set of events.
1804 *
1805 * @returns VBox status code.
1806 * @param pUVM The user mode VM handle.
1807 * @param paConfigs The events to query and where to return the state.
1808 * @param cConfigs The number of elements in @a paConfigs.
1809 * @sa DBGFR3EventIsEnabled, DBGF_IS_EVENT_ENABLED
1810 */
1811VMMR3DECL(int) DBGFR3EventQuery(PUVM pUVM, PDBGFEVENTCONFIG paConfigs, size_t cConfigs)
1812{
1813 /*
1814 * Validate input.
1815 */
1816 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1817 PVM pVM = pUVM->pVM;
1818 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1819
1820 for (size_t i = 0; i < cConfigs; i++)
1821 {
1822 DBGFEVENTTYPE enmType = paConfigs[i].enmType;
1823 AssertReturn( enmType >= DBGFEVENT_HALT_DONE
1824 && enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1825 Assert( enmType >= DBGFEVENT_FIRST_SELECTABLE
1826 || enmType == DBGFEVENT_BREAKPOINT
1827 || enmType == DBGFEVENT_BREAKPOINT_IO
1828 || enmType == DBGFEVENT_BREAKPOINT_MMIO);
1829 paConfigs[i].fEnabled = ASMBitTest(&pVM->dbgf.s.bmSelectedEvents, paConfigs[i].enmType);
1830 }
1831
1832 return VINF_SUCCESS;
1833}
1834
1835
1836/**
1837 * dbgfR3InterruptConfigEx argument packet.
1838 */
1839typedef struct DBGFR3INTERRUPTCONFIGEXARGS
1840{
1841 PCDBGFINTERRUPTCONFIG paConfigs;
1842 size_t cConfigs;
1843 int rc;
1844} DBGFR3INTERRUPTCONFIGEXARGS;
1845/** Pointer to a dbgfR3InterruptConfigEx argument packet. */
1846typedef DBGFR3INTERRUPTCONFIGEXARGS *PDBGFR3INTERRUPTCONFIGEXARGS;
1847
1848/**
1849 * @callback_method_impl{FNVMMEMTRENDEZVOUS,
1850 * Worker for DBGFR3InterruptConfigEx.}
1851 */
1852static DECLCALLBACK(VBOXSTRICTRC) dbgfR3InterruptConfigEx(PVM pVM, PVMCPU pVCpu, void *pvUser)
1853{
1854 if (pVCpu->idCpu == 0)
1855 {
1856 PDBGFR3INTERRUPTCONFIGEXARGS pArgs = (PDBGFR3INTERRUPTCONFIGEXARGS)pvUser;
1857 PCDBGFINTERRUPTCONFIG paConfigs = pArgs->paConfigs;
1858 size_t cConfigs = pArgs->cConfigs;
1859
1860 /*
1861 * Apply the changes.
1862 */
1863 bool fChanged = false;
1864 bool fThis;
1865 for (uint32_t i = 0; i < cConfigs; i++)
1866 {
1867 /*
1868 * Hardware interrupts.
1869 */
1870 if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_ENABLED)
1871 {
1872 fChanged |= fThis = ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmHardIntBreakpoints, paConfigs[i].iInterrupt) == false;
1873 if (fThis)
1874 {
1875 Assert(pVM->dbgf.s.cHardIntBreakpoints < 256);
1876 pVM->dbgf.s.cHardIntBreakpoints++;
1877 }
1878 }
1879 else if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_DISABLED)
1880 {
1881 fChanged |= fThis = ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmHardIntBreakpoints, paConfigs[i].iInterrupt) == true;
1882 if (fThis)
1883 {
1884 Assert(pVM->dbgf.s.cHardIntBreakpoints > 0);
1885 pVM->dbgf.s.cHardIntBreakpoints--;
1886 }
1887 }
1888
1889 /*
1890 * Software interrupts.
1891 */
1892 if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_ENABLED)
1893 {
1894 fChanged |= fThis = ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSoftIntBreakpoints, paConfigs[i].iInterrupt) == false;
1895 if (fThis)
1896 {
1897 Assert(pVM->dbgf.s.cSoftIntBreakpoints < 256);
1898 pVM->dbgf.s.cSoftIntBreakpoints++;
1899 }
1900 }
1901 else if (paConfigs[i].enmSoftState == DBGFINTERRUPTSTATE_DISABLED)
1902 {
1903 fChanged |= fThis = ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSoftIntBreakpoints, paConfigs[i].iInterrupt) == true;
1904 if (fThis)
1905 {
1906 Assert(pVM->dbgf.s.cSoftIntBreakpoints > 0);
1907 pVM->dbgf.s.cSoftIntBreakpoints--;
1908 }
1909 }
1910 }
1911
1912 /*
1913 * Update the event bitmap entries.
1914 */
1915 if (pVM->dbgf.s.cHardIntBreakpoints > 0)
1916 fChanged |= ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_HARDWARE) == false;
1917 else
1918 fChanged |= ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_HARDWARE) == true;
1919
1920 if (pVM->dbgf.s.cSoftIntBreakpoints > 0)
1921 fChanged |= ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_SOFTWARE) == false;
1922 else
1923 fChanged |= ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_SOFTWARE) == true;
1924
1925 /*
1926 * Inform HM about changes.
1927 */
1928 if (fChanged && HMIsEnabled(pVM))
1929 {
1930 HMR3NotifyDebugEventChanged(pVM);
1931 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1932 }
1933 }
1934 else if (HMIsEnabled(pVM))
1935 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1936
1937 return VINF_SUCCESS;
1938}
1939
1940
1941/**
1942 * Changes
1943 *
1944 * @returns VBox status code.
1945 * @param pUVM The user mode VM handle.
1946 * @param paConfigs The events to query and where to return the state.
1947 * @param cConfigs The number of elements in @a paConfigs.
1948 * @sa DBGFR3InterruptConfigHardware, DBGFR3InterruptConfigSoftware
1949 */
1950VMMR3DECL(int) DBGFR3InterruptConfigEx(PUVM pUVM, PCDBGFINTERRUPTCONFIG paConfigs, size_t cConfigs)
1951{
1952 /*
1953 * Validate input.
1954 */
1955 size_t i = cConfigs;
1956 while (i-- > 0)
1957 {
1958 AssertReturn(paConfigs[i].enmHardState <= DBGFINTERRUPTSTATE_DONT_TOUCH, VERR_INVALID_PARAMETER);
1959 AssertReturn(paConfigs[i].enmSoftState <= DBGFINTERRUPTSTATE_DONT_TOUCH, VERR_INVALID_PARAMETER);
1960 }
1961
1962 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1963 PVM pVM = pUVM->pVM;
1964 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1965
1966 /*
1967 * Apply the changes in EMT(0) and rendezvous with the other CPUs so they
1968 * can sync their data and execution with new debug state.
1969 */
1970 DBGFR3INTERRUPTCONFIGEXARGS Args = { paConfigs, cConfigs, VINF_SUCCESS };
1971 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING | VMMEMTRENDEZVOUS_FLAGS_PRIORITY,
1972 dbgfR3InterruptConfigEx, &Args);
1973 if (RT_SUCCESS(rc))
1974 rc = Args.rc;
1975 return rc;
1976}
1977
1978
1979/**
1980 * Configures interception of a hardware interrupt.
1981 *
1982 * @returns VBox status code.
1983 * @param pUVM The user mode VM handle.
1984 * @param iInterrupt The interrupt number.
1985 * @param fEnabled Whether interception is enabled or not.
1986 * @sa DBGFR3InterruptSoftwareConfig, DBGFR3InterruptConfigEx
1987 */
1988VMMR3DECL(int) DBGFR3InterruptHardwareConfig(PUVM pUVM, uint8_t iInterrupt, bool fEnabled)
1989{
1990 /*
1991 * Convert to DBGFR3InterruptConfigEx call.
1992 */
1993 DBGFINTERRUPTCONFIG IntCfg = { iInterrupt, (uint8_t)fEnabled, DBGFINTERRUPTSTATE_DONT_TOUCH };
1994 return DBGFR3InterruptConfigEx(pUVM, &IntCfg, 1);
1995}
1996
1997
1998/**
1999 * Configures interception of a software interrupt.
2000 *
2001 * @returns VBox status code.
2002 * @param pUVM The user mode VM handle.
2003 * @param iInterrupt The interrupt number.
2004 * @param fEnabled Whether interception is enabled or not.
2005 * @sa DBGFR3InterruptHardwareConfig, DBGFR3InterruptConfigEx
2006 */
2007VMMR3DECL(int) DBGFR3InterruptSoftwareConfig(PUVM pUVM, uint8_t iInterrupt, bool fEnabled)
2008{
2009 /*
2010 * Convert to DBGFR3InterruptConfigEx call.
2011 */
2012 DBGFINTERRUPTCONFIG IntCfg = { iInterrupt, DBGFINTERRUPTSTATE_DONT_TOUCH, (uint8_t)fEnabled };
2013 return DBGFR3InterruptConfigEx(pUVM, &IntCfg, 1);
2014}
2015
2016
2017/**
2018 * Checks whether interception is enabled for a hardware interrupt.
2019 *
2020 * @returns true if enabled, false if not or invalid input.
2021 * @param pUVM The user mode VM handle.
2022 * @param iInterrupt The interrupt number.
2023 * @sa DBGFR3InterruptSoftwareIsEnabled, DBGF_IS_HARDWARE_INT_ENABLED,
2024 * DBGF_IS_SOFTWARE_INT_ENABLED
2025 */
2026VMMR3DECL(int) DBGFR3InterruptHardwareIsEnabled(PUVM pUVM, uint8_t iInterrupt)
2027{
2028 /*
2029 * Validate input.
2030 */
2031 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2032 PVM pVM = pUVM->pVM;
2033 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2034
2035 /*
2036 * Check it.
2037 */
2038 return ASMBitTest(&pVM->dbgf.s.bmHardIntBreakpoints, iInterrupt);
2039}
2040
2041
2042/**
2043 * Checks whether interception is enabled for a software interrupt.
2044 *
2045 * @returns true if enabled, false if not or invalid input.
2046 * @param pUVM The user mode VM handle.
2047 * @param iInterrupt The interrupt number.
2048 * @sa DBGFR3InterruptHardwareIsEnabled, DBGF_IS_SOFTWARE_INT_ENABLED,
2049 * DBGF_IS_HARDWARE_INT_ENABLED,
2050 */
2051VMMR3DECL(int) DBGFR3InterruptSoftwareIsEnabled(PUVM pUVM, uint8_t iInterrupt)
2052{
2053 /*
2054 * Validate input.
2055 */
2056 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2057 PVM pVM = pUVM->pVM;
2058 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2059
2060 /*
2061 * Check it.
2062 */
2063 return ASMBitTest(&pVM->dbgf.s.bmSoftIntBreakpoints, iInterrupt);
2064}
2065
2066
2067
2068/**
2069 * Call this to single step programmatically.
2070 *
2071 * You must pass down the return code to the EM loop! That's
2072 * where the actual single stepping take place (at least in the
2073 * current implementation).
2074 *
2075 * @returns VINF_EM_DBG_STEP
2076 *
2077 * @param pVCpu The cross context virtual CPU structure.
2078 *
2079 * @thread VCpu EMT
2080 * @internal
2081 */
2082VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
2083{
2084 VMCPU_ASSERT_EMT(pVCpu);
2085
2086 pVCpu->dbgf.s.fSingleSteppingRaw = true;
2087 return VINF_EM_DBG_STEP;
2088}
2089
2090
2091/**
2092 * Inject an NMI into a running VM (only VCPU 0!)
2093 *
2094 * @returns VBox status code.
2095 * @param pUVM The user mode VM structure.
2096 * @param idCpu The ID of the CPU to inject the NMI on.
2097 */
2098VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
2099{
2100 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2101 PVM pVM = pUVM->pVM;
2102 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2103 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
2104
2105 /** @todo Implement generic NMI injection. */
2106 /** @todo NEM: NMI injection */
2107 if (!HMIsEnabled(pVM))
2108 return VERR_NOT_SUP_BY_NEM;
2109
2110 VMCPU_FF_SET(pVM->apCpusR3[idCpu], VMCPU_FF_INTERRUPT_NMI);
2111 return VINF_SUCCESS;
2112}
2113
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