VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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

© 2023 Oracle
ContactPrivacy policyTerms of Use