VirtualBox

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

Last change on this file since 59083 was 59074, checked in by vboxsync, 9 years ago

VMM: DBGFR3EventHandlePending stub.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 50.1 KB
Line 
1/* $Id: DBGF.cpp 59074 2015-12-10 12:50:17Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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* Internal Functions *
97*********************************************************************************************************************************/
98static int dbgfR3VMMWait(PVM pVM);
99static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution);
100static DECLCALLBACK(int) dbgfR3Attach(PVM pVM);
101
102
103/**
104 * Sets the VMM Debug Command variable.
105 *
106 * @returns Previous command.
107 * @param pVM The cross context VM structure.
108 * @param enmCmd The command.
109 */
110DECLINLINE(DBGFCMD) dbgfR3SetCmd(PVM pVM, DBGFCMD enmCmd)
111{
112 DBGFCMD rc;
113 if (enmCmd == DBGFCMD_NO_COMMAND)
114 {
115 Log2(("DBGF: Setting command to %d (DBGFCMD_NO_COMMAND)\n", enmCmd));
116 rc = (DBGFCMD)ASMAtomicXchgU32((uint32_t volatile *)(void *)&pVM->dbgf.s.enmVMMCmd, enmCmd);
117 VM_FF_CLEAR(pVM, VM_FF_DBGF);
118 }
119 else
120 {
121 Log2(("DBGF: Setting command to %d\n", enmCmd));
122 AssertMsg(pVM->dbgf.s.enmVMMCmd == DBGFCMD_NO_COMMAND, ("enmCmd=%d enmVMMCmd=%d\n", enmCmd, pVM->dbgf.s.enmVMMCmd));
123 rc = (DBGFCMD)ASMAtomicXchgU32((uint32_t volatile *)(void *)&pVM->dbgf.s.enmVMMCmd, enmCmd);
124 VM_FF_SET(pVM, VM_FF_DBGF);
125 VMR3NotifyGlobalFFU(pVM->pUVM, 0 /* didn't notify REM */);
126 }
127 return rc;
128}
129
130
131/**
132 * Initializes the DBGF.
133 *
134 * @returns VBox status code.
135 * @param pVM The cross context VM structure.
136 */
137VMMR3_INT_DECL(int) DBGFR3Init(PVM pVM)
138{
139 PUVM pUVM = pVM->pUVM;
140 AssertCompile(sizeof(pUVM->dbgf.s) <= sizeof(pUVM->dbgf.padding));
141 AssertCompile(sizeof(pUVM->aCpus[0].dbgf.s) <= sizeof(pUVM->aCpus[0].dbgf.padding));
142
143 /*
144 * The usual sideways mountain climbing style of init:
145 */
146 int rc = dbgfR3InfoInit(pUVM); /* (First, initalizes the shared critical section.) */
147 if (RT_SUCCESS(rc))
148 {
149 rc = dbgfR3TraceInit(pVM);
150 if (RT_SUCCESS(rc))
151 {
152 rc = dbgfR3RegInit(pUVM);
153 if (RT_SUCCESS(rc))
154 {
155 rc = dbgfR3AsInit(pUVM);
156 if (RT_SUCCESS(rc))
157 {
158 rc = dbgfR3BpInit(pVM);
159 if (RT_SUCCESS(rc))
160 {
161 rc = dbgfR3OSInit(pUVM);
162 if (RT_SUCCESS(rc))
163 {
164 rc = dbgfR3PlugInInit(pUVM);
165 if (RT_SUCCESS(rc))
166 {
167 return VINF_SUCCESS;
168 }
169 dbgfR3OSTerm(pUVM);
170 }
171 }
172 dbgfR3AsTerm(pUVM);
173 }
174 dbgfR3RegTerm(pUVM);
175 }
176 dbgfR3TraceTerm(pVM);
177 }
178 dbgfR3InfoTerm(pUVM);
179 }
180 return rc;
181}
182
183
184/**
185 * Terminates and cleans up resources allocated by the DBGF.
186 *
187 * @returns VBox status code.
188 * @param pVM The cross context VM structure.
189 */
190VMMR3_INT_DECL(int) DBGFR3Term(PVM pVM)
191{
192 PUVM pUVM = pVM->pUVM;
193
194 dbgfR3PlugInTerm(pUVM);
195 dbgfR3OSTerm(pUVM);
196 dbgfR3AsTerm(pUVM);
197 dbgfR3RegTerm(pUVM);
198 dbgfR3TraceTerm(pVM);
199 dbgfR3InfoTerm(pUVM);
200
201 return VINF_SUCCESS;
202}
203
204
205/**
206 * Called when the VM is powered off to detach debuggers.
207 *
208 * @param pVM The cross context VM structure.
209 */
210VMMR3_INT_DECL(void) DBGFR3PowerOff(PVM pVM)
211{
212
213 /*
214 * Send a termination event to any attached debugger.
215 */
216 /* wait to become the speaker (we should already be that). */
217 if ( pVM->dbgf.s.fAttached
218 && RTSemPingShouldWait(&pVM->dbgf.s.PingPong))
219 RTSemPingWait(&pVM->dbgf.s.PingPong, 5000);
220
221 if (pVM->dbgf.s.fAttached)
222 {
223 /* Just mark it as detached if we're not in a position to send a power
224 off event. It should fail later on. */
225 if (!RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
226 {
227 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
228 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
229 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
230 }
231
232 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
233 {
234 /* Try send the power off event. */
235 int rc;
236 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
237 if (enmCmd == DBGFCMD_DETACH_DEBUGGER)
238 /* the debugger beat us to initiating the detaching. */
239 rc = VINF_SUCCESS;
240 else
241 {
242 /* ignore the command (if any). */
243 enmCmd = DBGFCMD_NO_COMMAND;
244 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_POWERING_OFF;
245 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
246 rc = RTSemPing(&pVM->dbgf.s.PingPong);
247 }
248
249 /*
250 * Process commands and priority requests until we get a command
251 * indicating that the debugger has detached.
252 */
253 uint32_t cPollHack = 1;
254 PVMCPU pVCpu = VMMGetCpu(pVM);
255 while (RT_SUCCESS(rc))
256 {
257 if (enmCmd != DBGFCMD_NO_COMMAND)
258 {
259 /* process command */
260 bool fResumeExecution;
261 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
262 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
263 if (enmCmd == DBGFCMD_DETACHED_DEBUGGER)
264 break;
265 enmCmd = DBGFCMD_NO_COMMAND;
266 }
267 else
268 {
269 /* Wait for new command, processing pending priority requests
270 first. The request processing is a bit crazy, but
271 unfortunately required by plugin unloading. */
272 if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
273 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
274 {
275 LogFlow(("DBGFR3PowerOff: Processes priority requests...\n"));
276 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
277 if (rc == VINF_SUCCESS)
278 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, true /*fPriorityOnly*/);
279 LogFlow(("DBGFR3PowerOff: VMR3ReqProcess -> %Rrc\n", rc));
280 cPollHack = 1;
281 }
282 else if (cPollHack < 120)
283 cPollHack++;
284
285 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
286 if (RT_SUCCESS(rc))
287 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
288 else if (rc == VERR_TIMEOUT)
289 rc = VINF_SUCCESS;
290 }
291 }
292
293 /*
294 * Clear the FF so we won't get confused later on.
295 */
296 VM_FF_CLEAR(pVM, VM_FF_DBGF);
297 }
298 }
299}
300
301
302/**
303 * Applies relocations to data and code managed by this
304 * component. This function will be called at init and
305 * whenever the VMM need to relocate it self inside the GC.
306 *
307 * @param pVM The cross context VM structure.
308 * @param offDelta Relocation delta relative to old location.
309 */
310VMMR3_INT_DECL(void) DBGFR3Relocate(PVM pVM, RTGCINTPTR offDelta)
311{
312 dbgfR3TraceRelocate(pVM);
313 dbgfR3AsRelocate(pVM->pUVM, offDelta);
314}
315
316
317/**
318 * Waits a little while for a debuggger to attach.
319 *
320 * @returns True is a debugger have attached.
321 * @param pVM The cross context VM structure.
322 * @param enmEvent Event.
323 */
324bool dbgfR3WaitForAttach(PVM pVM, DBGFEVENTTYPE enmEvent)
325{
326 /*
327 * First a message.
328 */
329#ifndef RT_OS_L4
330
331# if !defined(DEBUG) || defined(DEBUG_sandervl) || defined(DEBUG_frank) || defined(IEM_VERIFICATION_MODE)
332 int cWait = 10;
333# else
334 int cWait = HMIsEnabled(pVM)
335 && ( enmEvent == DBGFEVENT_ASSERTION_HYPER
336 || enmEvent == DBGFEVENT_FATAL_ERROR)
337 && !RTEnvExist("VBOX_DBGF_WAIT_FOR_ATTACH")
338 ? 10
339 : 150;
340# endif
341 RTStrmPrintf(g_pStdErr, "DBGF: No debugger attached, waiting %d second%s for one to attach (event=%d)\n",
342 cWait / 10, cWait != 10 ? "s" : "", enmEvent);
343 RTStrmFlush(g_pStdErr);
344 while (cWait > 0)
345 {
346 RTThreadSleep(100);
347 if (pVM->dbgf.s.fAttached)
348 {
349 RTStrmPrintf(g_pStdErr, "Attached!\n");
350 RTStrmFlush(g_pStdErr);
351 return true;
352 }
353
354 /* next */
355 if (!(cWait % 10))
356 {
357 RTStrmPrintf(g_pStdErr, "%d.", cWait / 10);
358 RTStrmFlush(g_pStdErr);
359 }
360 cWait--;
361 }
362#endif
363
364 RTStrmPrintf(g_pStdErr, "Stopping the VM!\n");
365 RTStrmFlush(g_pStdErr);
366 return false;
367}
368
369
370/**
371 * Forced action callback.
372 * The VMM will call this from it's main loop when VM_FF_DBGF is set.
373 *
374 * The function checks and executes pending commands from the debugger.
375 *
376 * @returns VINF_SUCCESS normally.
377 * @returns VERR_DBGF_RAISE_FATAL_ERROR to pretend a fatal error happened.
378 * @param pVM The cross context VM structure.
379 */
380VMMR3_INT_DECL(int) DBGFR3VMMForcedAction(PVM pVM)
381{
382 int rc = VINF_SUCCESS;
383
384 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_DBGF))
385 {
386 PVMCPU pVCpu = VMMGetCpu(pVM);
387
388 /*
389 * Command pending? Process it.
390 */
391 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
392 {
393 bool fResumeExecution;
394 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
395 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
396 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
397 if (!fResumeExecution)
398 rc = dbgfR3VMMWait(pVM);
399 }
400 }
401 return rc;
402}
403
404
405/**
406 * Flag whether the event implies that we're stopped in the hypervisor code
407 * and have to block certain operations.
408 *
409 * @param pVM The cross context VM structure.
410 * @param enmEvent The event.
411 */
412static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
413{
414 switch (enmEvent)
415 {
416 case DBGFEVENT_STEPPED_HYPER:
417 case DBGFEVENT_ASSERTION_HYPER:
418 case DBGFEVENT_BREAKPOINT_HYPER:
419 pVM->dbgf.s.fStoppedInHyper = true;
420 break;
421 default:
422 pVM->dbgf.s.fStoppedInHyper = false;
423 break;
424 }
425}
426
427
428/**
429 * Try to determine the event context.
430 *
431 * @returns debug event context.
432 * @param pVM The cross context VM structure.
433 */
434static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
435{
436 /** @todo SMP support! */
437 PVMCPU pVCpu = &pVM->aCpus[0];
438
439 switch (EMGetState(pVCpu))
440 {
441 case EMSTATE_RAW:
442 case EMSTATE_DEBUG_GUEST_RAW:
443 return DBGFEVENTCTX_RAW;
444
445 case EMSTATE_REM:
446 case EMSTATE_DEBUG_GUEST_REM:
447 return DBGFEVENTCTX_REM;
448
449 case EMSTATE_DEBUG_HYPER:
450 case EMSTATE_GURU_MEDITATION:
451 return DBGFEVENTCTX_HYPER;
452
453 default:
454 return DBGFEVENTCTX_OTHER;
455 }
456}
457
458/**
459 * The common event prologue code.
460 * It will set the 'stopped-in-hyper' flag, make sure someone is attached,
461 * and perhaps process any high priority pending actions (none yet).
462 *
463 * @returns VBox status code.
464 * @param pVM The cross context VM structure.
465 * @param enmEvent The event to be sent.
466 */
467static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
468{
469 /** @todo SMP */
470 PVMCPU pVCpu = VMMGetCpu(pVM);
471
472 /*
473 * Check if a debugger is attached.
474 */
475 if ( !pVM->dbgf.s.fAttached
476 && !dbgfR3WaitForAttach(pVM, enmEvent))
477 {
478 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
479 return VERR_DBGF_NOT_ATTACHED;
480 }
481
482 /*
483 * Sync back the state from the REM.
484 */
485 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
486#ifdef VBOX_WITH_REM
487 if (!pVM->dbgf.s.fStoppedInHyper)
488 REMR3StateUpdate(pVM, pVCpu);
489#endif
490
491 /*
492 * Look thru pending commands and finish those which make sense now.
493 */
494 /** @todo Process/purge pending commands. */
495 //int rc = DBGFR3VMMForcedAction(pVM);
496 return VINF_SUCCESS;
497}
498
499
500/**
501 * Sends the event in the event buffer.
502 *
503 * @returns VBox status code.
504 * @param pVM The cross context VM structure.
505 */
506static int dbgfR3SendEvent(PVM pVM)
507{
508 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
509 if (RT_SUCCESS(rc))
510 rc = dbgfR3VMMWait(pVM);
511
512 pVM->dbgf.s.fStoppedInHyper = false;
513 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
514 return rc;
515}
516
517
518VMMR3_INT_DECL(VBOXSTRICTRC) DBGFR3EventHandlePending(PVM pVM, PVMCPU pVCpu)
519{
520 return VINF_SUCCESS;
521}
522
523
524/**
525 * Send a generic debugger event which takes no data.
526 *
527 * @returns VBox status code.
528 * @param pVM The cross context VM structure.
529 * @param enmEvent The event to send.
530 * @internal
531 */
532VMMR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
533{
534 int rc = dbgfR3EventPrologue(pVM, enmEvent);
535 if (RT_FAILURE(rc))
536 return rc;
537
538 /*
539 * Send the event and process the reply communication.
540 */
541 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
542 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
543 return dbgfR3SendEvent(pVM);
544}
545
546
547/**
548 * Send a debugger event which takes the full source file location.
549 *
550 * @returns VBox status code.
551 * @param pVM The cross context VM structure.
552 * @param enmEvent The event to send.
553 * @param pszFile Source file.
554 * @param uLine Line number in source file.
555 * @param pszFunction Function name.
556 * @param pszFormat Message which accompanies the event.
557 * @param ... Message arguments.
558 * @internal
559 */
560VMMR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
561{
562 va_list args;
563 va_start(args, pszFormat);
564 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
565 va_end(args);
566 return rc;
567}
568
569
570/**
571 * Send a debugger event which takes the full source file location.
572 *
573 * @returns VBox status code.
574 * @param pVM The cross context VM structure.
575 * @param enmEvent The event to send.
576 * @param pszFile Source file.
577 * @param uLine Line number in source file.
578 * @param pszFunction Function name.
579 * @param pszFormat Message which accompanies the event.
580 * @param args Message arguments.
581 * @internal
582 */
583VMMR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
584{
585 int rc = dbgfR3EventPrologue(pVM, enmEvent);
586 if (RT_FAILURE(rc))
587 return rc;
588
589 /*
590 * Format the message.
591 */
592 char *pszMessage = NULL;
593 char szMessage[8192];
594 if (pszFormat && *pszFormat)
595 {
596 pszMessage = &szMessage[0];
597 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
598 }
599
600 /*
601 * Send the event and process the reply communication.
602 */
603 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
604 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
605 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
606 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
607 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
608 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
609 return dbgfR3SendEvent(pVM);
610}
611
612
613/**
614 * Send a debugger event which takes the two assertion messages.
615 *
616 * @returns VBox status code.
617 * @param pVM The cross context VM structure.
618 * @param enmEvent The event to send.
619 * @param pszMsg1 First assertion message.
620 * @param pszMsg2 Second assertion message.
621 */
622VMMR3_INT_DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
623{
624 int rc = dbgfR3EventPrologue(pVM, enmEvent);
625 if (RT_FAILURE(rc))
626 return rc;
627
628 /*
629 * Send the event and process the reply communication.
630 */
631 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
632 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
633 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg1 = pszMsg1;
634 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
635 return dbgfR3SendEvent(pVM);
636}
637
638
639/**
640 * Breakpoint was hit somewhere.
641 * Figure out which breakpoint it is and notify the debugger.
642 *
643 * @returns VBox status code.
644 * @param pVM The cross context VM structure.
645 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
646 */
647VMMR3_INT_DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
648{
649 int rc = dbgfR3EventPrologue(pVM, enmEvent);
650 if (RT_FAILURE(rc))
651 return rc;
652
653 /*
654 * Send the event and process the reply communication.
655 */
656 /** @todo SMP */
657 PVMCPU pVCpu = VMMGetCpu0(pVM);
658
659 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
660 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVCpu->dbgf.s.iActiveBp;
661 pVCpu->dbgf.s.iActiveBp = ~0U;
662 if (iBp != ~0U)
663 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
664 else
665 {
666 /* REM breakpoints has be been searched for. */
667#if 0 /** @todo get flat PC api! */
668 uint32_t eip = CPUMGetGuestEIP(pVM);
669#else
670 /* @todo SMP support!! */
671 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(VMMGetCpu(pVM));
672 RTGCPTR eip = pCtx->rip + pCtx->cs.u64Base;
673#endif
674 for (size_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.aBreakpoints); i++)
675 if ( pVM->dbgf.s.aBreakpoints[i].enmType == DBGFBPTYPE_REM
676 && pVM->dbgf.s.aBreakpoints[i].u.Rem.GCPtr == eip)
677 {
678 pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVM->dbgf.s.aBreakpoints[i].iBp;
679 break;
680 }
681 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
682 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
683 }
684 return dbgfR3SendEvent(pVM);
685}
686
687
688/**
689 * Waits for the debugger to respond.
690 *
691 * @returns VBox status code. (clearify)
692 * @param pVM The cross context VM structure.
693 */
694static int dbgfR3VMMWait(PVM pVM)
695{
696 PVMCPU pVCpu = VMMGetCpu(pVM);
697
698 LogFlow(("dbgfR3VMMWait:\n"));
699 int rcRet = VINF_SUCCESS;
700
701 /*
702 * Waits for the debugger to reply (i.e. issue an command).
703 */
704 for (;;)
705 {
706 /*
707 * Wait.
708 */
709 uint32_t cPollHack = 1; /** @todo this interface is horrible now that we're using lots of VMR3ReqCall stuff all over DBGF. */
710 for (;;)
711 {
712 int rc;
713 if ( !VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_REQUEST)
714 && !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
715 {
716 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
717 if (RT_SUCCESS(rc))
718 break;
719 if (rc != VERR_TIMEOUT)
720 {
721 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
722 return rc;
723 }
724 }
725
726 if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
727 {
728 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
729 cPollHack = 1;
730 }
731 else if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
732 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
733 {
734 LogFlow(("dbgfR3VMMWait: Processes requests...\n"));
735 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
736 if (rc == VINF_SUCCESS)
737 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, false /*fPriorityOnly*/);
738 LogFlow(("dbgfR3VMMWait: VMR3ReqProcess -> %Rrc rcRet=%Rrc\n", rc, rcRet));
739 cPollHack = 1;
740 }
741 else
742 {
743 rc = VINF_SUCCESS;
744 if (cPollHack < 120)
745 cPollHack++;
746 }
747
748 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
749 {
750 switch (rc)
751 {
752 case VINF_EM_DBG_BREAKPOINT:
753 case VINF_EM_DBG_STEPPED:
754 case VINF_EM_DBG_STEP:
755 case VINF_EM_DBG_STOP:
756 case VINF_EM_DBG_EVENT:
757 AssertMsgFailed(("rc=%Rrc\n", rc));
758 break;
759
760 /* return straight away */
761 case VINF_EM_TERMINATE:
762 case VINF_EM_OFF:
763 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
764 return rc;
765
766 /* remember return code. */
767 default:
768 AssertReleaseMsgFailed(("rc=%Rrc is not in the switch!\n", rc));
769 case VINF_EM_RESET:
770 case VINF_EM_SUSPEND:
771 case VINF_EM_HALT:
772 case VINF_EM_RESUME:
773 case VINF_EM_RESCHEDULE:
774 case VINF_EM_RESCHEDULE_REM:
775 case VINF_EM_RESCHEDULE_RAW:
776 if (rc < rcRet || rcRet == VINF_SUCCESS)
777 rcRet = rc;
778 break;
779 }
780 }
781 else if (RT_FAILURE(rc))
782 {
783 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
784 return rc;
785 }
786 }
787
788 /*
789 * Process the command.
790 */
791 bool fResumeExecution;
792 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
793 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
794 int rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
795 if (fResumeExecution)
796 {
797 if (RT_FAILURE(rc))
798 rcRet = rc;
799 else if ( rc >= VINF_EM_FIRST
800 && rc <= VINF_EM_LAST
801 && (rc < rcRet || rcRet == VINF_SUCCESS))
802 rcRet = rc;
803 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rcRet));
804 return rcRet;
805 }
806 }
807}
808
809
810/**
811 * Executes command from debugger.
812 * The caller is responsible for waiting or resuming execution based on the
813 * value returned in the *pfResumeExecution indicator.
814 *
815 * @returns VBox status code. (clearify!)
816 * @param pVM The cross context VM structure.
817 * @param enmCmd The command in question.
818 * @param pCmdData Pointer to the command data.
819 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
820 */
821static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
822{
823 bool fSendEvent;
824 bool fResume;
825 int rc = VINF_SUCCESS;
826
827 NOREF(pCmdData); /* for later */
828
829 switch (enmCmd)
830 {
831 /*
832 * Halt is answered by an event say that we've halted.
833 */
834 case DBGFCMD_HALT:
835 {
836 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
837 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
838 fSendEvent = true;
839 fResume = false;
840 break;
841 }
842
843
844 /*
845 * Resume is not answered we'll just resume execution.
846 */
847 case DBGFCMD_GO:
848 {
849 /** @todo SMP */
850 PVMCPU pVCpu = VMMGetCpu0(pVM);
851 pVCpu->dbgf.s.fSingleSteppingRaw = false;
852 fSendEvent = false;
853 fResume = true;
854 break;
855 }
856
857 /** @todo implement (and define) the rest of the commands. */
858
859 /*
860 * Disable breakpoints and stuff.
861 * Send an everythings cool event to the debugger thread and resume execution.
862 */
863 case DBGFCMD_DETACH_DEBUGGER:
864 {
865 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
866 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
867 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
868 fSendEvent = true;
869 fResume = true;
870 break;
871 }
872
873 /*
874 * The debugger has detached successfully.
875 * There is no reply to this event.
876 */
877 case DBGFCMD_DETACHED_DEBUGGER:
878 {
879 fSendEvent = false;
880 fResume = true;
881 break;
882 }
883
884 /*
885 * Single step, with trace into.
886 */
887 case DBGFCMD_SINGLE_STEP:
888 {
889 Log2(("Single step\n"));
890 rc = VINF_EM_DBG_STEP;
891 /** @todo SMP */
892 PVMCPU pVCpu = VMMGetCpu0(pVM);
893 pVCpu->dbgf.s.fSingleSteppingRaw = true;
894 fSendEvent = false;
895 fResume = true;
896 break;
897 }
898
899 /*
900 * Default is to send an invalid command event.
901 */
902 default:
903 {
904 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
905 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
906 fSendEvent = true;
907 fResume = false;
908 break;
909 }
910 }
911
912 /*
913 * Send pending event.
914 */
915 if (fSendEvent)
916 {
917 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
918 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
919 if (RT_FAILURE(rc2))
920 {
921 AssertRC(rc2);
922 *pfResumeExecution = true;
923 return rc2;
924 }
925 }
926
927 /*
928 * Return.
929 */
930 *pfResumeExecution = fResume;
931 return rc;
932}
933
934
935/**
936 * Attaches a debugger to the specified VM.
937 *
938 * Only one debugger at a time.
939 *
940 * @returns VBox status code.
941 * @param pUVM The user mode VM handle.
942 */
943VMMR3DECL(int) DBGFR3Attach(PUVM pUVM)
944{
945 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
946 PVM pVM = pUVM->pVM;
947 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
948
949 /*
950 * Call the VM, use EMT for serialization.
951 */
952 /** @todo SMP */
953 return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)dbgfR3Attach, 1, pVM);
954}
955
956
957/**
958 * EMT worker for DBGFR3Attach.
959 *
960 * @returns VBox status code.
961 * @param pVM The cross context VM structure.
962 */
963static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
964{
965 if (pVM->dbgf.s.fAttached)
966 {
967 Log(("dbgR3Attach: Debugger already attached\n"));
968 return VERR_DBGF_ALREADY_ATTACHED;
969 }
970
971 /*
972 * Create the Ping-Pong structure.
973 */
974 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
975 AssertRCReturn(rc, rc);
976
977 /*
978 * Set the attached flag.
979 */
980 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
981 return VINF_SUCCESS;
982}
983
984
985/**
986 * Detaches a debugger from the specified VM.
987 *
988 * Caller must be attached to the VM.
989 *
990 * @returns VBox status code.
991 * @param pUVM The user mode VM handle.
992 */
993VMMR3DECL(int) DBGFR3Detach(PUVM pUVM)
994{
995 LogFlow(("DBGFR3Detach:\n"));
996 int rc;
997
998 /*
999 * Validate input. The UVM handle shall be valid, the VM handle might be
1000 * in the processes of being destroyed already, so deal quietly with that.
1001 */
1002 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1003 PVM pVM = pUVM->pVM;
1004 if (!VM_IS_VALID_EXT(pVM))
1005 return VERR_INVALID_VM_HANDLE;
1006
1007 /*
1008 * Check if attached.
1009 */
1010 if (!pVM->dbgf.s.fAttached)
1011 return VERR_DBGF_NOT_ATTACHED;
1012
1013 /*
1014 * Try send the detach command.
1015 * Keep in mind that we might be racing EMT, so, be extra careful.
1016 */
1017 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
1018 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
1019 {
1020 rc = RTSemPong(&pVM->dbgf.s.PingPong);
1021 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
1022 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
1023 }
1024
1025 /*
1026 * Wait for the OK event.
1027 */
1028 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
1029 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
1030
1031 /*
1032 * Send the notification command indicating that we're really done.
1033 */
1034 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
1035 rc = RTSemPong(&pVM->dbgf.s.PingPong);
1036 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
1037
1038 LogFlowFunc(("returns VINF_SUCCESS\n"));
1039 return VINF_SUCCESS;
1040}
1041
1042
1043/**
1044 * Wait for a debug event.
1045 *
1046 * @returns VBox status code. Will not return VBOX_INTERRUPTED.
1047 * @param pUVM The user mode VM handle.
1048 * @param cMillies Number of millis to wait.
1049 * @param ppEvent Where to store the event pointer.
1050 */
1051VMMR3DECL(int) DBGFR3EventWait(PUVM pUVM, RTMSINTERVAL cMillies, PCDBGFEVENT *ppEvent)
1052{
1053 /*
1054 * Check state.
1055 */
1056 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1057 PVM pVM = pUVM->pVM;
1058 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1059 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1060 *ppEvent = NULL;
1061
1062 /*
1063 * Wait.
1064 */
1065 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
1066 if (RT_SUCCESS(rc))
1067 {
1068 *ppEvent = &pVM->dbgf.s.DbgEvent;
1069 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
1070 return VINF_SUCCESS;
1071 }
1072
1073 return rc;
1074}
1075
1076
1077/**
1078 * Halts VM execution.
1079 *
1080 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
1081 * arrives. Until that time it's not possible to issue any new commands.
1082 *
1083 * @returns VBox status code.
1084 * @param pUVM The user mode VM handle.
1085 */
1086VMMR3DECL(int) DBGFR3Halt(PUVM pUVM)
1087{
1088 /*
1089 * Check state.
1090 */
1091 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1092 PVM pVM = pUVM->pVM;
1093 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1094 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1095 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1096 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
1097 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
1098 return VWRN_DBGF_ALREADY_HALTED;
1099
1100 /*
1101 * Send command.
1102 */
1103 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
1104
1105 return VINF_SUCCESS;
1106}
1107
1108
1109/**
1110 * Checks if the VM is halted by the debugger.
1111 *
1112 * @returns True if halted.
1113 * @returns False if not halted.
1114 * @param pUVM The user mode VM handle.
1115 */
1116VMMR3DECL(bool) DBGFR3IsHalted(PUVM pUVM)
1117{
1118 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1119 PVM pVM = pUVM->pVM;
1120 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1121 AssertReturn(pVM->dbgf.s.fAttached, false);
1122
1123 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1124 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1125 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1126}
1127
1128
1129/**
1130 * Checks if the debugger can wait for events or not.
1131 *
1132 * This function is only used by lazy, multiplexing debuggers. :-)
1133 *
1134 * @returns VBox status code.
1135 * @retval VINF_SUCCESS if waitable.
1136 * @retval VERR_SEM_OUT_OF_TURN if not waitable.
1137 * @retval VERR_INVALID_VM_HANDLE if the VM is being (/ has been) destroyed
1138 * (not asserted) or if the handle is invalid (asserted).
1139 * @retval VERR_DBGF_NOT_ATTACHED if not attached.
1140 *
1141 * @param pUVM The user mode VM handle.
1142 */
1143VMMR3DECL(int) DBGFR3QueryWaitable(PUVM pUVM)
1144{
1145 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1146
1147 /* Note! There is a slight race here, unfortunately. */
1148 PVM pVM = pUVM->pVM;
1149 if (!RT_VALID_PTR(pVM))
1150 return VERR_INVALID_VM_HANDLE;
1151 if (pVM->enmVMState >= VMSTATE_DESTROYING)
1152 return VERR_INVALID_VM_HANDLE;
1153 if (!pVM->dbgf.s.fAttached)
1154 return VERR_DBGF_NOT_ATTACHED;
1155
1156 if (!RTSemPongShouldWait(&pVM->dbgf.s.PingPong))
1157 return VERR_SEM_OUT_OF_TURN;
1158
1159 return VINF_SUCCESS;
1160}
1161
1162
1163/**
1164 * Resumes VM execution.
1165 *
1166 * There is no receipt event on this command.
1167 *
1168 * @returns VBox status code.
1169 * @param pUVM The user mode VM handle.
1170 */
1171VMMR3DECL(int) DBGFR3Resume(PUVM pUVM)
1172{
1173 /*
1174 * Check state.
1175 */
1176 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1177 PVM pVM = pUVM->pVM;
1178 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1179 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1180 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1181
1182 /*
1183 * Send the ping back to the emulation thread telling it to run.
1184 */
1185 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1186 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1187 AssertRC(rc);
1188
1189 return rc;
1190}
1191
1192
1193/**
1194 * Step Into.
1195 *
1196 * A single step event is generated from this command.
1197 * The current implementation is not reliable, so don't rely on the event coming.
1198 *
1199 * @returns VBox status code.
1200 * @param pUVM The user mode VM handle.
1201 * @param idCpu The ID of the CPU to single step on.
1202 */
1203VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
1204{
1205 /*
1206 * Check state.
1207 */
1208 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1209 PVM pVM = pUVM->pVM;
1210 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1211 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1212 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1213 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1214
1215 /*
1216 * Send the ping back to the emulation thread telling it to run.
1217 */
1218/** @todo SMP (idCpu) */
1219 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1220 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1221 AssertRC(rc);
1222 return rc;
1223}
1224
1225
1226/**
1227 * @callback_method_impl{FNVMMEMTRENDEZVOUS}
1228 */
1229static DECLCALLBACK(VBOXSTRICTRC) dbgfR3EventConfigNotifyAllCpus(PVM pVM, PVMCPU pVCpu, void *pvUser)
1230{
1231 if (pvUser /*fIsHmEnabled*/)
1232 HMR3NotifyDebugEventChangedPerCpu(pVM, pVCpu);
1233 return VINF_SUCCESS;
1234}
1235
1236
1237/**
1238 * Worker for DBGFR3EventConfigEx.
1239 *
1240 * @returns VBox status code. Will not return VBOX_INTERRUPTED.
1241 * @param pUVM The user mode VM handle.
1242 * @param paConfigs The event to configure and their new state.
1243 * @param cConfigs Number of entries in @a paConfigs.
1244 */
1245static DECLCALLBACK(int) dbgfR3EventConfigEx(PUVM pUVM, DBGFEVENTCONFIG volatile const *paConfigs, size_t cConfigs)
1246{
1247 PVM pVM = pUVM->pVM;
1248 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1249
1250 /*
1251 * Apply the changes.
1252 */
1253 unsigned cChanges = 0;
1254 for (uint32_t i = 0; i < cConfigs; i++)
1255 {
1256 DBGFEVENTTYPE enmType = paConfigs[i].enmType;
1257 AssertReturn(enmType >= DBGFEVENT_FIRST_SELECTABLE && enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1258 if (paConfigs[i].fEnabled)
1259 cChanges += ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, enmType) == false;
1260 else
1261 cChanges += ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, enmType) == true;
1262 }
1263
1264 /*
1265 * Inform HM about changes. In an SMP setup, interrupt execution on the
1266 * other CPUs so their execution loop can be reselected.
1267 */
1268 int rc = VINF_SUCCESS;
1269 if (cChanges > 0)
1270 {
1271 bool const fIsHmEnabled = HMIsEnabled(pVM);
1272 if (fIsHmEnabled)
1273 HMR3NotifyDebugEventChanged(pVM);
1274 if (pVM->cCpus > 1 || fIsHmEnabled)
1275 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE, dbgfR3EventConfigNotifyAllCpus,
1276 (void *)(uintptr_t)fIsHmEnabled);
1277 }
1278 return rc;
1279}
1280
1281
1282/**
1283 * Configures (enables/disables) multiple selectable debug events.
1284 *
1285 * @returns VBox status code.
1286 * @param pUVM The user mode VM handle.
1287 * @param paConfigs The event to configure and their new state.
1288 * @param cConfigs Number of entries in @a paConfigs.
1289 */
1290VMMR3DECL(int) DBGFR3EventConfigEx(PUVM pUVM, PCDBGFEVENTCONFIG paConfigs, size_t cConfigs)
1291{
1292 /*
1293 * Validate input.
1294 */
1295 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1296 size_t i = cConfigs;
1297 while (i-- > 0)
1298 {
1299 AssertReturn(paConfigs[i].enmType >= DBGFEVENT_FIRST_SELECTABLE, VERR_INVALID_PARAMETER);
1300 AssertReturn(paConfigs[i].enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1301 }
1302
1303 /*
1304 * Apply the changes in EMT(0).
1305 */
1306 return VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)dbgfR3EventConfigEx, 3, pUVM, paConfigs, cConfigs);
1307}
1308
1309
1310/**
1311 * Enables or disables a selectable debug event.
1312 *
1313 * @returns VBox status code.
1314 * @param pUVM The user mode VM handle.
1315 * @param enmEvent The selectable debug event.
1316 * @param fEnabled The new state.
1317 */
1318VMMR3DECL(int) DBGFR3EventConfig(PUVM pUVM, DBGFEVENTTYPE enmEvent, bool fEnabled)
1319{
1320 /*
1321 * Convert to an array call.
1322 */
1323 DBGFEVENTCONFIG EvtCfg = { enmEvent, fEnabled };
1324 return DBGFR3EventConfigEx(pUVM, &EvtCfg, 1);
1325}
1326
1327
1328/**
1329 * Checks if the given selectable event is enabled.
1330 *
1331 * @returns true if enabled, false if not or invalid input.
1332 * @param pUVM The user mode VM handle.
1333 * @param enmEvent The selectable debug event.
1334 * @sa DBGFR3EventQuery
1335 */
1336VMMR3DECL(bool) DBGFR3EventIsEnabled(PUVM pUVM, DBGFEVENTTYPE enmEvent)
1337{
1338 /*
1339 * Validate input.
1340 */
1341 AssertReturn( enmEvent >= DBGFEVENT_HALT_DONE
1342 && enmEvent < DBGFEVENT_END, false);
1343 Assert( enmEvent >= DBGFEVENT_FIRST_SELECTABLE
1344 || enmEvent == DBGFEVENT_BREAKPOINT
1345 || enmEvent == DBGFEVENT_BREAKPOINT_IO
1346 || enmEvent == DBGFEVENT_BREAKPOINT_MMIO);
1347
1348 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1349 PVM pVM = pUVM->pVM;
1350 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1351
1352 /*
1353 * Check the event status.
1354 */
1355 return ASMBitTest(&pVM->dbgf.s.bmSelectedEvents, enmEvent);
1356}
1357
1358
1359/**
1360 * Queries the status of a set of events.
1361 *
1362 * @returns VBox status code.
1363 * @param pUVM The user mode VM handle.
1364 * @param paConfigs The events to query and where to return the state.
1365 * @param cConfigs The number of elements in @a paConfigs.
1366 * @sa DBGFR3EventIsEnabled, DBGF_IS_EVENT_ENABLED
1367 */
1368VMMR3DECL(int) DBGFR3EventQuery(PUVM pUVM, PDBGFEVENTCONFIG paConfigs, size_t cConfigs)
1369{
1370 /*
1371 * Validate input.
1372 */
1373 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1374 PVM pVM = pUVM->pVM;
1375 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1376
1377 for (size_t i = 0; i < cConfigs; i++)
1378 {
1379 DBGFEVENTTYPE enmType = paConfigs[i].enmType;
1380 AssertReturn( enmType >= DBGFEVENT_HALT_DONE
1381 && enmType < DBGFEVENT_END, VERR_INVALID_PARAMETER);
1382 Assert( enmType >= DBGFEVENT_FIRST_SELECTABLE
1383 || enmType == DBGFEVENT_BREAKPOINT
1384 || enmType == DBGFEVENT_BREAKPOINT_IO
1385 || enmType == DBGFEVENT_BREAKPOINT_MMIO);
1386 paConfigs[i].fEnabled = ASMBitTest(&pVM->dbgf.s.bmSelectedEvents, paConfigs[i].enmType);
1387 }
1388
1389 return VINF_SUCCESS;
1390}
1391
1392
1393/**
1394 * Worker for DBGFR3InterruptConfigEx.
1395 *
1396 * @returns VBox status code. Will not return VBOX_INTERRUPTED.
1397 * @param pUVM The user mode VM handle.
1398 * @param paConfigs The event to configure and their new state.
1399 * @param cConfigs Number of entries in @a paConfigs.
1400 */
1401static DECLCALLBACK(int) dbgfR3InterruptConfigEx(PUVM pUVM, PCDBGFINTERRUPTCONFIG paConfigs, size_t cConfigs)
1402{
1403 PVM pVM = pUVM->pVM;
1404 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1405
1406 /*
1407 * Apply the changes.
1408 */
1409 bool fChanged = false;
1410 bool fThis;
1411 for (uint32_t i = 0; i < cConfigs; i++)
1412 {
1413 /*
1414 * Hardware interrupts.
1415 */
1416 if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_ENABLED)
1417 {
1418 fChanged |= fThis = ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmHardIntBreakpoints, paConfigs[i].iInterrupt) == false;
1419 if (fThis)
1420 {
1421 Assert(pVM->dbgf.s.cHardIntBreakpoints < 256);
1422 pVM->dbgf.s.cHardIntBreakpoints++;
1423 }
1424 }
1425 else if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_DISABLED)
1426 {
1427 fChanged |= fThis = ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmHardIntBreakpoints, paConfigs[i].iInterrupt) == true;
1428 if (fThis)
1429 {
1430 Assert(pVM->dbgf.s.cHardIntBreakpoints > 0);
1431 pVM->dbgf.s.cHardIntBreakpoints--;
1432 }
1433 }
1434
1435 /*
1436 * Software interrupts.
1437 */
1438 if (paConfigs[i].enmHardState == DBGFINTERRUPTSTATE_ENABLED)
1439 {
1440 fChanged |= fThis = ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSoftIntBreakpoints, paConfigs[i].iInterrupt) == false;
1441 if (fThis)
1442 {
1443 Assert(pVM->dbgf.s.cSoftIntBreakpoints < 256);
1444 pVM->dbgf.s.cSoftIntBreakpoints++;
1445 }
1446 }
1447 else if (paConfigs[i].enmSoftState == DBGFINTERRUPTSTATE_DISABLED)
1448 {
1449 fChanged |= fThis = ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSoftIntBreakpoints, paConfigs[i].iInterrupt) == true;
1450 if (fThis)
1451 {
1452 Assert(pVM->dbgf.s.cSoftIntBreakpoints > 0);
1453 pVM->dbgf.s.cSoftIntBreakpoints--;
1454 }
1455 }
1456 }
1457
1458 /*
1459 * Update the event bitmap entries.
1460 */
1461 if (pVM->dbgf.s.cHardIntBreakpoints > 0)
1462 fChanged |= ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_HARDWARE) == false;
1463 else
1464 fChanged |= ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_HARDWARE) == true;
1465
1466 if (pVM->dbgf.s.cSoftIntBreakpoints > 0)
1467 fChanged |= ASMAtomicBitTestAndSet(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_SOFTWARE) == false;
1468 else
1469 fChanged |= ASMAtomicBitTestAndClear(&pVM->dbgf.s.bmSelectedEvents, DBGFEVENT_INTERRUPT_SOFTWARE) == true;
1470
1471
1472 /*
1473 * Inform HM about changes. In an SMP setup, interrupt execution on the
1474 * other CPUs so their execution loop can be reselected.
1475 */
1476 int rc = VINF_SUCCESS;
1477 if (fChanged)
1478 {
1479 bool const fIsHmEnabled = HMIsEnabled(pVM);
1480 if (fIsHmEnabled)
1481 HMR3NotifyDebugEventChanged(pVM);
1482 if (pVM->cCpus > 1 || fIsHmEnabled)
1483 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE, dbgfR3EventConfigNotifyAllCpus,
1484 (void *)(uintptr_t)fIsHmEnabled);
1485 }
1486 return rc;
1487}
1488
1489
1490/**
1491 * Changes
1492 *
1493 * @returns VBox status code.
1494 * @param pUVM The user mode VM handle.
1495 * @param paConfigs The events to query and where to return the state.
1496 * @param cConfigs The number of elements in @a paConfigs.
1497 * @sa DBGFR3InterruptConfigHardware, DBGFR3InterruptConfigSoftware
1498 */
1499VMMR3DECL(int) DBGFR3InterruptConfigEx(PUVM pUVM, PCDBGFINTERRUPTCONFIG paConfigs, size_t cConfigs)
1500{
1501 /*
1502 * Validate input.
1503 */
1504 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1505 size_t i = cConfigs;
1506 while (i-- > 0)
1507 {
1508 AssertReturn(paConfigs[i].enmHardState <= DBGFINTERRUPTSTATE_DONT_TOUCH, VERR_INVALID_PARAMETER);
1509 AssertReturn(paConfigs[i].enmSoftState <= DBGFINTERRUPTSTATE_DONT_TOUCH, VERR_INVALID_PARAMETER);
1510 }
1511
1512 /*
1513 * Apply the changes in EMT(0).
1514 */
1515 return VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)dbgfR3InterruptConfigEx, 3, pUVM, paConfigs, cConfigs);
1516}
1517
1518
1519/**
1520 * Configures interception of a hardware interrupt.
1521 *
1522 * @returns VBox status code.
1523 * @param pUVM The user mode VM handle.
1524 * @param iInterrupt The interrupt number.
1525 * @param fEnabled Whether interception is enabled or not.
1526 * @sa DBGFR3InterruptSoftwareConfig, DBGFR3InterruptConfigEx
1527 */
1528VMMR3DECL(int) DBGFR3InterruptHardwareConfig(PUVM pUVM, uint8_t iInterrupt, bool fEnabled)
1529{
1530 /*
1531 * Convert to DBGFR3InterruptConfigEx call.
1532 */
1533 DBGFINTERRUPTCONFIG IntCfg = { iInterrupt, (uint8_t)fEnabled, DBGFINTERRUPTSTATE_DONT_TOUCH };
1534 return DBGFR3InterruptConfigEx(pUVM, &IntCfg, 1);
1535}
1536
1537
1538/**
1539 * Configures interception of a software interrupt.
1540 *
1541 * @returns VBox status code.
1542 * @param pUVM The user mode VM handle.
1543 * @param iInterrupt The interrupt number.
1544 * @param fEnabled Whether interception is enabled or not.
1545 * @sa DBGFR3InterruptHardwareConfig, DBGFR3InterruptConfigEx
1546 */
1547VMMR3DECL(int) DBGFR3InterruptSoftwareConfig(PUVM pUVM, uint8_t iInterrupt, bool fEnabled)
1548{
1549 /*
1550 * Convert to DBGFR3InterruptConfigEx call.
1551 */
1552 DBGFINTERRUPTCONFIG IntCfg = { iInterrupt, DBGFINTERRUPTSTATE_DONT_TOUCH, (uint8_t)fEnabled };
1553 return DBGFR3InterruptConfigEx(pUVM, &IntCfg, 1);
1554}
1555
1556
1557/**
1558 * Checks whether interception is enabled for a hardware interrupt.
1559 *
1560 * @returns true if enabled, false if not or invalid input.
1561 * @param pUVM The user mode VM handle.
1562 * @param iInterrupt The interrupt number.
1563 * @sa DBGFR3InterruptSoftwareIsEnabled, DBGF_IS_HARDWARE_INT_ENABLED,
1564 * DBGF_IS_SOFTWARE_INT_ENABLED
1565 */
1566VMMR3DECL(int) DBGFR3InterruptHardwareIsEnabled(PUVM pUVM, uint8_t iInterrupt)
1567{
1568 /*
1569 * Validate input.
1570 */
1571 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1572 PVM pVM = pUVM->pVM;
1573 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1574
1575 /*
1576 * Check it.
1577 */
1578 return ASMBitTest(&pVM->dbgf.s.bmHardIntBreakpoints, iInterrupt);
1579}
1580
1581
1582/**
1583 * Checks whether interception is enabled for a software interrupt.
1584 *
1585 * @returns true if enabled, false if not or invalid input.
1586 * @param pUVM The user mode VM handle.
1587 * @param iInterrupt The interrupt number.
1588 * @sa DBGFR3InterruptHardwareIsEnabled, DBGF_IS_SOFTWARE_INT_ENABLED,
1589 * DBGF_IS_HARDWARE_INT_ENABLED,
1590 */
1591VMMR3DECL(int) DBGFR3InterruptSoftwareIsEnabled(PUVM pUVM, uint8_t iInterrupt)
1592{
1593 /*
1594 * Validate input.
1595 */
1596 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1597 PVM pVM = pUVM->pVM;
1598 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1599
1600 /*
1601 * Check it.
1602 */
1603 return ASMBitTest(&pVM->dbgf.s.bmSoftIntBreakpoints, iInterrupt);
1604}
1605
1606
1607
1608/**
1609 * Call this to single step programmatically.
1610 *
1611 * You must pass down the return code to the EM loop! That's
1612 * where the actual single stepping take place (at least in the
1613 * current implementation).
1614 *
1615 * @returns VINF_EM_DBG_STEP
1616 *
1617 * @param pVCpu The cross context virtual CPU structure.
1618 *
1619 * @thread VCpu EMT
1620 * @internal
1621 */
1622VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
1623{
1624 VMCPU_ASSERT_EMT(pVCpu);
1625
1626 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1627 return VINF_EM_DBG_STEP;
1628}
1629
1630
1631/**
1632 * Inject an NMI into a running VM (only VCPU 0!)
1633 *
1634 * @returns VBox status code.
1635 * @param pUVM The user mode VM structure.
1636 * @param idCpu The ID of the CPU to inject the NMI on.
1637 */
1638VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
1639{
1640 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1641 PVM pVM = pUVM->pVM;
1642 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1643 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1644
1645 /** @todo Implement generic NMI injection. */
1646 if (!HMIsEnabled(pVM))
1647 return VERR_NOT_SUP_IN_RAW_MODE;
1648
1649 VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_INTERRUPT_NMI);
1650 return VINF_SUCCESS;
1651}
1652
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