VirtualBox

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

Last change on this file since 69300 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 68.8 KB
Line 
1/* $Id: DBGF.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 return VINF_SUCCESS;
189 }
190 dbgfR3OSTerm(pUVM);
191 }
192 }
193 dbgfR3AsTerm(pUVM);
194 }
195 dbgfR3RegTerm(pUVM);
196 }
197 dbgfR3TraceTerm(pVM);
198 }
199 dbgfR3InfoTerm(pUVM);
200 }
201 return rc;
202}
203
204
205/**
206 * Terminates and cleans up resources allocated by the DBGF.
207 *
208 * @returns VBox status code.
209 * @param pVM The cross context VM structure.
210 */
211VMMR3_INT_DECL(int) DBGFR3Term(PVM pVM)
212{
213 PUVM pUVM = pVM->pUVM;
214
215 dbgfR3PlugInTerm(pUVM);
216 dbgfR3OSTerm(pUVM);
217 dbgfR3AsTerm(pUVM);
218 dbgfR3RegTerm(pUVM);
219 dbgfR3TraceTerm(pVM);
220 dbgfR3InfoTerm(pUVM);
221
222 return VINF_SUCCESS;
223}
224
225
226/**
227 * Called when the VM is powered off to detach debuggers.
228 *
229 * @param pVM The cross context VM structure.
230 */
231VMMR3_INT_DECL(void) DBGFR3PowerOff(PVM pVM)
232{
233
234 /*
235 * Send a termination event to any attached debugger.
236 */
237 /* wait to become the speaker (we should already be that). */
238 if ( pVM->dbgf.s.fAttached
239 && RTSemPingShouldWait(&pVM->dbgf.s.PingPong))
240 RTSemPingWait(&pVM->dbgf.s.PingPong, 5000);
241
242 if (pVM->dbgf.s.fAttached)
243 {
244 /* Just mark it as detached if we're not in a position to send a power
245 off event. It should fail later on. */
246 if (!RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
247 {
248 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
249 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
250 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
251 }
252
253 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
254 {
255 /* Try send the power off event. */
256 int rc;
257 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
258 if (enmCmd == DBGFCMD_DETACH_DEBUGGER)
259 /* the debugger beat us to initiating the detaching. */
260 rc = VINF_SUCCESS;
261 else
262 {
263 /* ignore the command (if any). */
264 enmCmd = DBGFCMD_NO_COMMAND;
265 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_POWERING_OFF;
266 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
267 rc = RTSemPing(&pVM->dbgf.s.PingPong);
268 }
269
270 /*
271 * Process commands and priority requests until we get a command
272 * indicating that the debugger has detached.
273 */
274 uint32_t cPollHack = 1;
275 PVMCPU pVCpu = VMMGetCpu(pVM);
276 while (RT_SUCCESS(rc))
277 {
278 if (enmCmd != DBGFCMD_NO_COMMAND)
279 {
280 /* process command */
281 bool fResumeExecution;
282 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
283 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
284 if (enmCmd == DBGFCMD_DETACHED_DEBUGGER)
285 break;
286 enmCmd = DBGFCMD_NO_COMMAND;
287 }
288 else
289 {
290 /* Wait for new command, processing pending priority requests
291 first. The request processing is a bit crazy, but
292 unfortunately required by plugin unloading. */
293 if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
294 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
295 {
296 LogFlow(("DBGFR3PowerOff: Processes priority requests...\n"));
297 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
298 if (rc == VINF_SUCCESS)
299 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, true /*fPriorityOnly*/);
300 LogFlow(("DBGFR3PowerOff: VMR3ReqProcess -> %Rrc\n", rc));
301 cPollHack = 1;
302 }
303 /* Need to handle rendezvous too, for generic debug event management. */
304 else if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
305 {
306 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
307 AssertLogRel(rc == VINF_SUCCESS);
308 cPollHack = 1;
309 }
310 else if (cPollHack < 120)
311 cPollHack++;
312
313 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
314 if (RT_SUCCESS(rc))
315 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
316 else if (rc == VERR_TIMEOUT)
317 rc = VINF_SUCCESS;
318 }
319 }
320
321 /*
322 * Clear the FF so we won't get confused later on.
323 */
324 VM_FF_CLEAR(pVM, VM_FF_DBGF);
325 }
326 }
327}
328
329
330/**
331 * Applies relocations to data and code managed by this
332 * component. This function will be called at init and
333 * whenever the VMM need to relocate it self inside the GC.
334 *
335 * @param pVM The cross context VM structure.
336 * @param offDelta Relocation delta relative to old location.
337 */
338VMMR3_INT_DECL(void) DBGFR3Relocate(PVM pVM, RTGCINTPTR offDelta)
339{
340 dbgfR3TraceRelocate(pVM);
341 dbgfR3AsRelocate(pVM->pUVM, offDelta);
342}
343
344
345/**
346 * Waits a little while for a debuggger to attach.
347 *
348 * @returns True is a debugger have attached.
349 * @param pVM The cross context VM structure.
350 * @param pVCpu The cross context per CPU structure.
351 * @param enmEvent Event.
352 *
353 * @thread EMT(pVCpu)
354 */
355bool dbgfR3WaitForAttach(PVM pVM, PVMCPU pVCpu, DBGFEVENTTYPE enmEvent)
356{
357 /*
358 * First a message.
359 */
360#ifndef RT_OS_L4
361
362# if !defined(DEBUG) || defined(DEBUG_sandervl) || defined(DEBUG_frank) || defined(IEM_VERIFICATION_MODE)
363 int cWait = 10;
364# else
365 int cWait = HMIsEnabled(pVM)
366 && ( enmEvent == DBGFEVENT_ASSERTION_HYPER
367 || enmEvent == DBGFEVENT_FATAL_ERROR)
368 && !RTEnvExist("VBOX_DBGF_WAIT_FOR_ATTACH")
369 ? 10
370 : 150;
371# endif
372 RTStrmPrintf(g_pStdErr, "DBGF: No debugger attached, waiting %d second%s for one to attach (event=%d)\n",
373 cWait / 10, cWait != 10 ? "s" : "", enmEvent);
374 RTStrmFlush(g_pStdErr);
375 while (cWait > 0)
376 {
377 RTThreadSleep(100);
378 if (pVM->dbgf.s.fAttached)
379 {
380 RTStrmPrintf(g_pStdErr, "Attached!\n");
381 RTStrmFlush(g_pStdErr);
382 return true;
383 }
384
385 /* Process priority stuff. */
386 if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
387 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
388 {
389 int rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
390 if (rc == VINF_SUCCESS)
391 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, true /*fPriorityOnly*/);
392 if (rc != VINF_SUCCESS)
393 {
394 RTStrmPrintf(g_pStdErr, "[rcReq=%Rrc, ignored!]", rc);
395 RTStrmFlush(g_pStdErr);
396 }
397 }
398
399 /* next */
400 if (!(cWait % 10))
401 {
402 RTStrmPrintf(g_pStdErr, "%d.", cWait / 10);
403 RTStrmFlush(g_pStdErr);
404 }
405 cWait--;
406 }
407#endif
408
409 RTStrmPrintf(g_pStdErr, "Stopping the VM!\n");
410 RTStrmFlush(g_pStdErr);
411 return false;
412}
413
414
415/**
416 * Forced action callback.
417 *
418 * The VMM will call this from it's main loop when either VM_FF_DBGF or
419 * VMCPU_FF_DBGF are set.
420 *
421 * The function checks for and executes pending commands from the debugger.
422 * Then it checks for pending debug events and serves these.
423 *
424 * @returns VINF_SUCCESS normally.
425 * @returns VERR_DBGF_RAISE_FATAL_ERROR to pretend a fatal error happened.
426 * @param pVM The cross context VM structure.
427 * @param pVCpu The cross context per CPU structure.
428 */
429VMMR3_INT_DECL(int) DBGFR3VMMForcedAction(PVM pVM, PVMCPU pVCpu)
430{
431 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
432
433 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_DBGF))
434 {
435 /*
436 * Command pending? Process it.
437 */
438 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
439 {
440 bool fResumeExecution;
441 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
442 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
443 rcStrict = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
444 if (!fResumeExecution)
445 rcStrict = dbgfR3VMMWait(pVM);
446 }
447 }
448
449 /*
450 * Dispatch pending events.
451 */
452 if (VMCPU_FF_TEST_AND_CLEAR(pVCpu, VMCPU_FF_DBGF))
453 {
454 if ( pVCpu->dbgf.s.cEvents > 0
455 && pVCpu->dbgf.s.aEvents[pVCpu->dbgf.s.cEvents - 1].enmState == DBGFEVENTSTATE_CURRENT)
456 {
457 VBOXSTRICTRC rcStrict2 = DBGFR3EventHandlePending(pVM, pVCpu);
458 if ( rcStrict2 != VINF_SUCCESS
459 && ( rcStrict == VINF_SUCCESS
460 || RT_FAILURE(rcStrict2)
461 || rcStrict2 < rcStrict) ) /** @todo oversimplified? */
462 rcStrict = rcStrict2;
463 }
464 }
465
466 return VBOXSTRICTRC_TODO(rcStrict);
467}
468
469
470/**
471 * Flag whether the event implies that we're stopped in the hypervisor code
472 * and have to block certain operations.
473 *
474 * @param pVM The cross context VM structure.
475 * @param enmEvent The event.
476 */
477static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
478{
479 switch (enmEvent)
480 {
481 case DBGFEVENT_STEPPED_HYPER:
482 case DBGFEVENT_ASSERTION_HYPER:
483 case DBGFEVENT_BREAKPOINT_HYPER:
484 pVM->dbgf.s.fStoppedInHyper = true;
485 break;
486 default:
487 pVM->dbgf.s.fStoppedInHyper = false;
488 break;
489 }
490}
491
492
493/**
494 * Try to determine the event context.
495 *
496 * @returns debug event context.
497 * @param pVM The cross context VM structure.
498 */
499static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
500{
501 /** @todo SMP support! */
502 PVMCPU pVCpu = &pVM->aCpus[0];
503
504 switch (EMGetState(pVCpu))
505 {
506 case EMSTATE_RAW:
507 case EMSTATE_DEBUG_GUEST_RAW:
508 return DBGFEVENTCTX_RAW;
509
510 case EMSTATE_REM:
511 case EMSTATE_DEBUG_GUEST_REM:
512 return DBGFEVENTCTX_REM;
513
514 case EMSTATE_DEBUG_HYPER:
515 case EMSTATE_GURU_MEDITATION:
516 return DBGFEVENTCTX_HYPER;
517
518 default:
519 return DBGFEVENTCTX_OTHER;
520 }
521}
522
523/**
524 * The common event prologue code.
525 * It will set the 'stopped-in-hyper' flag, make sure someone is attached,
526 * and perhaps process any high priority pending actions (none yet).
527 *
528 * @returns VBox status code.
529 * @param pVM The cross context VM structure.
530 * @param enmEvent The event to be sent.
531 */
532static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
533{
534 /** @todo SMP */
535 PVMCPU pVCpu = VMMGetCpu(pVM);
536
537 /*
538 * Check if a debugger is attached.
539 */
540 if ( !pVM->dbgf.s.fAttached
541 && !dbgfR3WaitForAttach(pVM, pVCpu, enmEvent))
542 {
543 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
544 return VERR_DBGF_NOT_ATTACHED;
545 }
546
547 /*
548 * Sync back the state from the REM.
549 */
550 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
551#ifdef VBOX_WITH_REM
552 if (!pVM->dbgf.s.fStoppedInHyper)
553 REMR3StateUpdate(pVM, pVCpu);
554#endif
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_PENDING(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_REQUEST)
830 && !VMCPU_FF_IS_PENDING(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_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
843 {
844 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
845 cPollHack = 1;
846 }
847 else if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
848 || VMCPU_FF_IS_PENDING(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 bool fIsHyper = dbgfR3FigureEventCtx(pVM) == DBGFEVENTCTX_HYPER;
1350 size_t cbRead = 0;
1351 uint8_t abOpcode[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1352 int rc = PGMR3DbgReadGCPtr(pVM, abOpcode, !fIsHyper ? CPUMGetGuestFlatPC(pVCpu) : CPUMGetHyperRIP(pVCpu),
1353 sizeof(abOpcode) - 1, 0 /*fFlags*/, &cbRead);
1354 if (RT_SUCCESS(rc))
1355 {
1356 /*
1357 * Do minimal parsing. No real need to involve the disassembler here.
1358 */
1359 uint8_t *pb = abOpcode;
1360 for (;;)
1361 {
1362 switch (*pb++)
1363 {
1364 default:
1365 return DBGFSTEPINSTRTYPE_OTHER;
1366
1367 case 0xe8: /* call rel16/32 */
1368 case 0x9a: /* call farptr */
1369 case 0xcc: /* int3 */
1370 case 0xcd: /* int xx */
1371 // case 0xce: /* into */
1372 return DBGFSTEPINSTRTYPE_CALL;
1373
1374 case 0xc2: /* ret xx */
1375 case 0xc3: /* ret */
1376 case 0xca: /* retf xx */
1377 case 0xcb: /* retf */
1378 case 0xcf: /* iret */
1379 return DBGFSTEPINSTRTYPE_RET;
1380
1381 case 0xff:
1382 if ( ((*pb >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) == 2 /* call indir */
1383 || ((*pb >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) == 3) /* call indir-farptr */
1384 return DBGFSTEPINSTRTYPE_CALL;
1385 return DBGFSTEPINSTRTYPE_OTHER;
1386
1387 case 0x0f:
1388 switch (*pb++)
1389 {
1390 case 0x05: /* syscall */
1391 case 0x34: /* sysenter */
1392 return DBGFSTEPINSTRTYPE_CALL;
1393 case 0x07: /* sysret */
1394 case 0x35: /* sysexit */
1395 return DBGFSTEPINSTRTYPE_RET;
1396 }
1397 break;
1398
1399 /* Must handle some REX prefixes. So we do all normal prefixes. */
1400 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
1401 case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
1402 if (fIsHyper) /* ASSUMES 32-bit raw-mode! */
1403 return DBGFSTEPINSTRTYPE_OTHER;
1404 if (!CPUMIsGuestIn64BitCode(pVCpu))
1405 return DBGFSTEPINSTRTYPE_OTHER;
1406 break;
1407
1408 case 0x2e: /* CS */
1409 case 0x36: /* SS */
1410 case 0x3e: /* DS */
1411 case 0x26: /* ES */
1412 case 0x64: /* FS */
1413 case 0x65: /* GS */
1414 case 0x66: /* op size */
1415 case 0x67: /* addr size */
1416 case 0xf0: /* lock */
1417 case 0xf2: /* REPNZ */
1418 case 0xf3: /* REPZ */
1419 break;
1420 }
1421 }
1422 }
1423
1424 return DBGFSTEPINSTRTYPE_INVALID;
1425}
1426
1427
1428/**
1429 * Checks if the stepping has reached a stop point.
1430 *
1431 * Called when raising a stepped event.
1432 *
1433 * @returns true if the event should be raised, false if we should take one more
1434 * step first.
1435 * @param pVM The cross context VM structure.
1436 * @param pVCpu The cross context per CPU structure of the calling EMT.
1437 * @thread EMT(pVCpu)
1438 */
1439static bool dbgfStepAreWeThereYet(PVM pVM, PVMCPU pVCpu)
1440{
1441 /*
1442 * Check valid pVCpu and that it matches the CPU one stepping.
1443 */
1444 if (pVCpu)
1445 {
1446 if (pVCpu->idCpu == pVM->dbgf.s.SteppingFilter.idCpu)
1447 {
1448 /*
1449 * Increase the number of steps and see if we've reached the max.
1450 */
1451 pVM->dbgf.s.SteppingFilter.cSteps++;
1452 if (pVM->dbgf.s.SteppingFilter.cSteps < pVM->dbgf.s.SteppingFilter.cMaxSteps)
1453 {
1454 /*
1455 * Check PC and SP address filtering.
1456 */
1457 if (pVM->dbgf.s.SteppingFilter.fFlags & (DBGF_STEP_F_STOP_ON_ADDRESS | DBGF_STEP_F_STOP_ON_STACK_POP))
1458 {
1459 bool fIsHyper = dbgfR3FigureEventCtx(pVM) == DBGFEVENTCTX_HYPER;
1460 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1461 && pVM->dbgf.s.SteppingFilter.AddrPc == (!fIsHyper ? CPUMGetGuestFlatPC(pVCpu) : CPUMGetHyperRIP(pVCpu)))
1462 return true;
1463 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1464 && (!fIsHyper ? CPUMGetGuestFlatSP(pVCpu) : (uint64_t)CPUMGetHyperESP(pVCpu))
1465 - pVM->dbgf.s.SteppingFilter.AddrStackPop
1466 < pVM->dbgf.s.SteppingFilter.cbStackPop)
1467 return true;
1468 }
1469
1470 /*
1471 * Do step-over filtering separate from the step-into one.
1472 */
1473 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_OVER)
1474 {
1475 DBGFSTEPINSTRTYPE enmType = dbgfStepGetCurInstrType(pVM, pVCpu);
1476 switch (enmType)
1477 {
1478 default:
1479 if ( pVM->dbgf.s.SteppingFilter.uCallDepth != 0
1480 || (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_FILTER_MASK))
1481 break;
1482 return true;
1483 case DBGFSTEPINSTRTYPE_CALL:
1484 if ( (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_CALL)
1485 && pVM->dbgf.s.SteppingFilter.uCallDepth == 0)
1486 return true;
1487 pVM->dbgf.s.SteppingFilter.uCallDepth++;
1488 break;
1489 case DBGFSTEPINSTRTYPE_RET:
1490 if (pVM->dbgf.s.SteppingFilter.uCallDepth == 0)
1491 {
1492 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_RET)
1493 return true;
1494 /* If after return, we use the cMaxStep limit to stop the next time. */
1495 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_AFTER_RET)
1496 pVM->dbgf.s.SteppingFilter.cMaxSteps = pVM->dbgf.s.SteppingFilter.cSteps + 1;
1497 }
1498 else if (pVM->dbgf.s.SteppingFilter.uCallDepth > 0)
1499 pVM->dbgf.s.SteppingFilter.uCallDepth--;
1500 break;
1501 }
1502 return false;
1503 }
1504 /*
1505 * Filtered step-into.
1506 */
1507 else if ( pVM->dbgf.s.SteppingFilter.fFlags
1508 & (DBGF_STEP_F_STOP_ON_CALL | DBGF_STEP_F_STOP_ON_RET | DBGF_STEP_F_STOP_AFTER_RET))
1509 {
1510 DBGFSTEPINSTRTYPE enmType = dbgfStepGetCurInstrType(pVM, pVCpu);
1511 switch (enmType)
1512 {
1513 default:
1514 break;
1515 case DBGFSTEPINSTRTYPE_CALL:
1516 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_CALL)
1517 return true;
1518 break;
1519 case DBGFSTEPINSTRTYPE_RET:
1520 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_ON_RET)
1521 return true;
1522 /* If after return, we use the cMaxStep limit to stop the next time. */
1523 if (pVM->dbgf.s.SteppingFilter.fFlags & DBGF_STEP_F_STOP_AFTER_RET)
1524 pVM->dbgf.s.SteppingFilter.cMaxSteps = pVM->dbgf.s.SteppingFilter.cSteps + 1;
1525 break;
1526 }
1527 return false;
1528 }
1529 }
1530 }
1531 }
1532
1533 return true;
1534}
1535
1536
1537/**
1538 * Step Into.
1539 *
1540 * A single step event is generated from this command.
1541 * The current implementation is not reliable, so don't rely on the event coming.
1542 *
1543 * @returns VBox status code.
1544 * @param pUVM The user mode VM handle.
1545 * @param idCpu The ID of the CPU to single step on.
1546 */
1547VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
1548{
1549 return DBGFR3StepEx(pUVM, idCpu, DBGF_STEP_F_INTO, NULL, NULL, 0, 1);
1550}
1551
1552
1553/**
1554 * Full fleged step.
1555 *
1556 * This extended stepping API allows for doing multiple steps before raising an
1557 * event, helping implementing step over, step out and other more advanced
1558 * features.
1559 *
1560 * Like the DBGFR3Step() API, this will normally generate a DBGFEVENT_STEPPED or
1561 * DBGFEVENT_STEPPED_EVENT. However the stepping may be interrupted by other
1562 * events, which will abort the stepping.
1563 *
1564 * The stop on pop area feature is for safeguarding step out.
1565 *
1566 * Please note though, that it will always use stepping and never breakpoints.
1567 * While this allows for a much greater flexibility it can at times be rather
1568 * slow.
1569 *
1570 * @returns VBox status code.
1571 * @param pUVM The user mode VM handle.
1572 * @param idCpu The ID of the CPU to single step on.
1573 * @param fFlags Flags controlling the stepping, DBGF_STEP_F_XXX.
1574 * Either DBGF_STEP_F_INTO or DBGF_STEP_F_OVER must
1575 * always be specified.
1576 * @param pStopPcAddr Address to stop executing at. Completely ignored
1577 * unless DBGF_STEP_F_STOP_ON_ADDRESS is specified.
1578 * @param pStopPopAddr Stack address that SP must be lower than when
1579 * performing DBGF_STEP_F_STOP_ON_STACK_POP filtering.
1580 * @param cbStopPop The range starting at @a pStopPopAddr which is
1581 * considered to be within the same thread stack. Note
1582 * that the API allows @a pStopPopAddr and @a cbStopPop
1583 * to form an area that wraps around and it will
1584 * consider the part starting at 0 as included.
1585 * @param cMaxSteps The maximum number of steps to take. This is to
1586 * prevent stepping for ever, so passing UINT32_MAX is
1587 * not recommended.
1588 *
1589 * @remarks The two address arguments must be guest context virtual addresses,
1590 * or HMA. The code doesn't make much of a point of out HMA, though.
1591 */
1592VMMR3DECL(int) DBGFR3StepEx(PUVM pUVM, VMCPUID idCpu, uint32_t fFlags, PCDBGFADDRESS pStopPcAddr,
1593 PCDBGFADDRESS pStopPopAddr, RTGCUINTPTR cbStopPop, uint32_t cMaxSteps)
1594{
1595 /*
1596 * Check state.
1597 */
1598 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1599 PVM pVM = pUVM->pVM;
1600 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1601 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1602 AssertReturn(!(fFlags & ~DBGF_STEP_F_VALID_MASK), VERR_INVALID_FLAGS);
1603 AssertReturn(RT_BOOL(fFlags & DBGF_STEP_F_INTO) != RT_BOOL(fFlags & DBGF_STEP_F_OVER), VERR_INVALID_FLAGS);
1604 if (fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1605 {
1606 AssertReturn(RT_VALID_PTR(pStopPcAddr), VERR_INVALID_POINTER);
1607 AssertReturn(DBGFADDRESS_IS_VALID(pStopPcAddr), VERR_INVALID_PARAMETER);
1608 AssertReturn(DBGFADDRESS_IS_VIRT_GC(pStopPcAddr), VERR_INVALID_PARAMETER);
1609 }
1610 AssertReturn(!(fFlags & DBGF_STEP_F_STOP_ON_STACK_POP) || RT_VALID_PTR(pStopPopAddr), VERR_INVALID_POINTER);
1611 if (fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1612 {
1613 AssertReturn(RT_VALID_PTR(pStopPopAddr), VERR_INVALID_POINTER);
1614 AssertReturn(DBGFADDRESS_IS_VALID(pStopPopAddr), VERR_INVALID_PARAMETER);
1615 AssertReturn(DBGFADDRESS_IS_VIRT_GC(pStopPopAddr), VERR_INVALID_PARAMETER);
1616 AssertReturn(cbStopPop > 0, VERR_INVALID_PARAMETER);
1617 }
1618
1619 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1620 if (RT_LIKELY(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong)))
1621 { /* likely */ }
1622 else
1623 return VERR_SEM_OUT_OF_TURN;
1624 Assert(pVM->dbgf.s.SteppingFilter.idCpu == NIL_VMCPUID);
1625
1626 /*
1627 * Send the ping back to the emulation thread telling it to run.
1628 */
1629 if (fFlags == DBGF_STEP_F_INTO)
1630 pVM->dbgf.s.SteppingFilter.idCpu = NIL_VMCPUID;
1631 else
1632 pVM->dbgf.s.SteppingFilter.idCpu = idCpu;
1633 pVM->dbgf.s.SteppingFilter.fFlags = fFlags;
1634 if (fFlags & DBGF_STEP_F_STOP_ON_ADDRESS)
1635 pVM->dbgf.s.SteppingFilter.AddrPc = pStopPcAddr->FlatPtr;
1636 else
1637 pVM->dbgf.s.SteppingFilter.AddrPc = 0;
1638 if (fFlags & DBGF_STEP_F_STOP_ON_STACK_POP)
1639 {
1640 pVM->dbgf.s.SteppingFilter.AddrStackPop = pStopPopAddr->FlatPtr;
1641 pVM->dbgf.s.SteppingFilter.cbStackPop = cbStopPop;
1642 }
1643 else
1644 {
1645 pVM->dbgf.s.SteppingFilter.AddrStackPop = 0;
1646 pVM->dbgf.s.SteppingFilter.cbStackPop = RTGCPTR_MAX;
1647 }
1648
1649 pVM->dbgf.s.SteppingFilter.cMaxSteps = cMaxSteps;
1650 pVM->dbgf.s.SteppingFilter.cSteps = 0;
1651 pVM->dbgf.s.SteppingFilter.uCallDepth = 0;
1652
1653/** @todo SMP (idCpu) */
1654 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1655 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1656 AssertRC(rc);
1657 return rc;
1658}
1659
1660
1661
1662/**
1663 * dbgfR3EventConfigEx argument packet.
1664 */
1665typedef struct DBGFR3EVENTCONFIGEXARGS
1666{
1667 PCDBGFEVENTCONFIG paConfigs;
1668 size_t cConfigs;
1669 int rc;
1670} DBGFR3EVENTCONFIGEXARGS;
1671/** Pointer to a dbgfR3EventConfigEx argument packet. */
1672typedef DBGFR3EVENTCONFIGEXARGS *PDBGFR3EVENTCONFIGEXARGS;
1673
1674
1675/**
1676 * @callback_method_impl{FNVMMEMTRENDEZVOUS, Worker for DBGFR3EventConfigEx.}
1677 */
1678static DECLCALLBACK(VBOXSTRICTRC) dbgfR3EventConfigEx(PVM pVM, PVMCPU pVCpu, void *pvUser)
1679{
1680 if (pVCpu->idCpu == 0)
1681 {
1682 PDBGFR3EVENTCONFIGEXARGS pArgs = (PDBGFR3EVENTCONFIGEXARGS)pvUser;
1683 DBGFEVENTCONFIG volatile const *paConfigs = pArgs->paConfigs;
1684 size_t cConfigs = pArgs->cConfigs;
1685
1686 /*
1687 * Apply the changes.
1688 */
1689 unsigned cChanges = 0;
1690 for (uint32_t i = 0; i < cConfigs; i++)
1691 {
1692 DBGFEVENTTYPE enmType = paConfigs[i].enmType;
1693 AssertReturn(enmType >= DBGFEVENT_FIRST_SELECTABLE && enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1694 if (paConfigs[i].fEnabled)
1695 cChanges += ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, enmType) == false;
1696 else
1697 cChanges += ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, enmType) == true;
1698 }
1699
1700 /*
1701 * Inform HM about changes.
1702 */
1703 if (cChanges > 0 && HMIsEnabled(pVM))
1704 {
1705 HMR3NotifyDebugEventChanged(pVM);
1706 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1707 }
1708 }
1709 else if (HMIsEnabled(pVM))
1710 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1711
1712 return VINF_SUCCESS;
1713}
1714
1715
1716/**
1717 * Configures (enables/disables) multiple selectable debug events.
1718 *
1719 * @returns VBox status code.
1720 * @param pUVM The user mode VM handle.
1721 * @param paConfigs The event to configure and their new state.
1722 * @param cConfigs Number of entries in @a paConfigs.
1723 */
1724VMMR3DECL(int) DBGFR3EventConfigEx(PUVM pUVM, PCDBGFEVENTCONFIG paConfigs, size_t cConfigs)
1725{
1726 /*
1727 * Validate input.
1728 */
1729 size_t i = cConfigs;
1730 while (i-- > 0)
1731 {
1732 AssertReturn(paConfigs[i].enmType >= DBGFEVENT_FIRST_SELECTABLE, VERR_INVALID_PARAMETER);
1733 AssertReturn(paConfigs[i].enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1734 }
1735 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1736 PVM pVM = pUVM->pVM;
1737 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1738
1739 /*
1740 * Apply the changes in EMT(0) and rendezvous with the other CPUs so they
1741 * can sync their data and execution with new debug state.
1742 */
1743 DBGFR3EVENTCONFIGEXARGS Args = { paConfigs, cConfigs, VINF_SUCCESS };
1744 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING | VMMEMTRENDEZVOUS_FLAGS_PRIORITY,
1745 dbgfR3EventConfigEx, &Args);
1746 if (RT_SUCCESS(rc))
1747 rc = Args.rc;
1748 return rc;
1749}
1750
1751
1752/**
1753 * Enables or disables a selectable debug event.
1754 *
1755 * @returns VBox status code.
1756 * @param pUVM The user mode VM handle.
1757 * @param enmEvent The selectable debug event.
1758 * @param fEnabled The new state.
1759 */
1760VMMR3DECL(int) DBGFR3EventConfig(PUVM pUVM, DBGFEVENTTYPE enmEvent, bool fEnabled)
1761{
1762 /*
1763 * Convert to an array call.
1764 */
1765 DBGFEVENTCONFIG EvtCfg = { enmEvent, fEnabled };
1766 return DBGFR3EventConfigEx(pUVM, &EvtCfg, 1);
1767}
1768
1769
1770/**
1771 * Checks if the given selectable event is enabled.
1772 *
1773 * @returns true if enabled, false if not or invalid input.
1774 * @param pUVM The user mode VM handle.
1775 * @param enmEvent The selectable debug event.
1776 * @sa DBGFR3EventQuery
1777 */
1778VMMR3DECL(bool) DBGFR3EventIsEnabled(PUVM pUVM, DBGFEVENTTYPE enmEvent)
1779{
1780 /*
1781 * Validate input.
1782 */
1783 AssertReturn( enmEvent >= DBGFEVENT_HALT_DONE
1784 && enmEvent < DBGFEVENT_END, false);
1785 Assert( enmEvent >= DBGFEVENT_FIRST_SELECTABLE
1786 || enmEvent == DBGFEVENT_BREAKPOINT
1787 || enmEvent == DBGFEVENT_BREAKPOINT_IO
1788 || enmEvent == DBGFEVENT_BREAKPOINT_MMIO);
1789
1790 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1791 PVM pVM = pUVM->pVM;
1792 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1793
1794 /*
1795 * Check the event status.
1796 */
1797 return ASMBitTest(&pVM->dbgf.s.bmSelectedEvents, enmEvent);
1798}
1799
1800
1801/**
1802 * Queries the status of a set of events.
1803 *
1804 * @returns VBox status code.
1805 * @param pUVM The user mode VM handle.
1806 * @param paConfigs The events to query and where to return the state.
1807 * @param cConfigs The number of elements in @a paConfigs.
1808 * @sa DBGFR3EventIsEnabled, DBGF_IS_EVENT_ENABLED
1809 */
1810VMMR3DECL(int) DBGFR3EventQuery(PUVM pUVM, PDBGFEVENTCONFIG paConfigs, size_t cConfigs)
1811{
1812 /*
1813 * Validate input.
1814 */
1815 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1816 PVM pVM = pUVM->pVM;
1817 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1818
1819 for (size_t i = 0; i < cConfigs; i++)
1820 {
1821 DBGFEVENTTYPE enmType = paConfigs[i].enmType;
1822 AssertReturn( enmType >= DBGFEVENT_HALT_DONE
1823 && enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1824 Assert( enmType >= DBGFEVENT_FIRST_SELECTABLE
1825 || enmType == DBGFEVENT_BREAKPOINT
1826 || enmType == DBGFEVENT_BREAKPOINT_IO
1827 || enmType == DBGFEVENT_BREAKPOINT_MMIO);
1828 paConfigs[i].fEnabled = ASMBitTest(&pVM->dbgf.s.bmSelectedEvents, paConfigs[i].enmType);
1829 }
1830
1831 return VINF_SUCCESS;
1832}
1833
1834
1835/**
1836 * dbgfR3InterruptConfigEx argument packet.
1837 */
1838typedef struct DBGFR3INTERRUPTCONFIGEXARGS
1839{
1840 PCDBGFINTERRUPTCONFIG paConfigs;
1841 size_t cConfigs;
1842 int rc;
1843} DBGFR3INTERRUPTCONFIGEXARGS;
1844/** Pointer to a dbgfR3InterruptConfigEx argument packet. */
1845typedef DBGFR3INTERRUPTCONFIGEXARGS *PDBGFR3INTERRUPTCONFIGEXARGS;
1846
1847/**
1848 * @callback_method_impl{FNVMMEMTRENDEZVOUS,
1849 * Worker for DBGFR3InterruptConfigEx.}
1850 */
1851static DECLCALLBACK(VBOXSTRICTRC) dbgfR3InterruptConfigEx(PVM pVM, PVMCPU pVCpu, void *pvUser)
1852{
1853 if (pVCpu->idCpu == 0)
1854 {
1855 PDBGFR3INTERRUPTCONFIGEXARGS pArgs = (PDBGFR3INTERRUPTCONFIGEXARGS)pvUser;
1856 PCDBGFINTERRUPTCONFIG paConfigs = pArgs->paConfigs;
1857 size_t cConfigs = pArgs->cConfigs;
1858
1859 /*
1860 * Apply the changes.
1861 */
1862 bool fChanged = false;
1863 bool fThis;
1864 for (uint32_t i = 0; i < cConfigs; i++)
1865 {
1866 /*
1867 * Hardware interrupts.
1868 */
1869 if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_ENABLED)
1870 {
1871 fChanged |= fThis = ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmHardIntBreakpoints, paConfigs[i].iInterrupt) == false;
1872 if (fThis)
1873 {
1874 Assert(pVM->dbgf.s.cHardIntBreakpoints < 256);
1875 pVM->dbgf.s.cHardIntBreakpoints++;
1876 }
1877 }
1878 else if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_DISABLED)
1879 {
1880 fChanged |= fThis = ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmHardIntBreakpoints, paConfigs[i].iInterrupt) == true;
1881 if (fThis)
1882 {
1883 Assert(pVM->dbgf.s.cHardIntBreakpoints > 0);
1884 pVM->dbgf.s.cHardIntBreakpoints--;
1885 }
1886 }
1887
1888 /*
1889 * Software interrupts.
1890 */
1891 if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_ENABLED)
1892 {
1893 fChanged |= fThis = ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSoftIntBreakpoints, paConfigs[i].iInterrupt) == false;
1894 if (fThis)
1895 {
1896 Assert(pVM->dbgf.s.cSoftIntBreakpoints < 256);
1897 pVM->dbgf.s.cSoftIntBreakpoints++;
1898 }
1899 }
1900 else if (paConfigs[i].enmSoftState == DBGFINTERRUPTSTATE_DISABLED)
1901 {
1902 fChanged |= fThis = ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSoftIntBreakpoints, paConfigs[i].iInterrupt) == true;
1903 if (fThis)
1904 {
1905 Assert(pVM->dbgf.s.cSoftIntBreakpoints > 0);
1906 pVM->dbgf.s.cSoftIntBreakpoints--;
1907 }
1908 }
1909 }
1910
1911 /*
1912 * Update the event bitmap entries.
1913 */
1914 if (pVM->dbgf.s.cHardIntBreakpoints > 0)
1915 fChanged |= ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_HARDWARE) == false;
1916 else
1917 fChanged |= ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_HARDWARE) == true;
1918
1919 if (pVM->dbgf.s.cSoftIntBreakpoints > 0)
1920 fChanged |= ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_SOFTWARE) == false;
1921 else
1922 fChanged |= ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_SOFTWARE) == true;
1923
1924 /*
1925 * Inform HM about changes.
1926 */
1927 if (fChanged && HMIsEnabled(pVM))
1928 {
1929 HMR3NotifyDebugEventChanged(pVM);
1930 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1931 }
1932 }
1933 else if (HMIsEnabled(pVM))
1934 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1935
1936 return VINF_SUCCESS;
1937}
1938
1939
1940/**
1941 * Changes
1942 *
1943 * @returns VBox status code.
1944 * @param pUVM The user mode VM handle.
1945 * @param paConfigs The events to query and where to return the state.
1946 * @param cConfigs The number of elements in @a paConfigs.
1947 * @sa DBGFR3InterruptConfigHardware, DBGFR3InterruptConfigSoftware
1948 */
1949VMMR3DECL(int) DBGFR3InterruptConfigEx(PUVM pUVM, PCDBGFINTERRUPTCONFIG paConfigs, size_t cConfigs)
1950{
1951 /*
1952 * Validate input.
1953 */
1954 size_t i = cConfigs;
1955 while (i-- > 0)
1956 {
1957 AssertReturn(paConfigs[i].enmHardState <= DBGFINTERRUPTSTATE_DONT_TOUCH, VERR_INVALID_PARAMETER);
1958 AssertReturn(paConfigs[i].enmSoftState <= DBGFINTERRUPTSTATE_DONT_TOUCH, VERR_INVALID_PARAMETER);
1959 }
1960
1961 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1962 PVM pVM = pUVM->pVM;
1963 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1964
1965 /*
1966 * Apply the changes in EMT(0) and rendezvous with the other CPUs so they
1967 * can sync their data and execution with new debug state.
1968 */
1969 DBGFR3INTERRUPTCONFIGEXARGS Args = { paConfigs, cConfigs, VINF_SUCCESS };
1970 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING | VMMEMTRENDEZVOUS_FLAGS_PRIORITY,
1971 dbgfR3InterruptConfigEx, &Args);
1972 if (RT_SUCCESS(rc))
1973 rc = Args.rc;
1974 return rc;
1975}
1976
1977
1978/**
1979 * Configures interception of a hardware interrupt.
1980 *
1981 * @returns VBox status code.
1982 * @param pUVM The user mode VM handle.
1983 * @param iInterrupt The interrupt number.
1984 * @param fEnabled Whether interception is enabled or not.
1985 * @sa DBGFR3InterruptSoftwareConfig, DBGFR3InterruptConfigEx
1986 */
1987VMMR3DECL(int) DBGFR3InterruptHardwareConfig(PUVM pUVM, uint8_t iInterrupt, bool fEnabled)
1988{
1989 /*
1990 * Convert to DBGFR3InterruptConfigEx call.
1991 */
1992 DBGFINTERRUPTCONFIG IntCfg = { iInterrupt, (uint8_t)fEnabled, DBGFINTERRUPTSTATE_DONT_TOUCH };
1993 return DBGFR3InterruptConfigEx(pUVM, &IntCfg, 1);
1994}
1995
1996
1997/**
1998 * Configures interception of a software interrupt.
1999 *
2000 * @returns VBox status code.
2001 * @param pUVM The user mode VM handle.
2002 * @param iInterrupt The interrupt number.
2003 * @param fEnabled Whether interception is enabled or not.
2004 * @sa DBGFR3InterruptHardwareConfig, DBGFR3InterruptConfigEx
2005 */
2006VMMR3DECL(int) DBGFR3InterruptSoftwareConfig(PUVM pUVM, uint8_t iInterrupt, bool fEnabled)
2007{
2008 /*
2009 * Convert to DBGFR3InterruptConfigEx call.
2010 */
2011 DBGFINTERRUPTCONFIG IntCfg = { iInterrupt, DBGFINTERRUPTSTATE_DONT_TOUCH, (uint8_t)fEnabled };
2012 return DBGFR3InterruptConfigEx(pUVM, &IntCfg, 1);
2013}
2014
2015
2016/**
2017 * Checks whether interception is enabled for a hardware interrupt.
2018 *
2019 * @returns true if enabled, false if not or invalid input.
2020 * @param pUVM The user mode VM handle.
2021 * @param iInterrupt The interrupt number.
2022 * @sa DBGFR3InterruptSoftwareIsEnabled, DBGF_IS_HARDWARE_INT_ENABLED,
2023 * DBGF_IS_SOFTWARE_INT_ENABLED
2024 */
2025VMMR3DECL(int) DBGFR3InterruptHardwareIsEnabled(PUVM pUVM, uint8_t iInterrupt)
2026{
2027 /*
2028 * Validate input.
2029 */
2030 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2031 PVM pVM = pUVM->pVM;
2032 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2033
2034 /*
2035 * Check it.
2036 */
2037 return ASMBitTest(&pVM->dbgf.s.bmHardIntBreakpoints, iInterrupt);
2038}
2039
2040
2041/**
2042 * Checks whether interception is enabled for a software interrupt.
2043 *
2044 * @returns true if enabled, false if not or invalid input.
2045 * @param pUVM The user mode VM handle.
2046 * @param iInterrupt The interrupt number.
2047 * @sa DBGFR3InterruptHardwareIsEnabled, DBGF_IS_SOFTWARE_INT_ENABLED,
2048 * DBGF_IS_HARDWARE_INT_ENABLED,
2049 */
2050VMMR3DECL(int) DBGFR3InterruptSoftwareIsEnabled(PUVM pUVM, uint8_t iInterrupt)
2051{
2052 /*
2053 * Validate input.
2054 */
2055 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2056 PVM pVM = pUVM->pVM;
2057 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2058
2059 /*
2060 * Check it.
2061 */
2062 return ASMBitTest(&pVM->dbgf.s.bmSoftIntBreakpoints, iInterrupt);
2063}
2064
2065
2066
2067/**
2068 * Call this to single step programmatically.
2069 *
2070 * You must pass down the return code to the EM loop! That's
2071 * where the actual single stepping take place (at least in the
2072 * current implementation).
2073 *
2074 * @returns VINF_EM_DBG_STEP
2075 *
2076 * @param pVCpu The cross context virtual CPU structure.
2077 *
2078 * @thread VCpu EMT
2079 * @internal
2080 */
2081VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
2082{
2083 VMCPU_ASSERT_EMT(pVCpu);
2084
2085 pVCpu->dbgf.s.fSingleSteppingRaw = true;
2086 return VINF_EM_DBG_STEP;
2087}
2088
2089
2090/**
2091 * Inject an NMI into a running VM (only VCPU 0!)
2092 *
2093 * @returns VBox status code.
2094 * @param pUVM The user mode VM structure.
2095 * @param idCpu The ID of the CPU to inject the NMI on.
2096 */
2097VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
2098{
2099 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2100 PVM pVM = pUVM->pVM;
2101 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2102 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
2103
2104 /** @todo Implement generic NMI injection. */
2105 if (!HMIsEnabled(pVM))
2106 return VERR_NOT_SUP_IN_RAW_MODE;
2107
2108 VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_INTERRUPT_NMI);
2109 return VINF_SUCCESS;
2110}
2111
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