VirtualBox

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

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

VM: VMR3Notify*FF refactorying (for poking); converting fNotifiedREM to a flag and dropping the pVM/pVCpu edition of the API.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 31.2 KB
Line 
1/* $Id: DBGF.cpp 19400 2009-05-05 21:49:16Z 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 VMR3NotifyGlobalFFU(pVM->pUVM, 0 /* 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 || VMCPU_FF_ISSET(pVCpu, VMCPU_FF_REQUEST))
633 {
634 LogFlow(("dbgfR3VMMWait: Processes requests...\n"));
635 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY);
636 if (rc == VINF_SUCCESS)
637 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu);
638 LogFlow(("dbgfR3VMMWait: VMR3ReqProcess -> %Rrc rcRet=%Rrc\n", rc, rcRet));
639 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
640 {
641 switch (rc)
642 {
643 case VINF_EM_DBG_BREAKPOINT:
644 case VINF_EM_DBG_STEPPED:
645 case VINF_EM_DBG_STEP:
646 case VINF_EM_DBG_STOP:
647 AssertMsgFailed(("rc=%Rrc\n", rc));
648 break;
649
650 /* return straight away */
651 case VINF_EM_TERMINATE:
652 case VINF_EM_OFF:
653 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
654 return rc;
655
656 /* remember return code. */
657 default:
658 AssertReleaseMsgFailed(("rc=%Rrc is not in the switch!\n", rc));
659 case VINF_EM_RESET:
660 case VINF_EM_SUSPEND:
661 case VINF_EM_HALT:
662 case VINF_EM_RESUME:
663 case VINF_EM_RESCHEDULE:
664 case VINF_EM_RESCHEDULE_REM:
665 case VINF_EM_RESCHEDULE_RAW:
666 if (rc < rcRet || rcRet == VINF_SUCCESS)
667 rcRet = rc;
668 break;
669 }
670 }
671 else if (RT_FAILURE(rc))
672 {
673 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
674 return rc;
675 }
676 }
677 }
678
679 /*
680 * Process the command.
681 */
682 bool fResumeExecution;
683 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
684 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
685 int rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
686 if (fResumeExecution)
687 {
688 if (RT_FAILURE(rc))
689 rcRet = rc;
690 else if ( rc >= VINF_EM_FIRST
691 && rc <= VINF_EM_LAST
692 && (rc < rcRet || rcRet == VINF_SUCCESS))
693 rcRet = rc;
694 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rcRet));
695 return rcRet;
696 }
697 }
698}
699
700
701/**
702 * Executes command from debugger.
703 * The caller is responsible for waiting or resuming execution based on the
704 * value returned in the *pfResumeExecution indicator.
705 *
706 * @returns VBox status. (clearify!)
707 * @param pVM VM Handle.
708 * @param enmCmd The command in question.
709 * @param pCmdData Pointer to the command data.
710 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
711 */
712static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
713{
714 bool fSendEvent;
715 bool fResume;
716 int rc = VINF_SUCCESS;
717
718 switch (enmCmd)
719 {
720 /*
721 * Halt is answered by an event say that we've halted.
722 */
723 case DBGFCMD_HALT:
724 {
725 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
726 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
727 fSendEvent = true;
728 fResume = false;
729 break;
730 }
731
732
733 /*
734 * Resume is not answered we'll just resume execution.
735 */
736 case DBGFCMD_GO:
737 {
738 fSendEvent = false;
739 fResume = true;
740 break;
741 }
742
743 /** @todo implement (and define) the rest of the commands. */
744
745 /*
746 * Disable breakpoints and stuff.
747 * Send an everythings cool event to the debugger thread and resume execution.
748 */
749 case DBGFCMD_DETACH_DEBUGGER:
750 {
751 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
752 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
753 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
754 fSendEvent = true;
755 fResume = true;
756 break;
757 }
758
759 /*
760 * The debugger has detached successfully.
761 * There is no reply to this event.
762 */
763 case DBGFCMD_DETACHED_DEBUGGER:
764 {
765 fSendEvent = false;
766 fResume = true;
767 break;
768 }
769
770 /*
771 * Single step, with trace into.
772 */
773 case DBGFCMD_SINGLE_STEP:
774 {
775 Log2(("Single step\n"));
776 rc = VINF_EM_DBG_STEP;
777 /** @todo SMP */
778 PVMCPU pVCpu = VMMGetCpu0(pVM);
779 pVCpu->dbgf.s.fSingleSteppingRaw = true;
780 fSendEvent = false;
781 fResume = true;
782 break;
783 }
784
785 /*
786 * Default is to send an invalid command event.
787 */
788 default:
789 {
790 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
791 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
792 fSendEvent = true;
793 fResume = false;
794 break;
795 }
796 }
797
798 /*
799 * Send pending event.
800 */
801 if (fSendEvent)
802 {
803 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
804 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
805 if (RT_FAILURE(rc2))
806 {
807 AssertRC(rc2);
808 *pfResumeExecution = true;
809 return rc2;
810 }
811 }
812
813 /*
814 * Return.
815 */
816 *pfResumeExecution = fResume;
817 return rc;
818}
819
820
821/**
822 * Attaches a debugger to the specified VM.
823 *
824 * Only one debugger at a time.
825 *
826 * @returns VBox status code.
827 * @param pVM VM Handle.
828 */
829VMMR3DECL(int) DBGFR3Attach(PVM pVM)
830{
831 /*
832 * Some validations first.
833 */
834 if (!VALID_PTR(pVM))
835 {
836 Log(("DBGFR3Attach: bad VM handle: %p\n", pVM));
837 return VERR_INVALID_HANDLE;
838 }
839 VMSTATE enmVMState = pVM->enmVMState;
840 if ( enmVMState >= VMSTATE_DESTROYING
841 || enmVMState < VMSTATE_CREATING)
842 {
843 Log(("DBGFR3Attach: Invalid VM state: %s\n", VMGetStateName(enmVMState)));
844 return VERR_INVALID_HANDLE;
845 }
846
847 /*
848 * Call the VM, use EMT for serialization.
849 */
850 PVMREQ pReq;
851 int rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)dbgfR3Attach, 1, pVM);
852 if (RT_SUCCESS(rc))
853 rc = pReq->iStatus;
854 VMR3ReqFree(pReq);
855
856 return rc;
857}
858
859
860/**
861 * EMT worker for DBGFR3Attach.
862 *
863 * @returns VBox status code.
864 * @param pVM Pointer to the shared VM structure.
865 */
866static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
867{
868 if (pVM->dbgf.s.fAttached)
869 {
870 Log(("dbgR3Attach: Debugger already attached\n"));
871 return VERR_DBGF_ALREADY_ATTACHED;
872 }
873
874 /*
875 * Create the Ping-Pong structure.
876 */
877 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
878 AssertRCReturn(rc, rc);
879
880 /*
881 * Set the attached flag.
882 */
883 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
884 return VINF_SUCCESS;
885}
886
887
888/**
889 * Detaches a debugger from the specified VM.
890 *
891 * Caller must be attached to the VM.
892 *
893 * @returns VBox status code.
894 * @param pVM VM Handle.
895 */
896VMMR3DECL(int) DBGFR3Detach(PVM pVM)
897{
898 LogFlow(("DBGFR3Detach:\n"));
899 int rc;
900
901 /*
902 * Check if attached.
903 */
904 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
905
906 /*
907 * Try send the detach command.
908 * Keep in mind that we might be racing EMT, so, be extra careful.
909 */
910 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
911 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
912 {
913 rc = RTSemPong(&pVM->dbgf.s.PingPong);
914 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
915 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
916 }
917
918 /*
919 * Wait for the OK event.
920 */
921 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
922 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
923
924 /*
925 * Send the notification command indicating that we're really done.
926 */
927 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
928 rc = RTSemPong(&pVM->dbgf.s.PingPong);
929 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
930
931 LogFlowFunc(("returns VINF_SUCCESS\n"));
932 return VINF_SUCCESS;
933}
934
935
936/**
937 * Wait for a debug event.
938 *
939 * @returns VBox status. Will not return VBOX_INTERRUPTED.
940 * @param pVM VM handle.
941 * @param cMillies Number of millies to wait.
942 * @param ppEvent Where to store the event pointer.
943 */
944VMMR3DECL(int) DBGFR3EventWait(PVM pVM, unsigned cMillies, PCDBGFEVENT *ppEvent)
945{
946 /*
947 * Check state.
948 */
949 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
950 *ppEvent = NULL;
951
952 /*
953 * Wait.
954 */
955 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
956 if (RT_SUCCESS(rc))
957 {
958 *ppEvent = &pVM->dbgf.s.DbgEvent;
959 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
960 return VINF_SUCCESS;
961 }
962
963 return rc;
964}
965
966
967/**
968 * Halts VM execution.
969 *
970 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
971 * arrives. Until that time it's not possible to issue any new commands.
972 *
973 * @returns VBox status.
974 * @param pVM VM handle.
975 */
976VMMR3DECL(int) DBGFR3Halt(PVM pVM)
977{
978 /*
979 * Check state.
980 */
981 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
982 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
983 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
984 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
985 return VWRN_DBGF_ALREADY_HALTED;
986
987 /*
988 * Send command.
989 */
990 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
991
992 return VINF_SUCCESS;
993}
994
995
996/**
997 * Checks if the VM is halted by the debugger.
998 *
999 * @returns True if halted.
1000 * @returns False if not halted.
1001 * @param pVM VM handle.
1002 */
1003VMMR3DECL(bool) DBGFR3IsHalted(PVM pVM)
1004{
1005 AssertReturn(pVM->dbgf.s.fAttached, false);
1006 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1007 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1008 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1009}
1010
1011
1012/**
1013 * Checks if the debugger can wait for events or not.
1014 *
1015 * This function is only used by lazy, multiplexing debuggers. :-)
1016 *
1017 * @returns True if waitable.
1018 * @returns False if not waitable.
1019 * @param pVM VM handle.
1020 */
1021VMMR3DECL(bool) DBGFR3CanWait(PVM pVM)
1022{
1023 AssertReturn(pVM->dbgf.s.fAttached, false);
1024 return RTSemPongShouldWait(&pVM->dbgf.s.PingPong);
1025}
1026
1027
1028/**
1029 * Resumes VM execution.
1030 *
1031 * There is no receipt event on this command.
1032 *
1033 * @returns VBox status.
1034 * @param pVM VM handle.
1035 */
1036VMMR3DECL(int) DBGFR3Resume(PVM pVM)
1037{
1038 /*
1039 * Check state.
1040 */
1041 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1042 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1043
1044 /*
1045 * Send the ping back to the emulation thread telling it to run.
1046 */
1047 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1048 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1049 AssertRC(rc);
1050
1051 return rc;
1052}
1053
1054
1055/**
1056 * Step Into.
1057 *
1058 * A single step event is generated from this command.
1059 * The current implementation is not reliable, so don't rely on the event comming.
1060 *
1061 * @returns VBox status.
1062 * @param pVM VM handle.
1063 * @param idCpu The ID of the CPU to single step on.
1064 */
1065VMMR3DECL(int) DBGFR3Step(PVM pVM, VMCPUID idCpu)
1066{
1067 /*
1068 * Check state.
1069 */
1070 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1071 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1072 AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_PARAMETER);
1073
1074 /*
1075 * Send the ping back to the emulation thread telling it to run.
1076 */
1077/** @todo SMP (idCpu) */
1078 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1079 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1080 AssertRC(rc);
1081 return rc;
1082}
1083
1084
1085/**
1086 * Call this to single step programatically.
1087 *
1088 * You must pass down the return code to the EM loop! That's
1089 * where the actual single stepping take place (at least in the
1090 * current implementation).
1091 *
1092 * @returns VINF_EM_DBG_STEP
1093 *
1094 * @param pVCpu The virtual CPU handle.
1095 *
1096 * @thread VCpu EMT
1097 */
1098VMMR3DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
1099{
1100 VMCPU_ASSERT_EMT(pVCpu);
1101
1102 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1103 return VINF_EM_DBG_STEP;
1104}
1105
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