VirtualBox

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

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

Doxygen.

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