VirtualBox

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

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

VMM + VBox/cdefs.h: consolidated all the XYZ*DECLS of the VMM into VMM*DECL. Removed dead DECL and IN_XYZ* macros.

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