VirtualBox

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

Last change on this file since 1122 was 23, checked in by vboxsync, 18 years ago

string.h & stdio.h + header cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 29.3 KB
Line 
1/* $Id: DBGF.cpp 23 2007-01-15 14:08:28Z vboxsync $ */
2/** @file
3 * VMM DBGF - Debugger Facility.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
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 dbgfR3InfoTerm(pVM);
206 return VINF_SUCCESS;
207}
208
209
210/**
211 * Applies relocations to data and code managed by this
212 * component. This function will be called at init and
213 * whenever the VMM need to relocate it self inside the GC.
214 *
215 * @param pVM VM handle.
216 * @param offDelta Relocation delta relative to old location.
217 */
218DBGFR3DECL(void) DBGFR3Relocate(PVM pVM, RTGCINTPTR offDelta)
219{
220}
221
222
223/**
224 * Waits a little while for a debuggger to attach.
225 *
226 * @returns True is a debugger have attached.
227 * @param pVM VM handle.
228 * @param enmEvent Event.
229 */
230bool dbgfR3WaitForAttach(PVM pVM, DBGFEVENTTYPE enmEvent)
231{
232 /*
233 * First a message.
234 */
235 RTStrmPrintf(g_pStdErr, "DBGF: No debugger attached, waiting 15 seconds for one to attach (event=%d)\n", enmEvent);
236 RTStrmFlush(g_pStdErr);
237#ifdef DEBUG_sandervl
238 int cWait = 10;
239#else
240 int cWait = 150;
241#endif
242 while (cWait > 0)
243 {
244 RTThreadSleep(100);
245 if (pVM->dbgf.s.fAttached)
246 {
247 RTStrmPrintf(g_pStdErr, "Attached!\n");
248 RTStrmFlush(g_pStdErr);
249 return true;
250 }
251
252 /* next */
253 if (!(cWait % 10))
254 {
255 RTStrmPrintf(g_pStdErr, "%d.", cWait / 10);
256 RTStrmFlush(g_pStdErr);
257 }
258 cWait--;
259 }
260
261 RTStrmPrintf(g_pStdErr, "Stopping the VM!\n");
262 RTStrmFlush(g_pStdErr);
263 return false;
264}
265
266
267/**
268 * Forced action callback.
269 * The VMM will call this from it's main loop when VM_FF_DBGF is set.
270 *
271 * The function checks and executes pending commands from the debugger.
272 *
273 * @returns VINF_SUCCESS normally.
274 * @returns VERR_DBGF_RAISE_FATAL_ERROR to pretend a fatal error happend.
275 * @param pVM VM Handle.
276 */
277DBGFR3DECL(int) DBGFR3VMMForcedAction(PVM pVM)
278{
279 /*
280 * Clear the FF DBGF request flag.
281 */
282 Assert(pVM->fForcedActions & VM_FF_DBGF);
283 VM_FF_CLEAR(pVM, VM_FF_DBGF);
284
285 /*
286 * Commands?
287 */
288 int rc = VINF_SUCCESS;
289 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
290 {
291 /** @todo stupid GDT/LDT sync hack. go away! */
292 SELMR3UpdateFromCPUM(pVM);
293
294 /*
295 * Process the command.
296 */
297 bool fResumeExecution;
298 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
299 DBGFCMD enmCmd = dbgfr3SetCmd(pVM, DBGFCMD_NO_COMMAND);
300 rc = dbgfr3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
301 if (!fResumeExecution)
302 rc = dbgfr3VMMWait(pVM);
303 }
304 return rc;
305}
306
307
308/**
309 * Flag whether the event implies that we're stopped in the hypervisor code
310 * and have to block certain operations.
311 *
312 * @param pVM The VM handle.
313 * @param enmEvent The event.
314 */
315static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
316{
317 switch (enmEvent)
318 {
319 case DBGFEVENT_STEPPED_HYPER:
320 case DBGFEVENT_ASSERTION_HYPER:
321 case DBGFEVENT_BREAKPOINT_HYPER:
322 pVM->dbgf.s.fStoppedInHyper = true;
323 break;
324 default:
325 pVM->dbgf.s.fStoppedInHyper = false;
326 break;
327 }
328}
329
330
331/**
332 * Try determin the event context.
333 *
334 * @returns debug event context.
335 * @param pVM The VM handle.
336 */
337static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
338{
339 switch (EMGetState(pVM))
340 {
341 case EMSTATE_RAW:
342 case EMSTATE_DEBUG_GUEST_RAW:
343 return DBGFEVENTCTX_RAW;
344
345 case EMSTATE_REM:
346 case EMSTATE_DEBUG_GUEST_REM:
347 return DBGFEVENTCTX_REM;
348
349 case EMSTATE_DEBUG_HYPER:
350 case EMSTATE_GURU_MEDITATION:
351 return DBGFEVENTCTX_HYPER;
352
353 default:
354 return DBGFEVENTCTX_OTHER;
355 }
356}
357
358/**
359 * The common event prologue code.
360 * It will set the 'stopped-in-hyper' flag, make sure someone's attach,
361 * and perhaps process any high priority pending actions (none yet).
362 *
363 * @returns VBox status.
364 * @param pVM The VM handle.
365 * @param enmEvent The event to be sent.
366 */
367static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
368{
369 /*
370 * Check if a debugger is attached.
371 */
372 if ( !pVM->dbgf.s.fAttached
373 && !dbgfR3WaitForAttach(pVM, enmEvent))
374 {
375 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
376 return VERR_DBGF_NOT_ATTACHED;
377 }
378
379 /*
380 * Sync back the state from the REM.
381 */
382 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
383 if (!pVM->dbgf.s.fStoppedInHyper)
384 REMR3StateUpdate(pVM);
385
386 /*
387 * Look thru pending commands and finish those which make sense now.
388 */
389 /** @todo Process/purge pending commands. */
390 //int rc = DBGFR3VMMForcedAction(pVM);
391 return VINF_SUCCESS;
392}
393
394
395/**
396 * Sends the event in the event buffer.
397 *
398 * @returns VBox status code.
399 * @param pVM The VM handle.
400 */
401static int dbgfR3SendEvent(PVM pVM)
402{
403 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
404 if (VBOX_SUCCESS(rc))
405 rc = dbgfr3VMMWait(pVM);
406
407 pVM->dbgf.s.fStoppedInHyper = false;
408 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
409 return rc;
410}
411
412
413/**
414 * Send a generic debugger event which takes no data.
415 *
416 * @returns VBox status.
417 * @param pVM The VM handle.
418 * @param enmEvent The event to send.
419 */
420DBGFR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
421{
422 int rc = dbgfR3EventPrologue(pVM, enmEvent);
423 if (VBOX_FAILURE(rc))
424 return rc;
425
426 /*
427 * Send the event and process the reply communication.
428 */
429 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
430 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
431 return dbgfR3SendEvent(pVM);
432}
433
434
435/**
436 * Send a debugger event which takes the full source file location.
437 *
438 * @returns VBox status.
439 * @param pVM The VM handle.
440 * @param enmEvent The event to send.
441 * @param pszFile Source file.
442 * @param uLine Line number in source file.
443 * @param pszFunction Function name.
444 * @param pszFormat Message which accompanies the event.
445 * @param ... Message arguments.
446 */
447DBGFR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
448{
449 va_list args;
450 va_start(args, pszFormat);
451 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
452 va_end(args);
453 return rc;
454}
455
456
457/**
458 * Send a debugger event which takes the full source file location.
459 *
460 * @returns VBox status.
461 * @param pVM The VM handle.
462 * @param enmEvent The event to send.
463 * @param pszFile Source file.
464 * @param uLine Line number in source file.
465 * @param pszFunction Function name.
466 * @param pszFormat Message which accompanies the event.
467 * @param args Message arguments.
468 */
469DBGFR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
470{
471 int rc = dbgfR3EventPrologue(pVM, enmEvent);
472 if (VBOX_FAILURE(rc))
473 return rc;
474
475 /*
476 * Format the message.
477 */
478 char *pszMessage = NULL;
479 char szMessage[8192];
480 if (pszFormat && *pszFormat)
481 {
482 pszMessage = &szMessage[0];
483 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
484 }
485
486 /*
487 * Send the event and process the reply communication.
488 */
489 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
490 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
491 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
492 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
493 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
494 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
495 return dbgfR3SendEvent(pVM);
496}
497
498
499/**
500 * Send a debugger event which takes the two assertion messages.
501 *
502 * @returns VBox status.
503 * @param pVM The VM handle.
504 * @param enmEvent The event to send.
505 * @param pszMsg1 First assertion message.
506 * @param pszMsg2 Second assertion message.
507 */
508DBGFR3DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
509{
510 int rc = dbgfR3EventPrologue(pVM, enmEvent);
511 if (VBOX_FAILURE(rc))
512 return rc;
513
514 /*
515 * Send the event and process the reply communication.
516 */
517 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
518 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
519 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg1 = pszMsg1;
520 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
521 return dbgfR3SendEvent(pVM);
522}
523
524
525/**
526 * Breakpoint was hit somewhere.
527 * Figure out which breakpoint it is and notify the debugger.
528 *
529 * @returns VBox status.
530 * @param pVM The VM handle.
531 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
532 */
533DBGFR3DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
534{
535 int rc = dbgfR3EventPrologue(pVM, enmEvent);
536 if (VBOX_FAILURE(rc))
537 return rc;
538
539 /*
540 * Send the event and process the reply communication.
541 */
542 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
543 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVM->dbgf.s.iActiveBp;
544 pVM->dbgf.s.iActiveBp = ~0U;
545 if (iBp != ~0U)
546 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
547 else
548 {
549 /* REM breakpoints has be been searched for. */
550#if 0 /** @todo get flat PC api! */
551 uint32_t eip = CPUMGetGuestEIP(pVM);
552#else
553 PCPUMCTX pCtx;
554 CPUMQueryGuestCtxPtr(pVM, &pCtx);
555 uint32_t eip = pCtx->eip + pCtx->csHid.u32Base;
556#endif
557 for (iBp = 0; iBp < ELEMENTS(pVM->dbgf.s.aBreakpoints); iBp++)
558 if ( pVM->dbgf.s.aBreakpoints[iBp].enmType == DBGFBPTYPE_REM
559 && pVM->dbgf.s.aBreakpoints[iBp].GCPtr == eip)
560 {
561 pVM->dbgf.s.DbgEvent.u.Bp.iBp = iBp;
562 break;
563 }
564 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
565 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
566 }
567 return dbgfR3SendEvent(pVM);
568}
569
570
571/**
572 * Waits for the debugger to respond.
573 *
574 * @returns VBox status. (clearify)
575 * @param pVM VM handle.
576 */
577static int dbgfr3VMMWait(PVM pVM)
578{
579 LogFlow(("dbgfr3VMMWait:\n"));
580
581 /** @todo stupid GDT/LDT sync hack. go away! */
582 SELMR3UpdateFromCPUM(pVM);
583 int rcRet = VINF_SUCCESS;
584
585 /*
586 * Waits for the debugger to reply (i.e. issue an command).
587 */
588 for (;;)
589 {
590 /*
591 * Wait.
592 */
593 for (;;)
594 {
595 int rc = RTSemPingWait(&pVM->dbgf.s.PingPong, 250);
596 if (VBOX_SUCCESS(rc))
597 break;
598 if (rc != VERR_TIMEOUT)
599 {
600 LogFlow(("dbgfr3VMMWait: returns %Vrc\n", rc));
601 return rc;
602 }
603
604 if (VM_FF_ISSET(pVM, VM_FF_REQUEST))
605 {
606 LogFlow(("dbgfr3VMMWait: Processes requests...\n"));
607 rc = VMR3ReqProcess(pVM);
608 LogFlow(("dbgfr3VMMWait: VMR3ReqProcess -> %Vrc rcRet=%Vrc\n", rc, rcRet));
609 if (rc >= VINF_EM_FIRST && rc < VINF_EM_LAST)
610 {
611 switch (rc)
612 {
613 case VINF_EM_DBG_BREAKPOINT:
614 case VINF_EM_DBG_STEPPED:
615 case VINF_EM_DBG_STEP:
616 case VINF_EM_DBG_STOP:
617 AssertMsgFailed(("rc=%Vrc\n", rc));
618 break;
619
620 /* return straight away */
621 case VINF_EM_TERMINATE:
622 case VINF_EM_OFF:
623 LogFlow(("dbgfr3VMMWait: returns %Vrc\n", rc));
624 return rc;
625
626 /* remember return code. */
627 default:
628 AssertReleaseMsgFailed(("rc=%Vrc is not in the switch!\n", rc));
629 case VINF_EM_RESET:
630 case VINF_EM_SUSPEND:
631 case VINF_EM_HALT:
632 case VINF_EM_RESUME:
633 case VINF_EM_RESCHEDULE:
634 case VINF_EM_RESCHEDULE_REM:
635 case VINF_EM_RESCHEDULE_RAW:
636 if (rc < rcRet || rcRet == VINF_SUCCESS)
637 rcRet = rc;
638 break;
639 }
640 }
641 else if (VBOX_FAILURE(rc))
642 {
643 LogFlow(("dbgfr3VMMWait: returns %Vrc\n", rc));
644 return rc;
645 }
646 }
647 }
648
649 /*
650 * Process the command.
651 */
652 bool fResumeExecution;
653 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
654 DBGFCMD enmCmd = dbgfr3SetCmd(pVM, DBGFCMD_NO_COMMAND);
655 int rc = dbgfr3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
656 if (fResumeExecution)
657 {
658 if (VBOX_FAILURE(rc))
659 rcRet = rc;
660 else if ( rc >= VINF_EM_FIRST
661 && rc < VINF_EM_LAST
662 && (rc < rcRet || rcRet == VINF_SUCCESS))
663 rcRet = rc;
664 LogFlow(("dbgfr3VMMWait: returns %Vrc\n", rcRet));
665 return rcRet;
666 }
667 }
668}
669
670
671/**
672 * Executes command from debugger.
673 * The caller is responsible for waiting or resuming execution based on the
674 * value returned in the *pfResumeExecution indicator.
675 *
676 * @returns VBox status. (clearify!)
677 * @param pVM VM Handle.
678 * @param enmCmd The command in question.
679 * @param pCmdData Pointer to the command data.
680 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
681 */
682static int dbgfr3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
683{
684 bool fSendEvent;
685 bool fResume;
686 int rc = VINF_SUCCESS;
687
688 switch (enmCmd)
689 {
690 /*
691 * Halt is answered by an event say that we've halted.
692 */
693 case DBGFCMD_HALT:
694 {
695 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
696 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
697 fSendEvent = true;
698 fResume = false;
699 break;
700 }
701
702
703 /*
704 * Resume is not answered we'll just resume execution.
705 */
706 case DBGFCMD_GO:
707 {
708 fSendEvent = false;
709 fResume = true;
710 break;
711 }
712
713 /** @todo implement (and define) the rest of the commands. */
714
715 /*
716 * Disable breakpoints and stuff.
717 * Send an everythings cool event to the debugger thread and resume execution.
718 */
719 case DBGFCMD_DETACH_DEBUGGER:
720 {
721 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
722 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
723 VM_FF_CLEAR(pVM, VM_FF_DBGF);
724 pVM->dbgf.s.fAttached = false;
725 fSendEvent = true;
726 fResume = true;
727 break;
728 }
729
730 /*
731 * Single step, with trace into.
732 */
733 case DBGFCMD_SINGLE_STEP:
734 {
735 Log2(("Single step\n"));
736 rc = VINF_EM_DBG_STEP;
737 pVM->dbgf.s.fSingleSteppingRaw = true;
738 fSendEvent = false;
739 fResume = true;
740 break;
741 }
742
743 /*
744 * Default is to send an invalid command event.
745 */
746 default:
747 {
748 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
749 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
750 fSendEvent = true;
751 fResume = false;
752 break;
753 }
754 }
755
756 /*
757 * Send pending event.
758 */
759 if (fSendEvent)
760 {
761 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
762 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
763 if (VBOX_FAILURE(rc2))
764 {
765 AssertRC(rc2);
766 *pfResumeExecution = true;
767 return rc2;
768 }
769 }
770
771 /*
772 * Return.
773 */
774 *pfResumeExecution = fResume;
775 return rc;
776}
777
778
779
780
781/**
782 * Attaches a debugger to the specified VM.
783 *
784 * Only one debugger at a time.
785 *
786 * @returns VBox status code.
787 * @param pVM VM Handle.
788 */
789DBGFR3DECL(int) DBGFR3Attach(PVM pVM)
790{
791 /*
792 * Check if already attached.
793 */
794 if (pVM->dbgf.s.fAttached)
795 {
796 AssertMsgFailed(("Already attached\n"));
797 return VERR_DBGF_ALREADY_ATTACHED;
798 }
799
800 /*
801 * Create the Ping-Pong structure.
802 */
803 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
804 if (rc)
805 return rc;
806
807 /*
808 * Set the attached flag.
809 */
810 pVM->dbgf.s.fAttached = true;
811 return VINF_SUCCESS;
812}
813
814
815/**
816 * Detaches a debugger from the specified VM.
817 *
818 * Caller must be attached to the VM.
819 *
820 * @returns VBox status code.
821 * @param pVM VM Handle.
822 */
823DBGFR3DECL(int) DBGFR3Detach(PVM pVM)
824{
825 /*
826 * Check if attached.
827 */
828 if (!pVM->dbgf.s.fAttached)
829 {
830 AssertMsgFailed(("Not attached to VM!\n"));
831 return VERR_DBGF_NOT_ATTACHED;
832 }
833
834 /*
835 * Send detach command.
836 */
837 if (pVM->dbgf.s.PingPong.enmSpeaker == RTPINGPONGSPEAKER_PONG)
838 {
839 dbgfr3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
840 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
841 if (VBOX_FAILURE(rc))
842 {
843 AssertMsgFailed(("Failed to signal emulation thread. rc=%d\n", rc));
844 return rc;
845 }
846 }
847 else
848 dbgfr3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
849
850 /*
851 * Wait for the ok event.
852 */
853 PCDBGFEVENT pEvent;
854 int rc = DBGFR3EventWait(pVM, RT_INDEFINITE_WAIT, &pEvent);
855 if (VBOX_FAILURE(rc))
856 {
857 AssertMsgFailed(("Wait on detach command failed, rc=%d\n", rc));
858 return rc;
859 }
860
861 /*
862 * Destroy the ping-pong construct and return.
863 */
864 pVM->dbgf.s.fAttached = false;
865 RTThreadSleep(10);
866 rc = RTSemPingPongDestroy(&pVM->dbgf.s.PingPong);
867 AssertRC(rc);
868
869 return VINF_SUCCESS;
870}
871
872
873
874/**
875 * Wait for a debug event.
876 *
877 * @returns VBox status. Will not return VBOX_INTERRUPTED.
878 * @param pVM VM handle.
879 * @param cMillies Number of millies to wait.
880 * @param ppEvent Where to store the event pointer.
881 */
882DBGFR3DECL(int) DBGFR3EventWait(PVM pVM, unsigned cMillies, PCDBGFEVENT *ppEvent)
883{
884 /*
885 * Check state.
886 */
887 if (!pVM->dbgf.s.fAttached)
888 {
889 AssertMsgFailed(("Not attached to VM!\n"));
890 return VERR_DBGF_NOT_ATTACHED;
891 }
892 *ppEvent = NULL;
893
894 /*
895 * Wait.
896 */
897 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
898 if (VBOX_SUCCESS(rc))
899 {
900 *ppEvent = &pVM->dbgf.s.DbgEvent;
901 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
902 return VINF_SUCCESS;
903 }
904
905 return rc;
906}
907
908
909/**
910 * Halts VM execution.
911 *
912 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
913 * arrives. Until that time it's not possible to issue any new commands.
914 *
915 * @returns VBox status.
916 * @param pVM VM handle.
917 */
918DBGFR3DECL(int) DBGFR3Halt(PVM pVM)
919{
920 /*
921 * Check state.
922 */
923 if (!pVM->dbgf.s.fAttached)
924 {
925 AssertMsgFailed(("Not attached to VM!\n"));
926 return VERR_DBGF_NOT_ATTACHED;
927 }
928 if ( pVM->dbgf.s.PingPong.enmSpeaker == RTPINGPONGSPEAKER_PONG
929 || pVM->dbgf.s.PingPong.enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
930 return VWRN_DBGF_ALREADY_HALTED;
931
932 /*
933 * Send command.
934 */
935 dbgfr3SetCmd(pVM, DBGFCMD_HALT);
936
937 return VINF_SUCCESS;
938}
939
940
941/**
942 * Checks if the VM is halted by the debugger.
943 *
944 * @returns True if halted.
945 * @returns False if not halted.
946 * @param pVM VM handle.
947 */
948DBGFR3DECL(bool) DBGFR3IsHalted(PVM pVM)
949{
950 AssertMsg(pVM->dbgf.s.fAttached, ("Not attached to VM!\n"));
951
952 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
953 return pVM->dbgf.s.fAttached
954 && ( enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
955 || enmSpeaker == RTPINGPONGSPEAKER_PONG);
956}
957
958
959/**
960 * Checks if the the debugger can wait for events or not.
961 *
962 * This function is only used by lazy, multiplexing debuggers. :-)
963 *
964 * @returns True if waitable.
965 * @returns False if not waitable.
966 * @param pVM VM handle.
967 */
968DBGFR3DECL(bool) DBGFR3CanWait(PVM pVM)
969{
970 AssertMsg(pVM->dbgf.s.fAttached, ("Not attached to VM!\n"));
971
972 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
973 return pVM->dbgf.s.fAttached
974 && ( enmSpeaker == RTPINGPONGSPEAKER_PING
975 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED
976 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED);
977}
978
979
980/**
981 * Resumes VM execution.
982 *
983 * There is no receipt event on this command.
984 *
985 * @returns VBox status.
986 * @param pVM VM handle.
987 */
988DBGFR3DECL(int) DBGFR3Resume(PVM pVM)
989{
990 /*
991 * Check state.
992 */
993 if (!pVM->dbgf.s.fAttached)
994 {
995 AssertMsgFailed(("Not attached to VM!\n"));
996 return VERR_DBGF_NOT_ATTACHED;
997 }
998 if (pVM->dbgf.s.PingPong.enmSpeaker != RTPINGPONGSPEAKER_PONG)
999 {
1000 AssertMsgFailed(("Speaking out of turn!\n"));
1001 return VERR_SEM_OUT_OF_TURN;
1002 }
1003
1004 /*
1005 * Send the ping back to the emulation thread telling it to run.
1006 */
1007 dbgfr3SetCmd(pVM, DBGFCMD_GO);
1008 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1009 AssertRC(rc);
1010
1011 return rc;
1012}
1013
1014
1015/**
1016 * Step Into.
1017 *
1018 * A single step event is generated from this command.
1019 * The current implementation is not reliable, so don't rely on the event comming.
1020 *
1021 * @returns VBox status.
1022 * @param pVM VM handle.
1023 */
1024DBGFR3DECL(int) DBGFR3Step(PVM pVM)
1025{
1026 /*
1027 * Check state.
1028 */
1029 if (!pVM->dbgf.s.fAttached)
1030 {
1031 AssertMsgFailed(("Not attached to VM!\n"));
1032 return VERR_DBGF_NOT_ATTACHED;
1033 }
1034 if (pVM->dbgf.s.PingPong.enmSpeaker != RTPINGPONGSPEAKER_PONG)
1035 {
1036 AssertMsgFailed(("Speaking out of turn!\n"));
1037 return VERR_SEM_OUT_OF_TURN;
1038 }
1039
1040 /*
1041 * Send the ping back to the emulation thread telling it to run.
1042 */
1043 dbgfr3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1044 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1045 AssertRC(rc);
1046 return rc;
1047}
1048
1049
1050/**
1051 * Call this to single step rawmode or recompiled mode.
1052 *
1053 * You must pass down the return code to the EM loop! That's
1054 * where the actual single stepping take place (at least in the
1055 * current implementation).
1056 *
1057 * @returns VINF_EM_DBG_STEP
1058 * @thread EMT
1059 */
1060DBGFR3DECL(int) DBGFR3PrgStep(PVM pVM)
1061{
1062 VM_ASSERT_EMT(pVM);
1063
1064 pVM->dbgf.s.fSingleSteppingRaw = true;
1065 return VINF_EM_DBG_STEP;
1066}
1067
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