VirtualBox

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

Last change on this file since 45692 was 45692, checked in by vboxsync, 12 years ago

VMMR3/DBGF: Single-stepping should no longer be in effect when resuming guest execution using the 'go' command.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 36.4 KB
Line 
1/* $Id: DBGF.cpp 45692 2013-04-24 11:28:19Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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 Pointer to the VM.
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 Pointer to the VM.
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 int rc = dbgfR3InfoInit(pUVM);
144 if (RT_SUCCESS(rc))
145 rc = dbgfR3TraceInit(pVM);
146 if (RT_SUCCESS(rc))
147 rc = dbgfR3RegInit(pUVM);
148 if (RT_SUCCESS(rc))
149 rc = dbgfR3AsInit(pUVM);
150 if (RT_SUCCESS(rc))
151 rc = dbgfR3SymInit(pVM);
152 if (RT_SUCCESS(rc))
153 rc = dbgfR3BpInit(pVM);
154 return rc;
155}
156
157
158/**
159 * Terminates and cleans up resources allocated by the DBGF.
160 *
161 * @returns VBox status code.
162 * @param pVM Pointer to the VM.
163 */
164VMMR3_INT_DECL(int) DBGFR3Term(PVM pVM)
165{
166 PUVM pUVM = pVM->pUVM;
167
168 dbgfR3OSTerm(pUVM);
169 dbgfR3AsTerm(pUVM);
170 dbgfR3RegTerm(pUVM);
171 dbgfR3TraceTerm(pVM);
172 dbgfR3InfoTerm(pUVM);
173
174 return VINF_SUCCESS;
175}
176
177
178/**
179 * Called when the VM is powered off to detach debuggers.
180 *
181 * @param pVM The VM handle.
182 */
183VMMR3_INT_DECL(void) DBGFR3PowerOff(PVM pVM)
184{
185
186 /*
187 * Send a termination event to any attached debugger.
188 */
189 /* wait to become the speaker (we should already be that). */
190 if ( pVM->dbgf.s.fAttached
191 && RTSemPingShouldWait(&pVM->dbgf.s.PingPong))
192 RTSemPingWait(&pVM->dbgf.s.PingPong, 5000);
193
194 if (pVM->dbgf.s.fAttached)
195 {
196 /* Just mark it as detached if we're not in a position to send a power
197 off event. It should fail later on. */
198 if (!RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
199 {
200 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
201 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
202 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
203 }
204
205 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
206 {
207 /* Try send the power off event. */
208 int rc;
209 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
210 if (enmCmd == DBGFCMD_DETACH_DEBUGGER)
211 /* the debugger beat us to initiating the detaching. */
212 rc = VINF_SUCCESS;
213 else
214 {
215 /* ignore the command (if any). */
216 enmCmd = DBGFCMD_NO_COMMAND;
217 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_POWERING_OFF;
218 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
219 rc = RTSemPing(&pVM->dbgf.s.PingPong);
220 }
221
222 /*
223 * Process commands and priority requests until we get a command
224 * indicating that the debugger has detached.
225 */
226 uint32_t cPollHack = 1;
227 PVMCPU pVCpu = VMMGetCpu(pVM);
228 while (RT_SUCCESS(rc))
229 {
230 if (enmCmd != DBGFCMD_NO_COMMAND)
231 {
232 /* process command */
233 bool fResumeExecution;
234 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
235 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
236 if (enmCmd == DBGFCMD_DETACHED_DEBUGGER)
237 break;
238 enmCmd = DBGFCMD_NO_COMMAND;
239 }
240 else
241 {
242 /* Wait for new command, processing pending priority requests
243 first. The request processing is a bit crazy, but
244 unfortunately required by plugin unloading. */
245 if ( VM_FF_ISPENDING(pVM, VM_FF_REQUEST)
246 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_REQUEST))
247 {
248 LogFlow(("DBGFR3PowerOff: Processes priority requests...\n"));
249 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
250 if (rc == VINF_SUCCESS)
251 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, true /*fPriorityOnly*/);
252 LogFlow(("DBGFR3PowerOff: VMR3ReqProcess -> %Rrc\n", rc));
253 cPollHack = 1;
254 }
255 else if (cPollHack < 120)
256 cPollHack++;
257
258 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
259 if (RT_SUCCESS(rc))
260 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
261 else if (rc == VERR_TIMEOUT)
262 rc = VINF_SUCCESS;
263 }
264 }
265
266 /*
267 * Clear the FF so we won't get confused later on.
268 */
269 VM_FF_CLEAR(pVM, VM_FF_DBGF);
270 }
271 }
272}
273
274
275/**
276 * Applies relocations to data and code managed by this
277 * component. This function will be called at init and
278 * whenever the VMM need to relocate it self inside the GC.
279 *
280 * @param pVM Pointer to the VM.
281 * @param offDelta Relocation delta relative to old location.
282 */
283VMMR3_INT_DECL(void) DBGFR3Relocate(PVM pVM, RTGCINTPTR offDelta)
284{
285 dbgfR3TraceRelocate(pVM);
286 dbgfR3AsRelocate(pVM->pUVM, offDelta);
287}
288
289
290/**
291 * Waits a little while for a debuggger to attach.
292 *
293 * @returns True is a debugger have attached.
294 * @param pVM Pointer to the VM.
295 * @param enmEvent Event.
296 */
297bool dbgfR3WaitForAttach(PVM pVM, DBGFEVENTTYPE enmEvent)
298{
299 /*
300 * First a message.
301 */
302#ifndef RT_OS_L4
303
304# if !defined(DEBUG) || defined(DEBUG_sandervl) || defined(DEBUG_frank) || defined(IEM_VERIFICATION_MODE)
305 int cWait = 10;
306# else
307 int cWait = HMIsEnabled(pVM)
308 && ( enmEvent == DBGFEVENT_ASSERTION_HYPER
309 || enmEvent == DBGFEVENT_FATAL_ERROR)
310 && !RTEnvExist("VBOX_DBGF_WAIT_FOR_ATTACH")
311 ? 10
312 : 150;
313# endif
314 RTStrmPrintf(g_pStdErr, "DBGF: No debugger attached, waiting %d second%s for one to attach (event=%d)\n",
315 cWait / 10, cWait != 10 ? "s" : "", enmEvent);
316 RTStrmFlush(g_pStdErr);
317 while (cWait > 0)
318 {
319 RTThreadSleep(100);
320 if (pVM->dbgf.s.fAttached)
321 {
322 RTStrmPrintf(g_pStdErr, "Attached!\n");
323 RTStrmFlush(g_pStdErr);
324 return true;
325 }
326
327 /* next */
328 if (!(cWait % 10))
329 {
330 RTStrmPrintf(g_pStdErr, "%d.", cWait / 10);
331 RTStrmFlush(g_pStdErr);
332 }
333 cWait--;
334 }
335#endif
336
337 RTStrmPrintf(g_pStdErr, "Stopping the VM!\n");
338 RTStrmFlush(g_pStdErr);
339 return false;
340}
341
342
343/**
344 * Forced action callback.
345 * The VMM will call this from it's main loop when VM_FF_DBGF is set.
346 *
347 * The function checks and executes pending commands from the debugger.
348 *
349 * @returns VINF_SUCCESS normally.
350 * @returns VERR_DBGF_RAISE_FATAL_ERROR to pretend a fatal error happened.
351 * @param pVM Pointer to the VM.
352 */
353VMMR3_INT_DECL(int) DBGFR3VMMForcedAction(PVM pVM)
354{
355 int rc = VINF_SUCCESS;
356
357 if (VM_FF_TESTANDCLEAR(pVM, VM_FF_DBGF))
358 {
359 PVMCPU pVCpu = VMMGetCpu(pVM);
360
361 /*
362 * Commands?
363 */
364 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
365 {
366#ifdef VBOX_WITH_RAW_MODE
367 /** @todo stupid GDT/LDT sync hack. go away! */
368 SELMR3UpdateFromCPUM(pVM, pVCpu);
369#endif
370
371 /*
372 * Process the command.
373 */
374 bool fResumeExecution;
375 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
376 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
377 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
378 if (!fResumeExecution)
379 rc = dbgfR3VMMWait(pVM);
380 }
381 }
382 return rc;
383}
384
385
386/**
387 * Flag whether the event implies that we're stopped in the hypervisor code
388 * and have to block certain operations.
389 *
390 * @param pVM Pointer to the VM.
391 * @param enmEvent The event.
392 */
393static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
394{
395 switch (enmEvent)
396 {
397 case DBGFEVENT_STEPPED_HYPER:
398 case DBGFEVENT_ASSERTION_HYPER:
399 case DBGFEVENT_BREAKPOINT_HYPER:
400 pVM->dbgf.s.fStoppedInHyper = true;
401 break;
402 default:
403 pVM->dbgf.s.fStoppedInHyper = false;
404 break;
405 }
406}
407
408
409/**
410 * Try to determine the event context.
411 *
412 * @returns debug event context.
413 * @param pVM Pointer to the VM.
414 */
415static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
416{
417 /** @todo SMP support! */
418 PVMCPU pVCpu = &pVM->aCpus[0];
419
420 switch (EMGetState(pVCpu))
421 {
422 case EMSTATE_RAW:
423 case EMSTATE_DEBUG_GUEST_RAW:
424 return DBGFEVENTCTX_RAW;
425
426 case EMSTATE_REM:
427 case EMSTATE_DEBUG_GUEST_REM:
428 return DBGFEVENTCTX_REM;
429
430 case EMSTATE_DEBUG_HYPER:
431 case EMSTATE_GURU_MEDITATION:
432 return DBGFEVENTCTX_HYPER;
433
434 default:
435 return DBGFEVENTCTX_OTHER;
436 }
437}
438
439/**
440 * The common event prologue code.
441 * It will set the 'stopped-in-hyper' flag, make sure someone is attached,
442 * and perhaps process any high priority pending actions (none yet).
443 *
444 * @returns VBox status.
445 * @param pVM Pointer to the VM.
446 * @param enmEvent The event to be sent.
447 */
448static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
449{
450 /** @todo SMP */
451 PVMCPU pVCpu = VMMGetCpu(pVM);
452
453 /*
454 * Check if a debugger is attached.
455 */
456 if ( !pVM->dbgf.s.fAttached
457 && !dbgfR3WaitForAttach(pVM, enmEvent))
458 {
459 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
460 return VERR_DBGF_NOT_ATTACHED;
461 }
462
463 /*
464 * Sync back the state from the REM.
465 */
466 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
467#ifdef VBOX_WITH_REM
468 if (!pVM->dbgf.s.fStoppedInHyper)
469 REMR3StateUpdate(pVM, pVCpu);
470#endif
471
472 /*
473 * Look thru pending commands and finish those which make sense now.
474 */
475 /** @todo Process/purge pending commands. */
476 //int rc = DBGFR3VMMForcedAction(pVM);
477 return VINF_SUCCESS;
478}
479
480
481/**
482 * Sends the event in the event buffer.
483 *
484 * @returns VBox status code.
485 * @param pVM Pointer to the VM.
486 */
487static int dbgfR3SendEvent(PVM pVM)
488{
489 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
490 if (RT_SUCCESS(rc))
491 rc = dbgfR3VMMWait(pVM);
492
493 pVM->dbgf.s.fStoppedInHyper = false;
494 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
495 return rc;
496}
497
498
499/**
500 * Send a generic debugger event which takes no data.
501 *
502 * @returns VBox status.
503 * @param pVM Pointer to the VM.
504 * @param enmEvent The event to send.
505 * @internal
506 */
507VMMR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
508{
509 int rc = dbgfR3EventPrologue(pVM, enmEvent);
510 if (RT_FAILURE(rc))
511 return rc;
512
513 /*
514 * Send the event and process the reply communication.
515 */
516 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
517 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
518 return dbgfR3SendEvent(pVM);
519}
520
521
522/**
523 * Send a debugger event which takes the full source file location.
524 *
525 * @returns VBox status.
526 * @param pVM Pointer to the VM.
527 * @param enmEvent The event to send.
528 * @param pszFile Source file.
529 * @param uLine Line number in source file.
530 * @param pszFunction Function name.
531 * @param pszFormat Message which accompanies the event.
532 * @param ... Message arguments.
533 * @internal
534 */
535VMMR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
536{
537 va_list args;
538 va_start(args, pszFormat);
539 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
540 va_end(args);
541 return rc;
542}
543
544
545/**
546 * Send a debugger event which takes the full source file location.
547 *
548 * @returns VBox status.
549 * @param pVM Pointer to the VM.
550 * @param enmEvent The event to send.
551 * @param pszFile Source file.
552 * @param uLine Line number in source file.
553 * @param pszFunction Function name.
554 * @param pszFormat Message which accompanies the event.
555 * @param args Message arguments.
556 * @internal
557 */
558VMMR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
559{
560 int rc = dbgfR3EventPrologue(pVM, enmEvent);
561 if (RT_FAILURE(rc))
562 return rc;
563
564 /*
565 * Format the message.
566 */
567 char *pszMessage = NULL;
568 char szMessage[8192];
569 if (pszFormat && *pszFormat)
570 {
571 pszMessage = &szMessage[0];
572 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
573 }
574
575 /*
576 * Send the event and process the reply communication.
577 */
578 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
579 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
580 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
581 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
582 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
583 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
584 return dbgfR3SendEvent(pVM);
585}
586
587
588/**
589 * Send a debugger event which takes the two assertion messages.
590 *
591 * @returns VBox status.
592 * @param pVM Pointer to the VM.
593 * @param enmEvent The event to send.
594 * @param pszMsg1 First assertion message.
595 * @param pszMsg2 Second assertion message.
596 */
597VMMR3_INT_DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
598{
599 int rc = dbgfR3EventPrologue(pVM, enmEvent);
600 if (RT_FAILURE(rc))
601 return rc;
602
603 /*
604 * Send the event and process the reply communication.
605 */
606 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
607 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
608 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg1 = pszMsg1;
609 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
610 return dbgfR3SendEvent(pVM);
611}
612
613
614/**
615 * Breakpoint was hit somewhere.
616 * Figure out which breakpoint it is and notify the debugger.
617 *
618 * @returns VBox status.
619 * @param pVM Pointer to the VM.
620 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
621 */
622VMMR3_INT_DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
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 /** @todo SMP */
632 PVMCPU pVCpu = VMMGetCpu0(pVM);
633
634 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
635 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVCpu->dbgf.s.iActiveBp;
636 pVCpu->dbgf.s.iActiveBp = ~0U;
637 if (iBp != ~0U)
638 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
639 else
640 {
641 /* REM breakpoints has be been searched for. */
642#if 0 /** @todo get flat PC api! */
643 uint32_t eip = CPUMGetGuestEIP(pVM);
644#else
645 /* @todo SMP support!! */
646 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(VMMGetCpu(pVM));
647 RTGCPTR eip = pCtx->rip + pCtx->cs.u64Base;
648#endif
649 for (size_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.aBreakpoints); i++)
650 if ( pVM->dbgf.s.aBreakpoints[i].enmType == DBGFBPTYPE_REM
651 && pVM->dbgf.s.aBreakpoints[i].GCPtr == eip)
652 {
653 pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVM->dbgf.s.aBreakpoints[i].iBp;
654 break;
655 }
656 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
657 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
658 }
659 return dbgfR3SendEvent(pVM);
660}
661
662
663/**
664 * Waits for the debugger to respond.
665 *
666 * @returns VBox status. (clearify)
667 * @param pVM Pointer to the VM.
668 */
669static int dbgfR3VMMWait(PVM pVM)
670{
671 PVMCPU pVCpu = VMMGetCpu(pVM);
672
673 LogFlow(("dbgfR3VMMWait:\n"));
674
675#ifdef VBOX_WITH_RAW_MODE
676 /** @todo stupid GDT/LDT sync hack. go away! */
677 SELMR3UpdateFromCPUM(pVM, pVCpu);
678#endif
679 int rcRet = VINF_SUCCESS;
680
681 /*
682 * Waits for the debugger to reply (i.e. issue an command).
683 */
684 for (;;)
685 {
686 /*
687 * Wait.
688 */
689 uint32_t cPollHack = 1; /** @todo this interface is horrible now that we're using lots of VMR3ReqCall stuff all over DBGF. */
690 for (;;)
691 {
692 int rc;
693 if ( !VM_FF_ISPENDING(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_REQUEST)
694 && !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_REQUEST))
695 {
696 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
697 if (RT_SUCCESS(rc))
698 break;
699 if (rc != VERR_TIMEOUT)
700 {
701 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
702 return rc;
703 }
704 }
705
706 if (VM_FF_ISPENDING(pVM, VM_FF_EMT_RENDEZVOUS))
707 {
708 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
709 cPollHack = 1;
710 }
711 else if ( VM_FF_ISPENDING(pVM, VM_FF_REQUEST)
712 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_REQUEST))
713 {
714 LogFlow(("dbgfR3VMMWait: Processes requests...\n"));
715 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
716 if (rc == VINF_SUCCESS)
717 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, false /*fPriorityOnly*/);
718 LogFlow(("dbgfR3VMMWait: VMR3ReqProcess -> %Rrc rcRet=%Rrc\n", rc, rcRet));
719 cPollHack = 1;
720 }
721 else
722 {
723 rc = VINF_SUCCESS;
724 if (cPollHack < 120)
725 cPollHack++;
726 }
727
728 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
729 {
730 switch (rc)
731 {
732 case VINF_EM_DBG_BREAKPOINT:
733 case VINF_EM_DBG_STEPPED:
734 case VINF_EM_DBG_STEP:
735 case VINF_EM_DBG_STOP:
736 AssertMsgFailed(("rc=%Rrc\n", rc));
737 break;
738
739 /* return straight away */
740 case VINF_EM_TERMINATE:
741 case VINF_EM_OFF:
742 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
743 return rc;
744
745 /* remember return code. */
746 default:
747 AssertReleaseMsgFailed(("rc=%Rrc is not in the switch!\n", rc));
748 case VINF_EM_RESET:
749 case VINF_EM_SUSPEND:
750 case VINF_EM_HALT:
751 case VINF_EM_RESUME:
752 case VINF_EM_RESCHEDULE:
753 case VINF_EM_RESCHEDULE_REM:
754 case VINF_EM_RESCHEDULE_RAW:
755 if (rc < rcRet || rcRet == VINF_SUCCESS)
756 rcRet = rc;
757 break;
758 }
759 }
760 else if (RT_FAILURE(rc))
761 {
762 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
763 return rc;
764 }
765 }
766
767 /*
768 * Process the command.
769 */
770 bool fResumeExecution;
771 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
772 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
773 int rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
774 if (fResumeExecution)
775 {
776 if (RT_FAILURE(rc))
777 rcRet = rc;
778 else if ( rc >= VINF_EM_FIRST
779 && rc <= VINF_EM_LAST
780 && (rc < rcRet || rcRet == VINF_SUCCESS))
781 rcRet = rc;
782 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rcRet));
783 return rcRet;
784 }
785 }
786}
787
788
789/**
790 * Executes command from debugger.
791 * The caller is responsible for waiting or resuming execution based on the
792 * value returned in the *pfResumeExecution indicator.
793 *
794 * @returns VBox status. (clearify!)
795 * @param pVM Pointer to the VM.
796 * @param enmCmd The command in question.
797 * @param pCmdData Pointer to the command data.
798 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
799 */
800static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
801{
802 bool fSendEvent;
803 bool fResume;
804 int rc = VINF_SUCCESS;
805
806 NOREF(pCmdData); /* for later */
807
808 switch (enmCmd)
809 {
810 /*
811 * Halt is answered by an event say that we've halted.
812 */
813 case DBGFCMD_HALT:
814 {
815 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
816 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
817 fSendEvent = true;
818 fResume = false;
819 break;
820 }
821
822
823 /*
824 * Resume is not answered we'll just resume execution.
825 */
826 case DBGFCMD_GO:
827 {
828 /** @todo SMP */
829 PVMCPU pVCpu = VMMGetCpu0(pVM);
830 pVCpu->dbgf.s.fSingleSteppingRaw = false;
831 fSendEvent = false;
832 fResume = true;
833 break;
834 }
835
836 /** @todo implement (and define) the rest of the commands. */
837
838 /*
839 * Disable breakpoints and stuff.
840 * Send an everythings cool event to the debugger thread and resume execution.
841 */
842 case DBGFCMD_DETACH_DEBUGGER:
843 {
844 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
845 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
846 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
847 fSendEvent = true;
848 fResume = true;
849 break;
850 }
851
852 /*
853 * The debugger has detached successfully.
854 * There is no reply to this event.
855 */
856 case DBGFCMD_DETACHED_DEBUGGER:
857 {
858 fSendEvent = false;
859 fResume = true;
860 break;
861 }
862
863 /*
864 * Single step, with trace into.
865 */
866 case DBGFCMD_SINGLE_STEP:
867 {
868 Log2(("Single step\n"));
869 rc = VINF_EM_DBG_STEP;
870 /** @todo SMP */
871 PVMCPU pVCpu = VMMGetCpu0(pVM);
872 pVCpu->dbgf.s.fSingleSteppingRaw = true;
873 fSendEvent = false;
874 fResume = true;
875 break;
876 }
877
878 /*
879 * Default is to send an invalid command event.
880 */
881 default:
882 {
883 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
884 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
885 fSendEvent = true;
886 fResume = false;
887 break;
888 }
889 }
890
891 /*
892 * Send pending event.
893 */
894 if (fSendEvent)
895 {
896 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
897 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
898 if (RT_FAILURE(rc2))
899 {
900 AssertRC(rc2);
901 *pfResumeExecution = true;
902 return rc2;
903 }
904 }
905
906 /*
907 * Return.
908 */
909 *pfResumeExecution = fResume;
910 return rc;
911}
912
913
914/**
915 * Attaches a debugger to the specified VM.
916 *
917 * Only one debugger at a time.
918 *
919 * @returns VBox status code.
920 * @param pUVM The user mode VM handle.
921 */
922VMMR3DECL(int) DBGFR3Attach(PUVM pUVM)
923{
924 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
925 PVM pVM = pUVM->pVM;
926 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
927
928 /*
929 * Call the VM, use EMT for serialization.
930 */
931 /** @todo SMP */
932 return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)dbgfR3Attach, 1, pVM);
933}
934
935
936/**
937 * EMT worker for DBGFR3Attach.
938 *
939 * @returns VBox status code.
940 * @param pVM Pointer to the VM.
941 */
942static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
943{
944 if (pVM->dbgf.s.fAttached)
945 {
946 Log(("dbgR3Attach: Debugger already attached\n"));
947 return VERR_DBGF_ALREADY_ATTACHED;
948 }
949
950 /*
951 * Create the Ping-Pong structure.
952 */
953 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
954 AssertRCReturn(rc, rc);
955
956 /*
957 * Set the attached flag.
958 */
959 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
960 return VINF_SUCCESS;
961}
962
963
964/**
965 * Detaches a debugger from the specified VM.
966 *
967 * Caller must be attached to the VM.
968 *
969 * @returns VBox status code.
970 * @param pUVM The user mode VM handle.
971 */
972VMMR3DECL(int) DBGFR3Detach(PUVM pUVM)
973{
974 LogFlow(("DBGFR3Detach:\n"));
975 int rc;
976
977 /*
978 * Check if attached.
979 */
980 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
981 PVM pVM = pUVM->pVM;
982 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
983 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
984
985 /*
986 * Try send the detach command.
987 * Keep in mind that we might be racing EMT, so, be extra careful.
988 */
989 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
990 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
991 {
992 rc = RTSemPong(&pVM->dbgf.s.PingPong);
993 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
994 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
995 }
996
997 /*
998 * Wait for the OK event.
999 */
1000 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
1001 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
1002
1003 /*
1004 * Send the notification command indicating that we're really done.
1005 */
1006 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
1007 rc = RTSemPong(&pVM->dbgf.s.PingPong);
1008 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
1009
1010 LogFlowFunc(("returns VINF_SUCCESS\n"));
1011 return VINF_SUCCESS;
1012}
1013
1014
1015/**
1016 * Wait for a debug event.
1017 *
1018 * @returns VBox status. Will not return VBOX_INTERRUPTED.
1019 * @param pUVM The user mode VM handle.
1020 * @param cMillies Number of millis to wait.
1021 * @param ppEvent Where to store the event pointer.
1022 */
1023VMMR3DECL(int) DBGFR3EventWait(PUVM pUVM, RTMSINTERVAL cMillies, PCDBGFEVENT *ppEvent)
1024{
1025 /*
1026 * Check state.
1027 */
1028 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1029 PVM pVM = pUVM->pVM;
1030 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1031 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1032 *ppEvent = NULL;
1033
1034 /*
1035 * Wait.
1036 */
1037 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
1038 if (RT_SUCCESS(rc))
1039 {
1040 *ppEvent = &pVM->dbgf.s.DbgEvent;
1041 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
1042 return VINF_SUCCESS;
1043 }
1044
1045 return rc;
1046}
1047
1048
1049/**
1050 * Halts VM execution.
1051 *
1052 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
1053 * arrives. Until that time it's not possible to issue any new commands.
1054 *
1055 * @returns VBox status.
1056 * @param pUVM The user mode VM handle.
1057 */
1058VMMR3DECL(int) DBGFR3Halt(PUVM pUVM)
1059{
1060 /*
1061 * Check state.
1062 */
1063 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1064 PVM pVM = pUVM->pVM;
1065 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1066 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1067 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1068 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
1069 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
1070 return VWRN_DBGF_ALREADY_HALTED;
1071
1072 /*
1073 * Send command.
1074 */
1075 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
1076
1077 return VINF_SUCCESS;
1078}
1079
1080
1081/**
1082 * Checks if the VM is halted by the debugger.
1083 *
1084 * @returns True if halted.
1085 * @returns False if not halted.
1086 * @param pUVM The user mode VM handle.
1087 */
1088VMMR3DECL(bool) DBGFR3IsHalted(PUVM pUVM)
1089{
1090 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1091 PVM pVM = pUVM->pVM;
1092 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1093 AssertReturn(pVM->dbgf.s.fAttached, false);
1094
1095 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1096 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1097 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1098}
1099
1100
1101/**
1102 * Checks if the debugger can wait for events or not.
1103 *
1104 * This function is only used by lazy, multiplexing debuggers. :-)
1105 *
1106 * @returns VBox status code.
1107 * @retval VINF_SUCCESS if waitable.
1108 * @retval VERR_SEM_OUT_OF_TURN if not waitable.
1109 * @retval VERR_INVALID_VM_HANDLE if the VM is being (/ has been) destroyed
1110 * (not asserted) or if the handle is invalid (asserted).
1111 * @retval VERR_DBGF_NOT_ATTACHED if not attached.
1112 *
1113 * @param pUVM The user mode VM handle.
1114 */
1115VMMR3DECL(int) DBGFR3QueryWaitable(PUVM pUVM)
1116{
1117 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1118
1119 /* Note! There is a slight race here, unfortunately. */
1120 PVM pVM = pUVM->pVM;
1121 if (!RT_VALID_PTR(pVM))
1122 return VERR_INVALID_VM_HANDLE;
1123 if (pVM->enmVMState >= VMSTATE_DESTROYING)
1124 return VERR_INVALID_VM_HANDLE;
1125 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1126
1127 if (!RTSemPongShouldWait(&pVM->dbgf.s.PingPong))
1128 return VERR_SEM_OUT_OF_TURN;
1129
1130 return VINF_SUCCESS;
1131}
1132
1133
1134/**
1135 * Resumes VM execution.
1136 *
1137 * There is no receipt event on this command.
1138 *
1139 * @returns VBox status.
1140 * @param pUVM The user mode VM handle.
1141 */
1142VMMR3DECL(int) DBGFR3Resume(PUVM pUVM)
1143{
1144 /*
1145 * Check state.
1146 */
1147 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1148 PVM pVM = pUVM->pVM;
1149 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1150 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1151 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1152
1153 /*
1154 * Send the ping back to the emulation thread telling it to run.
1155 */
1156 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1157 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1158 AssertRC(rc);
1159
1160 return rc;
1161}
1162
1163
1164/**
1165 * Step Into.
1166 *
1167 * A single step event is generated from this command.
1168 * The current implementation is not reliable, so don't rely on the event coming.
1169 *
1170 * @returns VBox status.
1171 * @param pUVM The user mode VM handle.
1172 * @param idCpu The ID of the CPU to single step on.
1173 */
1174VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
1175{
1176 /*
1177 * Check state.
1178 */
1179 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1180 PVM pVM = pUVM->pVM;
1181 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1182 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1183 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1184 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1185
1186 /*
1187 * Send the ping back to the emulation thread telling it to run.
1188 */
1189/** @todo SMP (idCpu) */
1190 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1191 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1192 AssertRC(rc);
1193 return rc;
1194}
1195
1196
1197/**
1198 * Call this to single step programmatically.
1199 *
1200 * You must pass down the return code to the EM loop! That's
1201 * where the actual single stepping take place (at least in the
1202 * current implementation).
1203 *
1204 * @returns VINF_EM_DBG_STEP
1205 *
1206 * @param pVCpu Pointer to the VMCPU.
1207 *
1208 * @thread VCpu EMT
1209 * @internal
1210 */
1211VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
1212{
1213 VMCPU_ASSERT_EMT(pVCpu);
1214
1215 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1216 return VINF_EM_DBG_STEP;
1217}
1218
1219
1220/**
1221 * Inject an NMI into a running VM (only VCPU 0!)
1222 *
1223 * @returns VBox status code.
1224 * @param pVM Pointer to the VM.
1225 */
1226VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
1227{
1228 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1229 PVM pVM = pUVM->pVM;
1230 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1231 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1232
1233 /** @todo Implement generic NMI injection. */
1234 if (!HMIsEnabled(pVM))
1235 return VERR_NOT_SUP_IN_RAW_MODE;
1236
1237 VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_INTERRUPT_NMI);
1238 return VINF_SUCCESS;
1239}
1240
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