VirtualBox

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

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

HM,++: pVM -> pUVM for main, mark as many as possible interfaces module internal.

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