VirtualBox

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

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

VMM,VBoxDbg: SMP refactoring, part 1.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 31.0 KB
Line 
1/* $Id: DBGF.cpp 19286 2009-05-01 12:41:07Z 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 VMR3NotifyGlobalFF(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 PVMCPU pVCpu = VMMGetCpu(pVM);
294
295 /*
296 * Clear the FF DBGF request flag.
297 */
298 Assert(pVM->fGlobalForcedActions & VM_FF_DBGF);
299 VM_FF_CLEAR(pVM, VM_FF_DBGF);
300
301 /*
302 * Commands?
303 */
304 int rc = VINF_SUCCESS;
305 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
306 {
307 /** @todo stupid GDT/LDT sync hack. go away! */
308 SELMR3UpdateFromCPUM(pVM, pVCpu);
309
310 /*
311 * Process the command.
312 */
313 bool fResumeExecution;
314 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
315 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
316 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
317 if (!fResumeExecution)
318 rc = dbgfR3VMMWait(pVM);
319 }
320 return rc;
321}
322
323
324/**
325 * Flag whether the event implies that we're stopped in the hypervisor code
326 * and have to block certain operations.
327 *
328 * @param pVM The VM handle.
329 * @param enmEvent The event.
330 */
331static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
332{
333 switch (enmEvent)
334 {
335 case DBGFEVENT_STEPPED_HYPER:
336 case DBGFEVENT_ASSERTION_HYPER:
337 case DBGFEVENT_BREAKPOINT_HYPER:
338 pVM->dbgf.s.fStoppedInHyper = true;
339 break;
340 default:
341 pVM->dbgf.s.fStoppedInHyper = false;
342 break;
343 }
344}
345
346
347/**
348 * Try to determine the event context.
349 *
350 * @returns debug event context.
351 * @param pVM The VM handle.
352 */
353static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
354{
355 /** @todo SMP support! */
356 PVMCPU pVCpu = &pVM->aCpus[0];
357
358 switch (EMGetState(pVCpu))
359 {
360 case EMSTATE_RAW:
361 case EMSTATE_DEBUG_GUEST_RAW:
362 return DBGFEVENTCTX_RAW;
363
364 case EMSTATE_REM:
365 case EMSTATE_DEBUG_GUEST_REM:
366 return DBGFEVENTCTX_REM;
367
368 case EMSTATE_DEBUG_HYPER:
369 case EMSTATE_GURU_MEDITATION:
370 return DBGFEVENTCTX_HYPER;
371
372 default:
373 return DBGFEVENTCTX_OTHER;
374 }
375}
376
377/**
378 * The common event prologue code.
379 * It will set the 'stopped-in-hyper' flag, make sure someone's attach,
380 * and perhaps process any high priority pending actions (none yet).
381 *
382 * @returns VBox status.
383 * @param pVM The VM handle.
384 * @param enmEvent The event to be sent.
385 */
386static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
387{
388 /** @todo SMP */
389 PVMCPU pVCpu = VMMGetCpu(pVM);
390
391 /*
392 * Check if a debugger is attached.
393 */
394 if ( !pVM->dbgf.s.fAttached
395 && !dbgfR3WaitForAttach(pVM, enmEvent))
396 {
397 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
398 return VERR_DBGF_NOT_ATTACHED;
399 }
400
401 /*
402 * Sync back the state from the REM.
403 */
404 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
405 if (!pVM->dbgf.s.fStoppedInHyper)
406 REMR3StateUpdate(pVM, pVCpu);
407
408 /*
409 * Look thru pending commands and finish those which make sense now.
410 */
411 /** @todo Process/purge pending commands. */
412 //int rc = DBGFR3VMMForcedAction(pVM);
413 return VINF_SUCCESS;
414}
415
416
417/**
418 * Sends the event in the event buffer.
419 *
420 * @returns VBox status code.
421 * @param pVM The VM handle.
422 */
423static int dbgfR3SendEvent(PVM pVM)
424{
425 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
426 if (RT_SUCCESS(rc))
427 rc = dbgfR3VMMWait(pVM);
428
429 pVM->dbgf.s.fStoppedInHyper = false;
430 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
431 return rc;
432}
433
434
435/**
436 * Send a generic debugger event which takes no data.
437 *
438 * @returns VBox status.
439 * @param pVM The VM handle.
440 * @param enmEvent The event to send.
441 */
442VMMR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
443{
444 int rc = dbgfR3EventPrologue(pVM, enmEvent);
445 if (RT_FAILURE(rc))
446 return rc;
447
448 /*
449 * Send the event and process the reply communication.
450 */
451 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
452 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
453 return dbgfR3SendEvent(pVM);
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 ... Message arguments.
468 */
469VMMR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
470{
471 va_list args;
472 va_start(args, pszFormat);
473 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
474 va_end(args);
475 return rc;
476}
477
478
479/**
480 * Send a debugger event which takes the full source file location.
481 *
482 * @returns VBox status.
483 * @param pVM The VM handle.
484 * @param enmEvent The event to send.
485 * @param pszFile Source file.
486 * @param uLine Line number in source file.
487 * @param pszFunction Function name.
488 * @param pszFormat Message which accompanies the event.
489 * @param args Message arguments.
490 */
491VMMR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
492{
493 int rc = dbgfR3EventPrologue(pVM, enmEvent);
494 if (RT_FAILURE(rc))
495 return rc;
496
497 /*
498 * Format the message.
499 */
500 char *pszMessage = NULL;
501 char szMessage[8192];
502 if (pszFormat && *pszFormat)
503 {
504 pszMessage = &szMessage[0];
505 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
506 }
507
508 /*
509 * Send the event and process the reply communication.
510 */
511 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
512 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
513 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
514 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
515 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
516 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
517 return dbgfR3SendEvent(pVM);
518}
519
520
521/**
522 * Send a debugger event which takes the two assertion messages.
523 *
524 * @returns VBox status.
525 * @param pVM The VM handle.
526 * @param enmEvent The event to send.
527 * @param pszMsg1 First assertion message.
528 * @param pszMsg2 Second assertion message.
529 */
530VMMR3DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
531{
532 int rc = dbgfR3EventPrologue(pVM, enmEvent);
533 if (RT_FAILURE(rc))
534 return rc;
535
536 /*
537 * Send the event and process the reply communication.
538 */
539 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
540 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
541 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg1 = pszMsg1;
542 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
543 return dbgfR3SendEvent(pVM);
544}
545
546
547/**
548 * Breakpoint was hit somewhere.
549 * Figure out which breakpoint it is and notify the debugger.
550 *
551 * @returns VBox status.
552 * @param pVM The VM handle.
553 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
554 */
555VMMR3DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
556{
557 int rc = dbgfR3EventPrologue(pVM, enmEvent);
558 if (RT_FAILURE(rc))
559 return rc;
560
561 /*
562 * Send the event and process the reply communication.
563 */
564 /** @todo SMP */
565 PVMCPU pVCpu = VMMGetCpu0(pVM);
566
567 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
568 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVCpu->dbgf.s.iActiveBp;
569 pVCpu->dbgf.s.iActiveBp = ~0U;
570 if (iBp != ~0U)
571 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
572 else
573 {
574 /* REM breakpoints has be been searched for. */
575#if 0 /** @todo get flat PC api! */
576 uint32_t eip = CPUMGetGuestEIP(pVM);
577#else
578 /* @todo SMP support!! */
579 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(VMMGetCpu(pVM));
580 RTGCPTR eip = pCtx->rip + pCtx->csHid.u64Base;
581#endif
582 for (iBp = 0; iBp < RT_ELEMENTS(pVM->dbgf.s.aBreakpoints); iBp++)
583 if ( pVM->dbgf.s.aBreakpoints[iBp].enmType == DBGFBPTYPE_REM
584 && pVM->dbgf.s.aBreakpoints[iBp].GCPtr == eip)
585 {
586 pVM->dbgf.s.DbgEvent.u.Bp.iBp = iBp;
587 break;
588 }
589 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
590 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
591 }
592 return dbgfR3SendEvent(pVM);
593}
594
595
596/**
597 * Waits for the debugger to respond.
598 *
599 * @returns VBox status. (clearify)
600 * @param pVM VM handle.
601 */
602static int dbgfR3VMMWait(PVM pVM)
603{
604 PVMCPU pVCpu = VMMGetCpu(pVM);
605
606 LogFlow(("dbgfR3VMMWait:\n"));
607
608 /** @todo stupid GDT/LDT sync hack. go away! */
609 SELMR3UpdateFromCPUM(pVM, pVCpu);
610 int rcRet = VINF_SUCCESS;
611
612 /*
613 * Waits for the debugger to reply (i.e. issue an command).
614 */
615 for (;;)
616 {
617 /*
618 * Wait.
619 */
620 for (;;)
621 {
622 int rc = RTSemPingWait(&pVM->dbgf.s.PingPong, 250);
623 if (RT_SUCCESS(rc))
624 break;
625 if (rc != VERR_TIMEOUT)
626 {
627 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
628 return rc;
629 }
630
631 if (VM_FF_ISSET(pVM, VM_FF_REQUEST))
632 {
633 LogFlow(("dbgfR3VMMWait: Processes requests...\n"));
634 rc = VMR3ReqProcessU(pVM->pUVM, VMREQDEST_ANY);
635 LogFlow(("dbgfR3VMMWait: VMR3ReqProcess -> %Rrc rcRet=%Rrc\n", rc, rcRet));
636 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
637 {
638 switch (rc)
639 {
640 case VINF_EM_DBG_BREAKPOINT:
641 case VINF_EM_DBG_STEPPED:
642 case VINF_EM_DBG_STEP:
643 case VINF_EM_DBG_STOP:
644 AssertMsgFailed(("rc=%Rrc\n", rc));
645 break;
646
647 /* return straight away */
648 case VINF_EM_TERMINATE:
649 case VINF_EM_OFF:
650 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
651 return rc;
652
653 /* remember return code. */
654 default:
655 AssertReleaseMsgFailed(("rc=%Rrc is not in the switch!\n", rc));
656 case VINF_EM_RESET:
657 case VINF_EM_SUSPEND:
658 case VINF_EM_HALT:
659 case VINF_EM_RESUME:
660 case VINF_EM_RESCHEDULE:
661 case VINF_EM_RESCHEDULE_REM:
662 case VINF_EM_RESCHEDULE_RAW:
663 if (rc < rcRet || rcRet == VINF_SUCCESS)
664 rcRet = rc;
665 break;
666 }
667 }
668 else if (RT_FAILURE(rc))
669 {
670 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
671 return rc;
672 }
673 }
674 }
675
676 /*
677 * Process the command.
678 */
679 bool fResumeExecution;
680 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
681 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
682 int rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
683 if (fResumeExecution)
684 {
685 if (RT_FAILURE(rc))
686 rcRet = rc;
687 else if ( rc >= VINF_EM_FIRST
688 && rc <= VINF_EM_LAST
689 && (rc < rcRet || rcRet == VINF_SUCCESS))
690 rcRet = rc;
691 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rcRet));
692 return rcRet;
693 }
694 }
695}
696
697
698/**
699 * Executes command from debugger.
700 * The caller is responsible for waiting or resuming execution based on the
701 * value returned in the *pfResumeExecution indicator.
702 *
703 * @returns VBox status. (clearify!)
704 * @param pVM VM Handle.
705 * @param enmCmd The command in question.
706 * @param pCmdData Pointer to the command data.
707 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
708 */
709static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
710{
711 bool fSendEvent;
712 bool fResume;
713 int rc = VINF_SUCCESS;
714
715 switch (enmCmd)
716 {
717 /*
718 * Halt is answered by an event say that we've halted.
719 */
720 case DBGFCMD_HALT:
721 {
722 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
723 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
724 fSendEvent = true;
725 fResume = false;
726 break;
727 }
728
729
730 /*
731 * Resume is not answered we'll just resume execution.
732 */
733 case DBGFCMD_GO:
734 {
735 fSendEvent = false;
736 fResume = true;
737 break;
738 }
739
740 /** @todo implement (and define) the rest of the commands. */
741
742 /*
743 * Disable breakpoints and stuff.
744 * Send an everythings cool event to the debugger thread and resume execution.
745 */
746 case DBGFCMD_DETACH_DEBUGGER:
747 {
748 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
749 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
750 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
751 fSendEvent = true;
752 fResume = true;
753 break;
754 }
755
756 /*
757 * The debugger has detached successfully.
758 * There is no reply to this event.
759 */
760 case DBGFCMD_DETACHED_DEBUGGER:
761 {
762 fSendEvent = false;
763 fResume = true;
764 break;
765 }
766
767 /*
768 * Single step, with trace into.
769 */
770 case DBGFCMD_SINGLE_STEP:
771 {
772 Log2(("Single step\n"));
773 rc = VINF_EM_DBG_STEP;
774 /** @todo SMP */
775 PVMCPU pVCpu = VMMGetCpu0(pVM);
776 pVCpu->dbgf.s.fSingleSteppingRaw = true;
777 fSendEvent = false;
778 fResume = true;
779 break;
780 }
781
782 /*
783 * Default is to send an invalid command event.
784 */
785 default:
786 {
787 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
788 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
789 fSendEvent = true;
790 fResume = false;
791 break;
792 }
793 }
794
795 /*
796 * Send pending event.
797 */
798 if (fSendEvent)
799 {
800 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
801 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
802 if (RT_FAILURE(rc2))
803 {
804 AssertRC(rc2);
805 *pfResumeExecution = true;
806 return rc2;
807 }
808 }
809
810 /*
811 * Return.
812 */
813 *pfResumeExecution = fResume;
814 return rc;
815}
816
817
818/**
819 * Attaches a debugger to the specified VM.
820 *
821 * Only one debugger at a time.
822 *
823 * @returns VBox status code.
824 * @param pVM VM Handle.
825 */
826VMMR3DECL(int) DBGFR3Attach(PVM pVM)
827{
828 /*
829 * Some validations first.
830 */
831 if (!VALID_PTR(pVM))
832 {
833 Log(("DBGFR3Attach: bad VM handle: %p\n", pVM));
834 return VERR_INVALID_HANDLE;
835 }
836 VMSTATE enmVMState = pVM->enmVMState;
837 if ( enmVMState >= VMSTATE_DESTROYING
838 || enmVMState < VMSTATE_CREATING)
839 {
840 Log(("DBGFR3Attach: Invalid VM state: %s\n", VMGetStateName(enmVMState)));
841 return VERR_INVALID_HANDLE;
842 }
843
844 /*
845 * Call the VM, use EMT for serialization.
846 */
847 PVMREQ pReq;
848 int rc = VMR3ReqCall(pVM, VMREQDEST_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)dbgfR3Attach, 1, pVM);
849 if (RT_SUCCESS(rc))
850 rc = pReq->iStatus;
851 VMR3ReqFree(pReq);
852
853 return rc;
854}
855
856
857/**
858 * EMT worker for DBGFR3Attach.
859 *
860 * @returns VBox status code.
861 * @param pVM Pointer to the shared VM structure.
862 */
863static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
864{
865 if (pVM->dbgf.s.fAttached)
866 {
867 Log(("dbgR3Attach: Debugger already attached\n"));
868 return VERR_DBGF_ALREADY_ATTACHED;
869 }
870
871 /*
872 * Create the Ping-Pong structure.
873 */
874 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
875 AssertRCReturn(rc, rc);
876
877 /*
878 * Set the attached flag.
879 */
880 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
881 return VINF_SUCCESS;
882}
883
884
885/**
886 * Detaches a debugger from the specified VM.
887 *
888 * Caller must be attached to the VM.
889 *
890 * @returns VBox status code.
891 * @param pVM VM Handle.
892 */
893VMMR3DECL(int) DBGFR3Detach(PVM pVM)
894{
895 LogFlow(("DBGFR3Detach:\n"));
896 int rc;
897
898 /*
899 * Check if attached.
900 */
901 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
902
903 /*
904 * Try send the detach command.
905 * Keep in mind that we might be racing EMT, so, be extra careful.
906 */
907 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
908 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
909 {
910 rc = RTSemPong(&pVM->dbgf.s.PingPong);
911 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
912 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
913 }
914
915 /*
916 * Wait for the OK event.
917 */
918 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
919 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
920
921 /*
922 * Send the notification command indicating that we're really done.
923 */
924 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
925 rc = RTSemPong(&pVM->dbgf.s.PingPong);
926 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
927
928 LogFlowFunc(("returns VINF_SUCCESS\n"));
929 return VINF_SUCCESS;
930}
931
932
933/**
934 * Wait for a debug event.
935 *
936 * @returns VBox status. Will not return VBOX_INTERRUPTED.
937 * @param pVM VM handle.
938 * @param cMillies Number of millies to wait.
939 * @param ppEvent Where to store the event pointer.
940 */
941VMMR3DECL(int) DBGFR3EventWait(PVM pVM, unsigned cMillies, PCDBGFEVENT *ppEvent)
942{
943 /*
944 * Check state.
945 */
946 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
947 *ppEvent = NULL;
948
949 /*
950 * Wait.
951 */
952 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
953 if (RT_SUCCESS(rc))
954 {
955 *ppEvent = &pVM->dbgf.s.DbgEvent;
956 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
957 return VINF_SUCCESS;
958 }
959
960 return rc;
961}
962
963
964/**
965 * Halts VM execution.
966 *
967 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
968 * arrives. Until that time it's not possible to issue any new commands.
969 *
970 * @returns VBox status.
971 * @param pVM VM handle.
972 */
973VMMR3DECL(int) DBGFR3Halt(PVM pVM)
974{
975 /*
976 * Check state.
977 */
978 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
979 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
980 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
981 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
982 return VWRN_DBGF_ALREADY_HALTED;
983
984 /*
985 * Send command.
986 */
987 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
988
989 return VINF_SUCCESS;
990}
991
992
993/**
994 * Checks if the VM is halted by the debugger.
995 *
996 * @returns True if halted.
997 * @returns False if not halted.
998 * @param pVM VM handle.
999 */
1000VMMR3DECL(bool) DBGFR3IsHalted(PVM pVM)
1001{
1002 AssertReturn(pVM->dbgf.s.fAttached, false);
1003 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1004 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1005 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1006}
1007
1008
1009/**
1010 * Checks if the debugger can wait for events or not.
1011 *
1012 * This function is only used by lazy, multiplexing debuggers. :-)
1013 *
1014 * @returns True if waitable.
1015 * @returns False if not waitable.
1016 * @param pVM VM handle.
1017 */
1018VMMR3DECL(bool) DBGFR3CanWait(PVM pVM)
1019{
1020 AssertReturn(pVM->dbgf.s.fAttached, false);
1021 return RTSemPongShouldWait(&pVM->dbgf.s.PingPong);
1022}
1023
1024
1025/**
1026 * Resumes VM execution.
1027 *
1028 * There is no receipt event on this command.
1029 *
1030 * @returns VBox status.
1031 * @param pVM VM handle.
1032 */
1033VMMR3DECL(int) DBGFR3Resume(PVM pVM)
1034{
1035 /*
1036 * Check state.
1037 */
1038 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1039 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1040
1041 /*
1042 * Send the ping back to the emulation thread telling it to run.
1043 */
1044 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1045 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1046 AssertRC(rc);
1047
1048 return rc;
1049}
1050
1051
1052/**
1053 * Step Into.
1054 *
1055 * A single step event is generated from this command.
1056 * The current implementation is not reliable, so don't rely on the event comming.
1057 *
1058 * @returns VBox status.
1059 * @param pVM VM handle.
1060 * @param idCpu The ID of the CPU to single step on.
1061 */
1062VMMR3DECL(int) DBGFR3Step(PVM pVM, VMCPUID idCpu)
1063{
1064 /*
1065 * Check state.
1066 */
1067 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1068 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1069 AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_PARAMETER);
1070
1071 /*
1072 * Send the ping back to the emulation thread telling it to run.
1073 */
1074/** @todo SMP (idCpu) */
1075 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1076 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1077 AssertRC(rc);
1078 return rc;
1079}
1080
1081
1082/**
1083 * Call this to single step programatically.
1084 *
1085 * You must pass down the return code to the EM loop! That's
1086 * where the actual single stepping take place (at least in the
1087 * current implementation).
1088 *
1089 * @returns VINF_EM_DBG_STEP
1090 *
1091 * @param pVCpu The virtual CPU handle.
1092 *
1093 * @thread VCpu EMT
1094 */
1095VMMR3DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
1096{
1097 VMCPU_ASSERT_EMT(pVCpu);
1098
1099 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1100 return VINF_EM_DBG_STEP;
1101}
1102
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