VirtualBox

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

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

header (C) fixes

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