VirtualBox

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

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

#ifdef the raw-mode force flags to find more code to #ifdef out.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 36.3 KB
Line 
1/* $Id: DBGF.cpp 45533 2013-04-13 16:13:22Z 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 fSendEvent = false;
829 fResume = true;
830 break;
831 }
832
833 /** @todo implement (and define) the rest of the commands. */
834
835 /*
836 * Disable breakpoints and stuff.
837 * Send an everythings cool event to the debugger thread and resume execution.
838 */
839 case DBGFCMD_DETACH_DEBUGGER:
840 {
841 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
842 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
843 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
844 fSendEvent = true;
845 fResume = true;
846 break;
847 }
848
849 /*
850 * The debugger has detached successfully.
851 * There is no reply to this event.
852 */
853 case DBGFCMD_DETACHED_DEBUGGER:
854 {
855 fSendEvent = false;
856 fResume = true;
857 break;
858 }
859
860 /*
861 * Single step, with trace into.
862 */
863 case DBGFCMD_SINGLE_STEP:
864 {
865 Log2(("Single step\n"));
866 rc = VINF_EM_DBG_STEP;
867 /** @todo SMP */
868 PVMCPU pVCpu = VMMGetCpu0(pVM);
869 pVCpu->dbgf.s.fSingleSteppingRaw = true;
870 fSendEvent = false;
871 fResume = true;
872 break;
873 }
874
875 /*
876 * Default is to send an invalid command event.
877 */
878 default:
879 {
880 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
881 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
882 fSendEvent = true;
883 fResume = false;
884 break;
885 }
886 }
887
888 /*
889 * Send pending event.
890 */
891 if (fSendEvent)
892 {
893 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
894 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
895 if (RT_FAILURE(rc2))
896 {
897 AssertRC(rc2);
898 *pfResumeExecution = true;
899 return rc2;
900 }
901 }
902
903 /*
904 * Return.
905 */
906 *pfResumeExecution = fResume;
907 return rc;
908}
909
910
911/**
912 * Attaches a debugger to the specified VM.
913 *
914 * Only one debugger at a time.
915 *
916 * @returns VBox status code.
917 * @param pUVM The user mode VM handle.
918 */
919VMMR3DECL(int) DBGFR3Attach(PUVM pUVM)
920{
921 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
922 PVM pVM = pUVM->pVM;
923 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
924
925 /*
926 * Call the VM, use EMT for serialization.
927 */
928 /** @todo SMP */
929 return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)dbgfR3Attach, 1, pVM);
930}
931
932
933/**
934 * EMT worker for DBGFR3Attach.
935 *
936 * @returns VBox status code.
937 * @param pVM Pointer to the VM.
938 */
939static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
940{
941 if (pVM->dbgf.s.fAttached)
942 {
943 Log(("dbgR3Attach: Debugger already attached\n"));
944 return VERR_DBGF_ALREADY_ATTACHED;
945 }
946
947 /*
948 * Create the Ping-Pong structure.
949 */
950 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
951 AssertRCReturn(rc, rc);
952
953 /*
954 * Set the attached flag.
955 */
956 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
957 return VINF_SUCCESS;
958}
959
960
961/**
962 * Detaches a debugger from the specified VM.
963 *
964 * Caller must be attached to the VM.
965 *
966 * @returns VBox status code.
967 * @param pUVM The user mode VM handle.
968 */
969VMMR3DECL(int) DBGFR3Detach(PUVM pUVM)
970{
971 LogFlow(("DBGFR3Detach:\n"));
972 int rc;
973
974 /*
975 * Check if attached.
976 */
977 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
978 PVM pVM = pUVM->pVM;
979 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
980 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
981
982 /*
983 * Try send the detach command.
984 * Keep in mind that we might be racing EMT, so, be extra careful.
985 */
986 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
987 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
988 {
989 rc = RTSemPong(&pVM->dbgf.s.PingPong);
990 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
991 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
992 }
993
994 /*
995 * Wait for the OK event.
996 */
997 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
998 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
999
1000 /*
1001 * Send the notification command indicating that we're really done.
1002 */
1003 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
1004 rc = RTSemPong(&pVM->dbgf.s.PingPong);
1005 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
1006
1007 LogFlowFunc(("returns VINF_SUCCESS\n"));
1008 return VINF_SUCCESS;
1009}
1010
1011
1012/**
1013 * Wait for a debug event.
1014 *
1015 * @returns VBox status. Will not return VBOX_INTERRUPTED.
1016 * @param pUVM The user mode VM handle.
1017 * @param cMillies Number of millis to wait.
1018 * @param ppEvent Where to store the event pointer.
1019 */
1020VMMR3DECL(int) DBGFR3EventWait(PUVM pUVM, RTMSINTERVAL cMillies, PCDBGFEVENT *ppEvent)
1021{
1022 /*
1023 * Check state.
1024 */
1025 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1026 PVM pVM = pUVM->pVM;
1027 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1028 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1029 *ppEvent = NULL;
1030
1031 /*
1032 * Wait.
1033 */
1034 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
1035 if (RT_SUCCESS(rc))
1036 {
1037 *ppEvent = &pVM->dbgf.s.DbgEvent;
1038 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
1039 return VINF_SUCCESS;
1040 }
1041
1042 return rc;
1043}
1044
1045
1046/**
1047 * Halts VM execution.
1048 *
1049 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
1050 * arrives. Until that time it's not possible to issue any new commands.
1051 *
1052 * @returns VBox status.
1053 * @param pUVM The user mode VM handle.
1054 */
1055VMMR3DECL(int) DBGFR3Halt(PUVM pUVM)
1056{
1057 /*
1058 * Check state.
1059 */
1060 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1061 PVM pVM = pUVM->pVM;
1062 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1063 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1064 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1065 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
1066 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
1067 return VWRN_DBGF_ALREADY_HALTED;
1068
1069 /*
1070 * Send command.
1071 */
1072 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
1073
1074 return VINF_SUCCESS;
1075}
1076
1077
1078/**
1079 * Checks if the VM is halted by the debugger.
1080 *
1081 * @returns True if halted.
1082 * @returns False if not halted.
1083 * @param pUVM The user mode VM handle.
1084 */
1085VMMR3DECL(bool) DBGFR3IsHalted(PUVM pUVM)
1086{
1087 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1088 PVM pVM = pUVM->pVM;
1089 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1090 AssertReturn(pVM->dbgf.s.fAttached, false);
1091
1092 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1093 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1094 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1095}
1096
1097
1098/**
1099 * Checks if the debugger can wait for events or not.
1100 *
1101 * This function is only used by lazy, multiplexing debuggers. :-)
1102 *
1103 * @returns VBox status code.
1104 * @retval VINF_SUCCESS if waitable.
1105 * @retval VERR_SEM_OUT_OF_TURN if not waitable.
1106 * @retval VERR_INVALID_VM_HANDLE if the VM is being (/ has been) destroyed
1107 * (not asserted) or if the handle is invalid (asserted).
1108 * @retval VERR_DBGF_NOT_ATTACHED if not attached.
1109 *
1110 * @param pUVM The user mode VM handle.
1111 */
1112VMMR3DECL(int) DBGFR3QueryWaitable(PUVM pUVM)
1113{
1114 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1115
1116 /* Note! There is a slight race here, unfortunately. */
1117 PVM pVM = pUVM->pVM;
1118 if (!RT_VALID_PTR(pVM))
1119 return VERR_INVALID_VM_HANDLE;
1120 if (pVM->enmVMState >= VMSTATE_DESTROYING)
1121 return VERR_INVALID_VM_HANDLE;
1122 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1123
1124 if (!RTSemPongShouldWait(&pVM->dbgf.s.PingPong))
1125 return VERR_SEM_OUT_OF_TURN;
1126
1127 return VINF_SUCCESS;
1128}
1129
1130
1131/**
1132 * Resumes VM execution.
1133 *
1134 * There is no receipt event on this command.
1135 *
1136 * @returns VBox status.
1137 * @param pUVM The user mode VM handle.
1138 */
1139VMMR3DECL(int) DBGFR3Resume(PUVM pUVM)
1140{
1141 /*
1142 * Check state.
1143 */
1144 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1145 PVM pVM = pUVM->pVM;
1146 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1147 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1148 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1149
1150 /*
1151 * Send the ping back to the emulation thread telling it to run.
1152 */
1153 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1154 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1155 AssertRC(rc);
1156
1157 return rc;
1158}
1159
1160
1161/**
1162 * Step Into.
1163 *
1164 * A single step event is generated from this command.
1165 * The current implementation is not reliable, so don't rely on the event coming.
1166 *
1167 * @returns VBox status.
1168 * @param pUVM The user mode VM handle.
1169 * @param idCpu The ID of the CPU to single step on.
1170 */
1171VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
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 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1182
1183 /*
1184 * Send the ping back to the emulation thread telling it to run.
1185 */
1186/** @todo SMP (idCpu) */
1187 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1188 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1189 AssertRC(rc);
1190 return rc;
1191}
1192
1193
1194/**
1195 * Call this to single step programmatically.
1196 *
1197 * You must pass down the return code to the EM loop! That's
1198 * where the actual single stepping take place (at least in the
1199 * current implementation).
1200 *
1201 * @returns VINF_EM_DBG_STEP
1202 *
1203 * @param pVCpu Pointer to the VMCPU.
1204 *
1205 * @thread VCpu EMT
1206 * @internal
1207 */
1208VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
1209{
1210 VMCPU_ASSERT_EMT(pVCpu);
1211
1212 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1213 return VINF_EM_DBG_STEP;
1214}
1215
1216
1217/**
1218 * Inject an NMI into a running VM (only VCPU 0!)
1219 *
1220 * @returns VBox status code.
1221 * @param pVM Pointer to the VM.
1222 */
1223VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
1224{
1225 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1226 PVM pVM = pUVM->pVM;
1227 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1228 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1229
1230 /** @todo Implement generic NMI injection. */
1231 if (!HMIsEnabled(pVM))
1232 return VERR_NOT_SUP_IN_RAW_MODE;
1233
1234 VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_INTERRUPT_NMI);
1235 return VINF_SUCCESS;
1236}
1237
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