VirtualBox

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

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

VMM: SELM and VMM early HM init changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 36.1 KB
Line 
1/* $Id: DBGF.cpp 45701 2013-04-24 14:21:09Z 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 * Command pending? Process it.
363 */
364 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
365 {
366 bool fResumeExecution;
367 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
368 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
369 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
370 if (!fResumeExecution)
371 rc = dbgfR3VMMWait(pVM);
372 }
373 }
374 return rc;
375}
376
377
378/**
379 * Flag whether the event implies that we're stopped in the hypervisor code
380 * and have to block certain operations.
381 *
382 * @param pVM Pointer to the VM.
383 * @param enmEvent The event.
384 */
385static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
386{
387 switch (enmEvent)
388 {
389 case DBGFEVENT_STEPPED_HYPER:
390 case DBGFEVENT_ASSERTION_HYPER:
391 case DBGFEVENT_BREAKPOINT_HYPER:
392 pVM->dbgf.s.fStoppedInHyper = true;
393 break;
394 default:
395 pVM->dbgf.s.fStoppedInHyper = false;
396 break;
397 }
398}
399
400
401/**
402 * Try to determine the event context.
403 *
404 * @returns debug event context.
405 * @param pVM Pointer to the VM.
406 */
407static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
408{
409 /** @todo SMP support! */
410 PVMCPU pVCpu = &pVM->aCpus[0];
411
412 switch (EMGetState(pVCpu))
413 {
414 case EMSTATE_RAW:
415 case EMSTATE_DEBUG_GUEST_RAW:
416 return DBGFEVENTCTX_RAW;
417
418 case EMSTATE_REM:
419 case EMSTATE_DEBUG_GUEST_REM:
420 return DBGFEVENTCTX_REM;
421
422 case EMSTATE_DEBUG_HYPER:
423 case EMSTATE_GURU_MEDITATION:
424 return DBGFEVENTCTX_HYPER;
425
426 default:
427 return DBGFEVENTCTX_OTHER;
428 }
429}
430
431/**
432 * The common event prologue code.
433 * It will set the 'stopped-in-hyper' flag, make sure someone is attached,
434 * and perhaps process any high priority pending actions (none yet).
435 *
436 * @returns VBox status.
437 * @param pVM Pointer to the VM.
438 * @param enmEvent The event to be sent.
439 */
440static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
441{
442 /** @todo SMP */
443 PVMCPU pVCpu = VMMGetCpu(pVM);
444
445 /*
446 * Check if a debugger is attached.
447 */
448 if ( !pVM->dbgf.s.fAttached
449 && !dbgfR3WaitForAttach(pVM, enmEvent))
450 {
451 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
452 return VERR_DBGF_NOT_ATTACHED;
453 }
454
455 /*
456 * Sync back the state from the REM.
457 */
458 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
459#ifdef VBOX_WITH_REM
460 if (!pVM->dbgf.s.fStoppedInHyper)
461 REMR3StateUpdate(pVM, pVCpu);
462#endif
463
464 /*
465 * Look thru pending commands and finish those which make sense now.
466 */
467 /** @todo Process/purge pending commands. */
468 //int rc = DBGFR3VMMForcedAction(pVM);
469 return VINF_SUCCESS;
470}
471
472
473/**
474 * Sends the event in the event buffer.
475 *
476 * @returns VBox status code.
477 * @param pVM Pointer to the VM.
478 */
479static int dbgfR3SendEvent(PVM pVM)
480{
481 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
482 if (RT_SUCCESS(rc))
483 rc = dbgfR3VMMWait(pVM);
484
485 pVM->dbgf.s.fStoppedInHyper = false;
486 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
487 return rc;
488}
489
490
491/**
492 * Send a generic debugger event which takes no data.
493 *
494 * @returns VBox status.
495 * @param pVM Pointer to the VM.
496 * @param enmEvent The event to send.
497 * @internal
498 */
499VMMR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
500{
501 int rc = dbgfR3EventPrologue(pVM, enmEvent);
502 if (RT_FAILURE(rc))
503 return rc;
504
505 /*
506 * Send the event and process the reply communication.
507 */
508 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
509 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
510 return dbgfR3SendEvent(pVM);
511}
512
513
514/**
515 * Send a debugger event which takes the full source file location.
516 *
517 * @returns VBox status.
518 * @param pVM Pointer to the VM.
519 * @param enmEvent The event to send.
520 * @param pszFile Source file.
521 * @param uLine Line number in source file.
522 * @param pszFunction Function name.
523 * @param pszFormat Message which accompanies the event.
524 * @param ... Message arguments.
525 * @internal
526 */
527VMMR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
528{
529 va_list args;
530 va_start(args, pszFormat);
531 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
532 va_end(args);
533 return rc;
534}
535
536
537/**
538 * Send a debugger event which takes the full source file location.
539 *
540 * @returns VBox status.
541 * @param pVM Pointer to the VM.
542 * @param enmEvent The event to send.
543 * @param pszFile Source file.
544 * @param uLine Line number in source file.
545 * @param pszFunction Function name.
546 * @param pszFormat Message which accompanies the event.
547 * @param args Message arguments.
548 * @internal
549 */
550VMMR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
551{
552 int rc = dbgfR3EventPrologue(pVM, enmEvent);
553 if (RT_FAILURE(rc))
554 return rc;
555
556 /*
557 * Format the message.
558 */
559 char *pszMessage = NULL;
560 char szMessage[8192];
561 if (pszFormat && *pszFormat)
562 {
563 pszMessage = &szMessage[0];
564 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
565 }
566
567 /*
568 * Send the event and process the reply communication.
569 */
570 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
571 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
572 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
573 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
574 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
575 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
576 return dbgfR3SendEvent(pVM);
577}
578
579
580/**
581 * Send a debugger event which takes the two assertion messages.
582 *
583 * @returns VBox status.
584 * @param pVM Pointer to the VM.
585 * @param enmEvent The event to send.
586 * @param pszMsg1 First assertion message.
587 * @param pszMsg2 Second assertion message.
588 */
589VMMR3_INT_DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
590{
591 int rc = dbgfR3EventPrologue(pVM, enmEvent);
592 if (RT_FAILURE(rc))
593 return rc;
594
595 /*
596 * Send the event and process the reply communication.
597 */
598 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
599 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
600 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg1 = pszMsg1;
601 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
602 return dbgfR3SendEvent(pVM);
603}
604
605
606/**
607 * Breakpoint was hit somewhere.
608 * Figure out which breakpoint it is and notify the debugger.
609 *
610 * @returns VBox status.
611 * @param pVM Pointer to the VM.
612 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
613 */
614VMMR3_INT_DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
615{
616 int rc = dbgfR3EventPrologue(pVM, enmEvent);
617 if (RT_FAILURE(rc))
618 return rc;
619
620 /*
621 * Send the event and process the reply communication.
622 */
623 /** @todo SMP */
624 PVMCPU pVCpu = VMMGetCpu0(pVM);
625
626 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
627 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVCpu->dbgf.s.iActiveBp;
628 pVCpu->dbgf.s.iActiveBp = ~0U;
629 if (iBp != ~0U)
630 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
631 else
632 {
633 /* REM breakpoints has be been searched for. */
634#if 0 /** @todo get flat PC api! */
635 uint32_t eip = CPUMGetGuestEIP(pVM);
636#else
637 /* @todo SMP support!! */
638 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(VMMGetCpu(pVM));
639 RTGCPTR eip = pCtx->rip + pCtx->cs.u64Base;
640#endif
641 for (size_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.aBreakpoints); i++)
642 if ( pVM->dbgf.s.aBreakpoints[i].enmType == DBGFBPTYPE_REM
643 && pVM->dbgf.s.aBreakpoints[i].GCPtr == eip)
644 {
645 pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVM->dbgf.s.aBreakpoints[i].iBp;
646 break;
647 }
648 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
649 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
650 }
651 return dbgfR3SendEvent(pVM);
652}
653
654
655/**
656 * Waits for the debugger to respond.
657 *
658 * @returns VBox status. (clearify)
659 * @param pVM Pointer to the VM.
660 */
661static int dbgfR3VMMWait(PVM pVM)
662{
663 PVMCPU pVCpu = VMMGetCpu(pVM);
664
665 LogFlow(("dbgfR3VMMWait:\n"));
666 int rcRet = VINF_SUCCESS;
667
668 /*
669 * Waits for the debugger to reply (i.e. issue an command).
670 */
671 for (;;)
672 {
673 /*
674 * Wait.
675 */
676 uint32_t cPollHack = 1; /** @todo this interface is horrible now that we're using lots of VMR3ReqCall stuff all over DBGF. */
677 for (;;)
678 {
679 int rc;
680 if ( !VM_FF_ISPENDING(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_REQUEST)
681 && !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_REQUEST))
682 {
683 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
684 if (RT_SUCCESS(rc))
685 break;
686 if (rc != VERR_TIMEOUT)
687 {
688 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
689 return rc;
690 }
691 }
692
693 if (VM_FF_ISPENDING(pVM, VM_FF_EMT_RENDEZVOUS))
694 {
695 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
696 cPollHack = 1;
697 }
698 else if ( VM_FF_ISPENDING(pVM, VM_FF_REQUEST)
699 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_REQUEST))
700 {
701 LogFlow(("dbgfR3VMMWait: Processes requests...\n"));
702 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
703 if (rc == VINF_SUCCESS)
704 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, false /*fPriorityOnly*/);
705 LogFlow(("dbgfR3VMMWait: VMR3ReqProcess -> %Rrc rcRet=%Rrc\n", rc, rcRet));
706 cPollHack = 1;
707 }
708 else
709 {
710 rc = VINF_SUCCESS;
711 if (cPollHack < 120)
712 cPollHack++;
713 }
714
715 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
716 {
717 switch (rc)
718 {
719 case VINF_EM_DBG_BREAKPOINT:
720 case VINF_EM_DBG_STEPPED:
721 case VINF_EM_DBG_STEP:
722 case VINF_EM_DBG_STOP:
723 AssertMsgFailed(("rc=%Rrc\n", rc));
724 break;
725
726 /* return straight away */
727 case VINF_EM_TERMINATE:
728 case VINF_EM_OFF:
729 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
730 return rc;
731
732 /* remember return code. */
733 default:
734 AssertReleaseMsgFailed(("rc=%Rrc is not in the switch!\n", rc));
735 case VINF_EM_RESET:
736 case VINF_EM_SUSPEND:
737 case VINF_EM_HALT:
738 case VINF_EM_RESUME:
739 case VINF_EM_RESCHEDULE:
740 case VINF_EM_RESCHEDULE_REM:
741 case VINF_EM_RESCHEDULE_RAW:
742 if (rc < rcRet || rcRet == VINF_SUCCESS)
743 rcRet = rc;
744 break;
745 }
746 }
747 else if (RT_FAILURE(rc))
748 {
749 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
750 return rc;
751 }
752 }
753
754 /*
755 * Process the command.
756 */
757 bool fResumeExecution;
758 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
759 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
760 int rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
761 if (fResumeExecution)
762 {
763 if (RT_FAILURE(rc))
764 rcRet = rc;
765 else if ( rc >= VINF_EM_FIRST
766 && rc <= VINF_EM_LAST
767 && (rc < rcRet || rcRet == VINF_SUCCESS))
768 rcRet = rc;
769 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rcRet));
770 return rcRet;
771 }
772 }
773}
774
775
776/**
777 * Executes command from debugger.
778 * The caller is responsible for waiting or resuming execution based on the
779 * value returned in the *pfResumeExecution indicator.
780 *
781 * @returns VBox status. (clearify!)
782 * @param pVM Pointer to the VM.
783 * @param enmCmd The command in question.
784 * @param pCmdData Pointer to the command data.
785 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
786 */
787static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
788{
789 bool fSendEvent;
790 bool fResume;
791 int rc = VINF_SUCCESS;
792
793 NOREF(pCmdData); /* for later */
794
795 switch (enmCmd)
796 {
797 /*
798 * Halt is answered by an event say that we've halted.
799 */
800 case DBGFCMD_HALT:
801 {
802 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
803 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
804 fSendEvent = true;
805 fResume = false;
806 break;
807 }
808
809
810 /*
811 * Resume is not answered we'll just resume execution.
812 */
813 case DBGFCMD_GO:
814 {
815 /** @todo SMP */
816 PVMCPU pVCpu = VMMGetCpu0(pVM);
817 pVCpu->dbgf.s.fSingleSteppingRaw = false;
818 fSendEvent = false;
819 fResume = true;
820 break;
821 }
822
823 /** @todo implement (and define) the rest of the commands. */
824
825 /*
826 * Disable breakpoints and stuff.
827 * Send an everythings cool event to the debugger thread and resume execution.
828 */
829 case DBGFCMD_DETACH_DEBUGGER:
830 {
831 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
832 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
833 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
834 fSendEvent = true;
835 fResume = true;
836 break;
837 }
838
839 /*
840 * The debugger has detached successfully.
841 * There is no reply to this event.
842 */
843 case DBGFCMD_DETACHED_DEBUGGER:
844 {
845 fSendEvent = false;
846 fResume = true;
847 break;
848 }
849
850 /*
851 * Single step, with trace into.
852 */
853 case DBGFCMD_SINGLE_STEP:
854 {
855 Log2(("Single step\n"));
856 rc = VINF_EM_DBG_STEP;
857 /** @todo SMP */
858 PVMCPU pVCpu = VMMGetCpu0(pVM);
859 pVCpu->dbgf.s.fSingleSteppingRaw = true;
860 fSendEvent = false;
861 fResume = true;
862 break;
863 }
864
865 /*
866 * Default is to send an invalid command event.
867 */
868 default:
869 {
870 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
871 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
872 fSendEvent = true;
873 fResume = false;
874 break;
875 }
876 }
877
878 /*
879 * Send pending event.
880 */
881 if (fSendEvent)
882 {
883 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
884 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
885 if (RT_FAILURE(rc2))
886 {
887 AssertRC(rc2);
888 *pfResumeExecution = true;
889 return rc2;
890 }
891 }
892
893 /*
894 * Return.
895 */
896 *pfResumeExecution = fResume;
897 return rc;
898}
899
900
901/**
902 * Attaches a debugger to the specified VM.
903 *
904 * Only one debugger at a time.
905 *
906 * @returns VBox status code.
907 * @param pUVM The user mode VM handle.
908 */
909VMMR3DECL(int) DBGFR3Attach(PUVM pUVM)
910{
911 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
912 PVM pVM = pUVM->pVM;
913 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
914
915 /*
916 * Call the VM, use EMT for serialization.
917 */
918 /** @todo SMP */
919 return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)dbgfR3Attach, 1, pVM);
920}
921
922
923/**
924 * EMT worker for DBGFR3Attach.
925 *
926 * @returns VBox status code.
927 * @param pVM Pointer to the VM.
928 */
929static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
930{
931 if (pVM->dbgf.s.fAttached)
932 {
933 Log(("dbgR3Attach: Debugger already attached\n"));
934 return VERR_DBGF_ALREADY_ATTACHED;
935 }
936
937 /*
938 * Create the Ping-Pong structure.
939 */
940 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
941 AssertRCReturn(rc, rc);
942
943 /*
944 * Set the attached flag.
945 */
946 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
947 return VINF_SUCCESS;
948}
949
950
951/**
952 * Detaches a debugger from the specified VM.
953 *
954 * Caller must be attached to the VM.
955 *
956 * @returns VBox status code.
957 * @param pUVM The user mode VM handle.
958 */
959VMMR3DECL(int) DBGFR3Detach(PUVM pUVM)
960{
961 LogFlow(("DBGFR3Detach:\n"));
962 int rc;
963
964 /*
965 * Check if attached.
966 */
967 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
968 PVM pVM = pUVM->pVM;
969 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
970 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
971
972 /*
973 * Try send the detach command.
974 * Keep in mind that we might be racing EMT, so, be extra careful.
975 */
976 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
977 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
978 {
979 rc = RTSemPong(&pVM->dbgf.s.PingPong);
980 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
981 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
982 }
983
984 /*
985 * Wait for the OK event.
986 */
987 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
988 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
989
990 /*
991 * Send the notification command indicating that we're really done.
992 */
993 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
994 rc = RTSemPong(&pVM->dbgf.s.PingPong);
995 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
996
997 LogFlowFunc(("returns VINF_SUCCESS\n"));
998 return VINF_SUCCESS;
999}
1000
1001
1002/**
1003 * Wait for a debug event.
1004 *
1005 * @returns VBox status. Will not return VBOX_INTERRUPTED.
1006 * @param pUVM The user mode VM handle.
1007 * @param cMillies Number of millis to wait.
1008 * @param ppEvent Where to store the event pointer.
1009 */
1010VMMR3DECL(int) DBGFR3EventWait(PUVM pUVM, RTMSINTERVAL cMillies, PCDBGFEVENT *ppEvent)
1011{
1012 /*
1013 * Check state.
1014 */
1015 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1016 PVM pVM = pUVM->pVM;
1017 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1018 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1019 *ppEvent = NULL;
1020
1021 /*
1022 * Wait.
1023 */
1024 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
1025 if (RT_SUCCESS(rc))
1026 {
1027 *ppEvent = &pVM->dbgf.s.DbgEvent;
1028 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
1029 return VINF_SUCCESS;
1030 }
1031
1032 return rc;
1033}
1034
1035
1036/**
1037 * Halts VM execution.
1038 *
1039 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
1040 * arrives. Until that time it's not possible to issue any new commands.
1041 *
1042 * @returns VBox status.
1043 * @param pUVM The user mode VM handle.
1044 */
1045VMMR3DECL(int) DBGFR3Halt(PUVM pUVM)
1046{
1047 /*
1048 * Check state.
1049 */
1050 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1051 PVM pVM = pUVM->pVM;
1052 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1053 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1054 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1055 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
1056 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
1057 return VWRN_DBGF_ALREADY_HALTED;
1058
1059 /*
1060 * Send command.
1061 */
1062 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
1063
1064 return VINF_SUCCESS;
1065}
1066
1067
1068/**
1069 * Checks if the VM is halted by the debugger.
1070 *
1071 * @returns True if halted.
1072 * @returns False if not halted.
1073 * @param pUVM The user mode VM handle.
1074 */
1075VMMR3DECL(bool) DBGFR3IsHalted(PUVM pUVM)
1076{
1077 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1078 PVM pVM = pUVM->pVM;
1079 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1080 AssertReturn(pVM->dbgf.s.fAttached, false);
1081
1082 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1083 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1084 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1085}
1086
1087
1088/**
1089 * Checks if the debugger can wait for events or not.
1090 *
1091 * This function is only used by lazy, multiplexing debuggers. :-)
1092 *
1093 * @returns VBox status code.
1094 * @retval VINF_SUCCESS if waitable.
1095 * @retval VERR_SEM_OUT_OF_TURN if not waitable.
1096 * @retval VERR_INVALID_VM_HANDLE if the VM is being (/ has been) destroyed
1097 * (not asserted) or if the handle is invalid (asserted).
1098 * @retval VERR_DBGF_NOT_ATTACHED if not attached.
1099 *
1100 * @param pUVM The user mode VM handle.
1101 */
1102VMMR3DECL(int) DBGFR3QueryWaitable(PUVM pUVM)
1103{
1104 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1105
1106 /* Note! There is a slight race here, unfortunately. */
1107 PVM pVM = pUVM->pVM;
1108 if (!RT_VALID_PTR(pVM))
1109 return VERR_INVALID_VM_HANDLE;
1110 if (pVM->enmVMState >= VMSTATE_DESTROYING)
1111 return VERR_INVALID_VM_HANDLE;
1112 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1113
1114 if (!RTSemPongShouldWait(&pVM->dbgf.s.PingPong))
1115 return VERR_SEM_OUT_OF_TURN;
1116
1117 return VINF_SUCCESS;
1118}
1119
1120
1121/**
1122 * Resumes VM execution.
1123 *
1124 * There is no receipt event on this command.
1125 *
1126 * @returns VBox status.
1127 * @param pUVM The user mode VM handle.
1128 */
1129VMMR3DECL(int) DBGFR3Resume(PUVM pUVM)
1130{
1131 /*
1132 * Check state.
1133 */
1134 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1135 PVM pVM = pUVM->pVM;
1136 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1137 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1138 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1139
1140 /*
1141 * Send the ping back to the emulation thread telling it to run.
1142 */
1143 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1144 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1145 AssertRC(rc);
1146
1147 return rc;
1148}
1149
1150
1151/**
1152 * Step Into.
1153 *
1154 * A single step event is generated from this command.
1155 * The current implementation is not reliable, so don't rely on the event coming.
1156 *
1157 * @returns VBox status.
1158 * @param pUVM The user mode VM handle.
1159 * @param idCpu The ID of the CPU to single step on.
1160 */
1161VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
1162{
1163 /*
1164 * Check state.
1165 */
1166 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1167 PVM pVM = pUVM->pVM;
1168 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1169 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1170 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1171 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1172
1173 /*
1174 * Send the ping back to the emulation thread telling it to run.
1175 */
1176/** @todo SMP (idCpu) */
1177 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1178 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1179 AssertRC(rc);
1180 return rc;
1181}
1182
1183
1184/**
1185 * Call this to single step programmatically.
1186 *
1187 * You must pass down the return code to the EM loop! That's
1188 * where the actual single stepping take place (at least in the
1189 * current implementation).
1190 *
1191 * @returns VINF_EM_DBG_STEP
1192 *
1193 * @param pVCpu Pointer to the VMCPU.
1194 *
1195 * @thread VCpu EMT
1196 * @internal
1197 */
1198VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
1199{
1200 VMCPU_ASSERT_EMT(pVCpu);
1201
1202 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1203 return VINF_EM_DBG_STEP;
1204}
1205
1206
1207/**
1208 * Inject an NMI into a running VM (only VCPU 0!)
1209 *
1210 * @returns VBox status code.
1211 * @param pVM Pointer to the VM.
1212 */
1213VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
1214{
1215 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1216 PVM pVM = pUVM->pVM;
1217 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1218 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1219
1220 /** @todo Implement generic NMI injection. */
1221 if (!HMIsEnabled(pVM))
1222 return VERR_NOT_SUP_IN_RAW_MODE;
1223
1224 VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_INTERRUPT_NMI);
1225 return VINF_SUCCESS;
1226}
1227
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