VirtualBox

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

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

Copyright year updates by scm.

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