VirtualBox

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

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

DBGF: don't bother waiting for attach assertions and errors when in HWACCM mode.

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