VirtualBox

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

Last change on this file since 8797 was 8797, checked in by vboxsync, 16 years ago

New DBGF interface for digging into the guts of the guest OS kernel.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 29.4 KB
Line 
1/* $Id: DBGF.cpp 8797 2008-05-13 23:16:03Z vboxsync $ */
2/** @file
3 * VMM DBGF - Debugger Facility.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/** @page pg_dbgf DBGC - The Debugger Facility
24 *
25 * The purpose of the DBGC is to provide an interface for debuggers to manipulate
26 * the VMM without having to mess up the source code for each of them. The DBGF
27 * is always built in and will always work when a debugger attaches to the VM.
28 * The DBGF provides the basic debugger features, such as halting execution,
29 * handling breakpoints and single step execution.
30 *
31 * The interface is working in a manner similar to the win32, linux and os2
32 * debugger interfaces. It interface has an asynchronous nature. This comes from
33 * the fact that the VMM and the Debugger are running in different threads.
34 * They are refered to as the "emulation thread" and the "debugger thread",
35 * or as the "ping thread" and the "pong thread, respectivly. (The last set
36 * of names comes from the use of the Ping-Pong synchronization construct from
37 * the RTSem API.)
38 *
39 *
40 *
41 * @section sec_dbgf_scenario Debugger Scenario
42 *
43 * The debugger starts by attaching to the VM. For pratical reasons we limit the
44 * number of concurrently attached debuggers to 1 per VM. The action of attaching
45 * to the VM causes the VM to check and generate debug events.
46 *
47 * The debugger then will wait/poll for debug events and issue commands.
48 *
49 * The waiting and polling is done by the DBGFEventWait() function. It will wait
50 * for the emulation thread to send a ping, thus indicating that there is an
51 * event waiting to be processed.
52 *
53 * An event can be a respons to an command issued previously, the hitting of a
54 * breakpoint, or running into a bad/fatal VMM condition. The debugger now have
55 * the ping and must respond to the event at hand - the VMM is waiting. This
56 * usually means that the user of the debugger must do something, but it doesn't
57 * have to. The debugger is free to call any DBGF function (nearly at least)
58 * while processing the event.
59 *
60 * Typically the user will issue a request for the execution to be resumed, so
61 * the debugger calls DBGFResume() and goes back to waiting/polling for events.
62 *
63 * When the user eventually terminates the debugging session or selects another
64 * VM, the debugger detaches from the VM. This means that breakpoints are disabled
65 * and that the emulation thread no longer polls for debugger commands.
66 *
67 */
68
69
70
71/*******************************************************************************
72* Header Files *
73*******************************************************************************/
74#define LOG_GROUP LOG_GROUP_DBGF
75#include <VBox/dbgf.h>
76#include <VBox/selm.h>
77#include <VBox/rem.h>
78#include <VBox/em.h>
79#include "DBGFInternal.h"
80#include <VBox/vm.h>
81#include <VBox/err.h>
82
83#include <VBox/log.h>
84#include <iprt/semaphore.h>
85#include <iprt/thread.h>
86#include <iprt/asm.h>
87#include <iprt/time.h>
88#include <iprt/assert.h>
89#include <iprt/stream.h>
90
91
92/*******************************************************************************
93* Internal Functions *
94*******************************************************************************/
95static int dbgfr3VMMWait(PVM pVM);
96static int dbgfr3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution);
97
98
99/**
100 * Sets the VMM Debug Command variable.
101 *
102 * @returns Previous command.
103 * @param pVM VM Handle.
104 * @param enmCmd The command.
105 */
106DECLINLINE(DBGFCMD) dbgfr3SetCmd(PVM pVM, DBGFCMD enmCmd)
107{
108 DBGFCMD rc;
109 if (enmCmd == DBGFCMD_NO_COMMAND)
110 {
111 Log2(("DBGF: Setting command to %d (DBGFCMD_NO_COMMAND)\n", enmCmd));
112 rc = (DBGFCMD)ASMAtomicXchgU32((uint32_t volatile *)(void *)&pVM->dbgf.s.enmVMMCmd, enmCmd);
113 VM_FF_CLEAR(pVM, VM_FF_DBGF);
114 }
115 else
116 {
117 Log2(("DBGF: Setting command to %d\n", enmCmd));
118 AssertMsg(pVM->dbgf.s.enmVMMCmd == DBGFCMD_NO_COMMAND, ("enmCmd=%d enmVMMCmd=%d\n", enmCmd, pVM->dbgf.s.enmVMMCmd));
119 rc = (DBGFCMD)ASMAtomicXchgU32((uint32_t volatile *)(void *)&pVM->dbgf.s.enmVMMCmd, enmCmd);
120 VM_FF_SET(pVM, VM_FF_DBGF);
121 VMR3NotifyFF(pVM, false /* didn't notify REM */);
122 }
123 return rc;
124}
125
126
127/**
128 * Initializes the DBGF.
129 *
130 * @returns VBox status code.
131 * @param pVM VM handle.
132 */
133DBGFR3DECL(int) DBGFR3Init(PVM pVM)
134{
135 int rc = dbgfR3InfoInit(pVM);
136 if (VBOX_SUCCESS(rc))
137 rc = dbgfR3SymInit(pVM);
138 if (VBOX_SUCCESS(rc))
139 rc = dbgfR3BpInit(pVM);
140 return rc;
141}
142
143
144/**
145 * Termiantes and cleans up resources allocated by the DBGF.
146 *
147 * @returns VBox status code.
148 * @param pVM VM Handle.
149 */
150DBGFR3DECL(int) DBGFR3Term(PVM pVM)
151{
152 int rc;
153
154 /*
155 * Send a termination event to any attached debuggers.
156 */
157 /* wait for any pending events or whatever */
158 if ( pVM->dbgf.s.fAttached
159 && pVM->dbgf.s.PingPong.enmSpeaker == RTPINGPONGSPEAKER_PONG)
160 {
161 do rc = RTSemPingWait(&pVM->dbgf.s.PingPong, 5000);
162 while (pVM->dbgf.s.fAttached);
163 }
164
165 /* now, send the event */
166 if ( pVM->dbgf.s.fAttached
167 && pVM->dbgf.s.PingPong.enmSpeaker != RTPINGPONGSPEAKER_PONG)
168 {
169 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_TERMINATING;
170 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
171 rc = RTSemPing(&pVM->dbgf.s.PingPong);
172 if (VBOX_SUCCESS(rc))
173 {
174 /*
175 * Waits for the debugger to detach.
176 */
177 while (pVM->dbgf.s.fAttached)
178 {
179 /*
180 * Wait.
181 */
182 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, 1000);
183
184 /*
185 * Process the command.
186 */
187 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
188 DBGFCMD enmCmd = dbgfr3SetCmd(pVM, DBGFCMD_NO_COMMAND);
189 if (enmCmd != DBGFCMD_NO_COMMAND)
190 {
191 bool fResumeExecution = false;
192 rc = dbgfr3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
193 if (enmCmd == DBGFCMD_DETACH_DEBUGGER)
194 break;
195 }
196 else if (VBOX_FAILURE(rc))
197 break;
198 }
199 }
200 }
201
202 /*
203 * Terminate the other bits.
204 */
205 dbgfR3OSTerm(pVM);
206 dbgfR3InfoTerm(pVM);
207 return VINF_SUCCESS;
208}
209
210
211/**
212 * Applies relocations to data and code managed by this
213 * component. This function will be called at init and
214 * whenever the VMM need to relocate it self inside the GC.
215 *
216 * @param pVM VM handle.
217 * @param offDelta Relocation delta relative to old location.
218 */
219DBGFR3DECL(void) DBGFR3Relocate(PVM pVM, RTGCINTPTR offDelta)
220{
221}
222
223
224/**
225 * Waits a little while for a debuggger to attach.
226 *
227 * @returns True is a debugger have attached.
228 * @param pVM VM handle.
229 * @param enmEvent Event.
230 */
231bool dbgfR3WaitForAttach(PVM pVM, DBGFEVENTTYPE enmEvent)
232{
233 /*
234 * First a message.
235 */
236#ifndef RT_OS_L4
237
238 RTStrmPrintf(g_pStdErr, "DBGF: No debugger attached, waiting 15 seconds for one to attach (event=%d)\n", enmEvent);
239 RTStrmFlush(g_pStdErr);
240# ifdef DEBUG_sandervl
241 int cWait = 10;
242# else
243 int cWait = 150;
244# endif
245 while (cWait > 0)
246 {
247 RTThreadSleep(100);
248 if (pVM->dbgf.s.fAttached)
249 {
250 RTStrmPrintf(g_pStdErr, "Attached!\n");
251 RTStrmFlush(g_pStdErr);
252 return true;
253 }
254
255 /* next */
256 if (!(cWait % 10))
257 {
258 RTStrmPrintf(g_pStdErr, "%d.", cWait / 10);
259 RTStrmFlush(g_pStdErr);
260 }
261 cWait--;
262 }
263#endif
264
265 RTStrmPrintf(g_pStdErr, "Stopping the VM!\n");
266 RTStrmFlush(g_pStdErr);
267 return false;
268}
269
270
271/**
272 * Forced action callback.
273 * The VMM will call this from it's main loop when VM_FF_DBGF is set.
274 *
275 * The function checks and executes pending commands from the debugger.
276 *
277 * @returns VINF_SUCCESS normally.
278 * @returns VERR_DBGF_RAISE_FATAL_ERROR to pretend a fatal error happend.
279 * @param pVM VM Handle.
280 */
281DBGFR3DECL(int) DBGFR3VMMForcedAction(PVM pVM)
282{
283 /*
284 * Clear the FF DBGF request flag.
285 */
286 Assert(pVM->fForcedActions & VM_FF_DBGF);
287 VM_FF_CLEAR(pVM, VM_FF_DBGF);
288
289 /*
290 * Commands?
291 */
292 int rc = VINF_SUCCESS;
293 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
294 {
295 /** @todo stupid GDT/LDT sync hack. go away! */
296 SELMR3UpdateFromCPUM(pVM);
297
298 /*
299 * Process the command.
300 */
301 bool fResumeExecution;
302 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
303 DBGFCMD enmCmd = dbgfr3SetCmd(pVM, DBGFCMD_NO_COMMAND);
304 rc = dbgfr3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
305 if (!fResumeExecution)
306 rc = dbgfr3VMMWait(pVM);
307 }
308 return rc;
309}
310
311
312/**
313 * Flag whether the event implies that we're stopped in the hypervisor code
314 * and have to block certain operations.
315 *
316 * @param pVM The VM handle.
317 * @param enmEvent The event.
318 */
319static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
320{
321 switch (enmEvent)
322 {
323 case DBGFEVENT_STEPPED_HYPER:
324 case DBGFEVENT_ASSERTION_HYPER:
325 case DBGFEVENT_BREAKPOINT_HYPER:
326 pVM->dbgf.s.fStoppedInHyper = true;
327 break;
328 default:
329 pVM->dbgf.s.fStoppedInHyper = false;
330 break;
331 }
332}
333
334
335/**
336 * Try determin the event context.
337 *
338 * @returns debug event context.
339 * @param pVM The VM handle.
340 */
341static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
342{
343 switch (EMGetState(pVM))
344 {
345 case EMSTATE_RAW:
346 case EMSTATE_DEBUG_GUEST_RAW:
347 return DBGFEVENTCTX_RAW;
348
349 case EMSTATE_REM:
350 case EMSTATE_DEBUG_GUEST_REM:
351 return DBGFEVENTCTX_REM;
352
353 case EMSTATE_DEBUG_HYPER:
354 case EMSTATE_GURU_MEDITATION:
355 return DBGFEVENTCTX_HYPER;
356
357 default:
358 return DBGFEVENTCTX_OTHER;
359 }
360}
361
362/**
363 * The common event prologue code.
364 * It will set the 'stopped-in-hyper' flag, make sure someone's attach,
365 * and perhaps process any high priority pending actions (none yet).
366 *
367 * @returns VBox status.
368 * @param pVM The VM handle.
369 * @param enmEvent The event to be sent.
370 */
371static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
372{
373 /*
374 * Check if a debugger is attached.
375 */
376 if ( !pVM->dbgf.s.fAttached
377 && !dbgfR3WaitForAttach(pVM, enmEvent))
378 {
379 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
380 return VERR_DBGF_NOT_ATTACHED;
381 }
382
383 /*
384 * Sync back the state from the REM.
385 */
386 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
387 if (!pVM->dbgf.s.fStoppedInHyper)
388 REMR3StateUpdate(pVM);
389
390 /*
391 * Look thru pending commands and finish those which make sense now.
392 */
393 /** @todo Process/purge pending commands. */
394 //int rc = DBGFR3VMMForcedAction(pVM);
395 return VINF_SUCCESS;
396}
397
398
399/**
400 * Sends the event in the event buffer.
401 *
402 * @returns VBox status code.
403 * @param pVM The VM handle.
404 */
405static int dbgfR3SendEvent(PVM pVM)
406{
407 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
408 if (VBOX_SUCCESS(rc))
409 rc = dbgfr3VMMWait(pVM);
410
411 pVM->dbgf.s.fStoppedInHyper = false;
412 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
413 return rc;
414}
415
416
417/**
418 * Send a generic debugger event which takes no data.
419 *
420 * @returns VBox status.
421 * @param pVM The VM handle.
422 * @param enmEvent The event to send.
423 */
424DBGFR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
425{
426 int rc = dbgfR3EventPrologue(pVM, enmEvent);
427 if (VBOX_FAILURE(rc))
428 return rc;
429
430 /*
431 * Send the event and process the reply communication.
432 */
433 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
434 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
435 return dbgfR3SendEvent(pVM);
436}
437
438
439/**
440 * Send a debugger event which takes the full source file location.
441 *
442 * @returns VBox status.
443 * @param pVM The VM handle.
444 * @param enmEvent The event to send.
445 * @param pszFile Source file.
446 * @param uLine Line number in source file.
447 * @param pszFunction Function name.
448 * @param pszFormat Message which accompanies the event.
449 * @param ... Message arguments.
450 */
451DBGFR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
452{
453 va_list args;
454 va_start(args, pszFormat);
455 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
456 va_end(args);
457 return rc;
458}
459
460
461/**
462 * Send a debugger event which takes the full source file location.
463 *
464 * @returns VBox status.
465 * @param pVM The VM handle.
466 * @param enmEvent The event to send.
467 * @param pszFile Source file.
468 * @param uLine Line number in source file.
469 * @param pszFunction Function name.
470 * @param pszFormat Message which accompanies the event.
471 * @param args Message arguments.
472 */
473DBGFR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
474{
475 int rc = dbgfR3EventPrologue(pVM, enmEvent);
476 if (VBOX_FAILURE(rc))
477 return rc;
478
479 /*
480 * Format the message.
481 */
482 char *pszMessage = NULL;
483 char szMessage[8192];
484 if (pszFormat && *pszFormat)
485 {
486 pszMessage = &szMessage[0];
487 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
488 }
489
490 /*
491 * Send the event and process the reply communication.
492 */
493 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
494 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
495 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
496 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
497 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
498 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
499 return dbgfR3SendEvent(pVM);
500}
501
502
503/**
504 * Send a debugger event which takes the two assertion messages.
505 *
506 * @returns VBox status.
507 * @param pVM The VM handle.
508 * @param enmEvent The event to send.
509 * @param pszMsg1 First assertion message.
510 * @param pszMsg2 Second assertion message.
511 */
512DBGFR3DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
513{
514 int rc = dbgfR3EventPrologue(pVM, enmEvent);
515 if (VBOX_FAILURE(rc))
516 return rc;
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.Assert.pszMsg1 = pszMsg1;
524 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
525 return dbgfR3SendEvent(pVM);
526}
527
528
529/**
530 * Breakpoint was hit somewhere.
531 * Figure out which breakpoint it is and notify the debugger.
532 *
533 * @returns VBox status.
534 * @param pVM The VM handle.
535 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
536 */
537DBGFR3DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
538{
539 int rc = dbgfR3EventPrologue(pVM, enmEvent);
540 if (VBOX_FAILURE(rc))
541 return rc;
542
543 /*
544 * Send the event and process the reply communication.
545 */
546 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
547 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVM->dbgf.s.iActiveBp;
548 pVM->dbgf.s.iActiveBp = ~0U;
549 if (iBp != ~0U)
550 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
551 else
552 {
553 /* REM breakpoints has be been searched for. */
554#if 0 /** @todo get flat PC api! */
555 uint32_t eip = CPUMGetGuestEIP(pVM);
556#else
557 PCPUMCTX pCtx;
558 CPUMQueryGuestCtxPtr(pVM, &pCtx);
559 uint32_t eip = pCtx->eip + pCtx->csHid.u32Base;
560#endif
561 for (iBp = 0; iBp < ELEMENTS(pVM->dbgf.s.aBreakpoints); iBp++)
562 if ( pVM->dbgf.s.aBreakpoints[iBp].enmType == DBGFBPTYPE_REM
563 && pVM->dbgf.s.aBreakpoints[iBp].GCPtr == eip)
564 {
565 pVM->dbgf.s.DbgEvent.u.Bp.iBp = iBp;
566 break;
567 }
568 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
569 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
570 }
571 return dbgfR3SendEvent(pVM);
572}
573
574
575/**
576 * Waits for the debugger to respond.
577 *
578 * @returns VBox status. (clearify)
579 * @param pVM VM handle.
580 */
581static int dbgfr3VMMWait(PVM pVM)
582{
583 LogFlow(("dbgfr3VMMWait:\n"));
584
585 /** @todo stupid GDT/LDT sync hack. go away! */
586 SELMR3UpdateFromCPUM(pVM);
587 int rcRet = VINF_SUCCESS;
588
589 /*
590 * Waits for the debugger to reply (i.e. issue an command).
591 */
592 for (;;)
593 {
594 /*
595 * Wait.
596 */
597 for (;;)
598 {
599 int rc = RTSemPingWait(&pVM->dbgf.s.PingPong, 250);
600 if (VBOX_SUCCESS(rc))
601 break;
602 if (rc != VERR_TIMEOUT)
603 {
604 LogFlow(("dbgfr3VMMWait: returns %Vrc\n", rc));
605 return rc;
606 }
607
608 if (VM_FF_ISSET(pVM, VM_FF_REQUEST))
609 {
610 LogFlow(("dbgfr3VMMWait: Processes requests...\n"));
611 rc = VMR3ReqProcessU(pVM->pUVM);
612 LogFlow(("dbgfr3VMMWait: VMR3ReqProcess -> %Vrc rcRet=%Vrc\n", rc, rcRet));
613 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
614 {
615 switch (rc)
616 {
617 case VINF_EM_DBG_BREAKPOINT:
618 case VINF_EM_DBG_STEPPED:
619 case VINF_EM_DBG_STEP:
620 case VINF_EM_DBG_STOP:
621 AssertMsgFailed(("rc=%Vrc\n", rc));
622 break;
623
624 /* return straight away */
625 case VINF_EM_TERMINATE:
626 case VINF_EM_OFF:
627 LogFlow(("dbgfr3VMMWait: returns %Vrc\n", rc));
628 return rc;
629
630 /* remember return code. */
631 default:
632 AssertReleaseMsgFailed(("rc=%Vrc is not in the switch!\n", rc));
633 case VINF_EM_RESET:
634 case VINF_EM_SUSPEND:
635 case VINF_EM_HALT:
636 case VINF_EM_RESUME:
637 case VINF_EM_RESCHEDULE:
638 case VINF_EM_RESCHEDULE_REM:
639 case VINF_EM_RESCHEDULE_RAW:
640 if (rc < rcRet || rcRet == VINF_SUCCESS)
641 rcRet = rc;
642 break;
643 }
644 }
645 else if (VBOX_FAILURE(rc))
646 {
647 LogFlow(("dbgfr3VMMWait: returns %Vrc\n", rc));
648 return rc;
649 }
650 }
651 }
652
653 /*
654 * Process the command.
655 */
656 bool fResumeExecution;
657 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
658 DBGFCMD enmCmd = dbgfr3SetCmd(pVM, DBGFCMD_NO_COMMAND);
659 int rc = dbgfr3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
660 if (fResumeExecution)
661 {
662 if (VBOX_FAILURE(rc))
663 rcRet = rc;
664 else if ( rc >= VINF_EM_FIRST
665 && rc <= VINF_EM_LAST
666 && (rc < rcRet || rcRet == VINF_SUCCESS))
667 rcRet = rc;
668 LogFlow(("dbgfr3VMMWait: returns %Vrc\n", rcRet));
669 return rcRet;
670 }
671 }
672}
673
674
675/**
676 * Executes command from debugger.
677 * The caller is responsible for waiting or resuming execution based on the
678 * value returned in the *pfResumeExecution indicator.
679 *
680 * @returns VBox status. (clearify!)
681 * @param pVM VM Handle.
682 * @param enmCmd The command in question.
683 * @param pCmdData Pointer to the command data.
684 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
685 */
686static int dbgfr3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
687{
688 bool fSendEvent;
689 bool fResume;
690 int rc = VINF_SUCCESS;
691
692 switch (enmCmd)
693 {
694 /*
695 * Halt is answered by an event say that we've halted.
696 */
697 case DBGFCMD_HALT:
698 {
699 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
700 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
701 fSendEvent = true;
702 fResume = false;
703 break;
704 }
705
706
707 /*
708 * Resume is not answered we'll just resume execution.
709 */
710 case DBGFCMD_GO:
711 {
712 fSendEvent = false;
713 fResume = true;
714 break;
715 }
716
717 /** @todo implement (and define) the rest of the commands. */
718
719 /*
720 * Disable breakpoints and stuff.
721 * Send an everythings cool event to the debugger thread and resume execution.
722 */
723 case DBGFCMD_DETACH_DEBUGGER:
724 {
725 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
726 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
727 VM_FF_CLEAR(pVM, VM_FF_DBGF);
728 pVM->dbgf.s.fAttached = false;
729 fSendEvent = true;
730 fResume = true;
731 break;
732 }
733
734 /*
735 * Single step, with trace into.
736 */
737 case DBGFCMD_SINGLE_STEP:
738 {
739 Log2(("Single step\n"));
740 rc = VINF_EM_DBG_STEP;
741 pVM->dbgf.s.fSingleSteppingRaw = true;
742 fSendEvent = false;
743 fResume = true;
744 break;
745 }
746
747 /*
748 * Default is to send an invalid command event.
749 */
750 default:
751 {
752 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
753 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
754 fSendEvent = true;
755 fResume = false;
756 break;
757 }
758 }
759
760 /*
761 * Send pending event.
762 */
763 if (fSendEvent)
764 {
765 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
766 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
767 if (VBOX_FAILURE(rc2))
768 {
769 AssertRC(rc2);
770 *pfResumeExecution = true;
771 return rc2;
772 }
773 }
774
775 /*
776 * Return.
777 */
778 *pfResumeExecution = fResume;
779 return rc;
780}
781
782
783
784
785/**
786 * Attaches a debugger to the specified VM.
787 *
788 * Only one debugger at a time.
789 *
790 * @returns VBox status code.
791 * @param pVM VM Handle.
792 */
793DBGFR3DECL(int) DBGFR3Attach(PVM pVM)
794{
795 /*
796 * Check if already attached.
797 */
798 if (pVM->dbgf.s.fAttached)
799 {
800 AssertMsgFailed(("Already attached\n"));
801 return VERR_DBGF_ALREADY_ATTACHED;
802 }
803
804 /*
805 * Create the Ping-Pong structure.
806 */
807 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
808 if (rc)
809 return rc;
810
811 /*
812 * Set the attached flag.
813 */
814 pVM->dbgf.s.fAttached = true;
815 return VINF_SUCCESS;
816}
817
818
819/**
820 * Detaches a debugger from the specified VM.
821 *
822 * Caller must be attached to the VM.
823 *
824 * @returns VBox status code.
825 * @param pVM VM Handle.
826 */
827DBGFR3DECL(int) DBGFR3Detach(PVM pVM)
828{
829 /*
830 * Check if attached.
831 */
832 if (!pVM->dbgf.s.fAttached)
833 {
834 AssertMsgFailed(("Not attached to VM!\n"));
835 return VERR_DBGF_NOT_ATTACHED;
836 }
837
838 /*
839 * Send detach command.
840 */
841 if (pVM->dbgf.s.PingPong.enmSpeaker == RTPINGPONGSPEAKER_PONG)
842 {
843 dbgfr3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
844 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
845 if (VBOX_FAILURE(rc))
846 {
847 AssertMsgFailed(("Failed to signal emulation thread. rc=%d\n", rc));
848 return rc;
849 }
850 }
851 else
852 dbgfr3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
853
854 /*
855 * Wait for the ok event.
856 */
857 PCDBGFEVENT pEvent;
858 int rc = DBGFR3EventWait(pVM, RT_INDEFINITE_WAIT, &pEvent);
859 if (VBOX_FAILURE(rc))
860 {
861 AssertMsgFailed(("Wait on detach command failed, rc=%d\n", rc));
862 return rc;
863 }
864
865 /*
866 * Destroy the ping-pong construct and return.
867 */
868 pVM->dbgf.s.fAttached = false;
869 RTThreadSleep(10);
870 rc = RTSemPingPongDestroy(&pVM->dbgf.s.PingPong);
871 AssertRC(rc);
872
873 return VINF_SUCCESS;
874}
875
876
877
878/**
879 * Wait for a debug event.
880 *
881 * @returns VBox status. Will not return VBOX_INTERRUPTED.
882 * @param pVM VM handle.
883 * @param cMillies Number of millies to wait.
884 * @param ppEvent Where to store the event pointer.
885 */
886DBGFR3DECL(int) DBGFR3EventWait(PVM pVM, unsigned cMillies, PCDBGFEVENT *ppEvent)
887{
888 /*
889 * Check state.
890 */
891 if (!pVM->dbgf.s.fAttached)
892 {
893 AssertMsgFailed(("Not attached to VM!\n"));
894 return VERR_DBGF_NOT_ATTACHED;
895 }
896 *ppEvent = NULL;
897
898 /*
899 * Wait.
900 */
901 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
902 if (VBOX_SUCCESS(rc))
903 {
904 *ppEvent = &pVM->dbgf.s.DbgEvent;
905 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
906 return VINF_SUCCESS;
907 }
908
909 return rc;
910}
911
912
913/**
914 * Halts VM execution.
915 *
916 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
917 * arrives. Until that time it's not possible to issue any new commands.
918 *
919 * @returns VBox status.
920 * @param pVM VM handle.
921 */
922DBGFR3DECL(int) DBGFR3Halt(PVM pVM)
923{
924 /*
925 * Check state.
926 */
927 if (!pVM->dbgf.s.fAttached)
928 {
929 AssertMsgFailed(("Not attached to VM!\n"));
930 return VERR_DBGF_NOT_ATTACHED;
931 }
932 if ( pVM->dbgf.s.PingPong.enmSpeaker == RTPINGPONGSPEAKER_PONG
933 || pVM->dbgf.s.PingPong.enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
934 return VWRN_DBGF_ALREADY_HALTED;
935
936 /*
937 * Send command.
938 */
939 dbgfr3SetCmd(pVM, DBGFCMD_HALT);
940
941 return VINF_SUCCESS;
942}
943
944
945/**
946 * Checks if the VM is halted by the debugger.
947 *
948 * @returns True if halted.
949 * @returns False if not halted.
950 * @param pVM VM handle.
951 */
952DBGFR3DECL(bool) DBGFR3IsHalted(PVM pVM)
953{
954 AssertMsg(pVM->dbgf.s.fAttached, ("Not attached to VM!\n"));
955
956 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
957 return pVM->dbgf.s.fAttached
958 && ( enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
959 || enmSpeaker == RTPINGPONGSPEAKER_PONG);
960}
961
962
963/**
964 * Checks if the the debugger can wait for events or not.
965 *
966 * This function is only used by lazy, multiplexing debuggers. :-)
967 *
968 * @returns True if waitable.
969 * @returns False if not waitable.
970 * @param pVM VM handle.
971 */
972DBGFR3DECL(bool) DBGFR3CanWait(PVM pVM)
973{
974 AssertMsg(pVM->dbgf.s.fAttached, ("Not attached to VM!\n"));
975
976 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
977 return pVM->dbgf.s.fAttached
978 && ( enmSpeaker == RTPINGPONGSPEAKER_PING
979 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED
980 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED);
981}
982
983
984/**
985 * Resumes VM execution.
986 *
987 * There is no receipt event on this command.
988 *
989 * @returns VBox status.
990 * @param pVM VM handle.
991 */
992DBGFR3DECL(int) DBGFR3Resume(PVM pVM)
993{
994 /*
995 * Check state.
996 */
997 if (!pVM->dbgf.s.fAttached)
998 {
999 AssertMsgFailed(("Not attached to VM!\n"));
1000 return VERR_DBGF_NOT_ATTACHED;
1001 }
1002 if (pVM->dbgf.s.PingPong.enmSpeaker != RTPINGPONGSPEAKER_PONG)
1003 {
1004 AssertMsgFailed(("Speaking out of turn!\n"));
1005 return VERR_SEM_OUT_OF_TURN;
1006 }
1007
1008 /*
1009 * Send the ping back to the emulation thread telling it to run.
1010 */
1011 dbgfr3SetCmd(pVM, DBGFCMD_GO);
1012 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1013 AssertRC(rc);
1014
1015 return rc;
1016}
1017
1018
1019/**
1020 * Step Into.
1021 *
1022 * A single step event is generated from this command.
1023 * The current implementation is not reliable, so don't rely on the event comming.
1024 *
1025 * @returns VBox status.
1026 * @param pVM VM handle.
1027 */
1028DBGFR3DECL(int) DBGFR3Step(PVM pVM)
1029{
1030 /*
1031 * Check state.
1032 */
1033 if (!pVM->dbgf.s.fAttached)
1034 {
1035 AssertMsgFailed(("Not attached to VM!\n"));
1036 return VERR_DBGF_NOT_ATTACHED;
1037 }
1038 if (pVM->dbgf.s.PingPong.enmSpeaker != RTPINGPONGSPEAKER_PONG)
1039 {
1040 AssertMsgFailed(("Speaking out of turn!\n"));
1041 return VERR_SEM_OUT_OF_TURN;
1042 }
1043
1044 /*
1045 * Send the ping back to the emulation thread telling it to run.
1046 */
1047 dbgfr3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1048 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1049 AssertRC(rc);
1050 return rc;
1051}
1052
1053
1054/**
1055 * Call this to single step rawmode or recompiled mode.
1056 *
1057 * You must pass down the return code to the EM loop! That's
1058 * where the actual single stepping take place (at least in the
1059 * current implementation).
1060 *
1061 * @returns VINF_EM_DBG_STEP
1062 * @thread EMT
1063 */
1064DBGFR3DECL(int) DBGFR3PrgStep(PVM pVM)
1065{
1066 VM_ASSERT_EMT(pVM);
1067
1068 pVM->dbgf.s.fSingleSteppingRaw = true;
1069 return VINF_EM_DBG_STEP;
1070}
1071
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