VirtualBox

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

Last change on this file since 49966 was 46420, checked in by vboxsync, 12 years ago

VMM, recompiler: Purge deprecated macros.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 36.3 KB
Line 
1/* $Id: DBGF.cpp 46420 2013-06-06 16:27:25Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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. The interface has an asynchronous nature. This comes
31 * from the fact that the VMM and the Debugger are running in different threads.
32 * They are referred to as the "emulation thread" and the "debugger thread", or
33 * as the "ping thread" and the "pong thread, respectivly. (The last set of
34 * names comes from the use of the Ping-Pong synchronization construct from the
35 * RTSem API.)
36 *
37 * @see grp_dbgf
38 *
39 *
40 * @section sec_dbgf_scenario Usage Scenario
41 *
42 * The debugger starts by attaching to the VM. For practical reasons we limit the
43 * number of concurrently attached debuggers to 1 per VM. The action of
44 * attaching to the VM causes the VM to check and generate debug events.
45 *
46 * The debugger then will wait/poll for debug events and issue commands.
47 *
48 * The waiting and polling is done by the DBGFEventWait() function. It will wait
49 * for the emulation thread to send a ping, thus indicating that there is an
50 * event waiting to be processed.
51 *
52 * An event can be a response to a command issued previously, the hitting of a
53 * breakpoint, or running into a bad/fatal VMM condition. The debugger now has
54 * the ping and must respond to the event at hand - the VMM is waiting. This
55 * usually means that the user of the debugger must do something, but it doesn't
56 * have to. The debugger is free to call any DBGF function (nearly at least)
57 * while processing the event.
58 *
59 * Typically the user will issue a request for the execution to be resumed, so
60 * the debugger calls DBGFResume() and goes back to waiting/polling for events.
61 *
62 * When the user eventually terminates the debugging session or selects another
63 * VM, the debugger detaches from the VM. This means that breakpoints are
64 * disabled and that the emulation thread no longer polls for debugger commands.
65 *
66 */
67
68
69/*******************************************************************************
70* Header Files *
71*******************************************************************************/
72#define LOG_GROUP LOG_GROUP_DBGF
73#include <VBox/vmm/dbgf.h>
74#include <VBox/vmm/selm.h>
75#ifdef VBOX_WITH_REM
76# include <VBox/vmm/rem.h>
77#endif
78#include <VBox/vmm/em.h>
79#include <VBox/vmm/hm.h>
80#include "DBGFInternal.h"
81#include <VBox/vmm/vm.h>
82#include <VBox/vmm/uvm.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 Pointer to the VM.
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 Pointer to the VM.
136 */
137VMMR3_INT_DECL(int) DBGFR3Init(PVM pVM)
138{
139 PUVM pUVM = pVM->pUVM;
140 AssertCompile(sizeof(pUVM->dbgf.s) <= sizeof(pUVM->dbgf.padding));
141 AssertCompile(sizeof(pUVM->aCpus[0].dbgf.s) <= sizeof(pUVM->aCpus[0].dbgf.padding));
142
143 int rc = dbgfR3InfoInit(pUVM);
144 if (RT_SUCCESS(rc))
145 rc = dbgfR3TraceInit(pVM);
146 if (RT_SUCCESS(rc))
147 rc = dbgfR3RegInit(pUVM);
148 if (RT_SUCCESS(rc))
149 rc = dbgfR3AsInit(pUVM);
150 if (RT_SUCCESS(rc))
151 rc = dbgfR3BpInit(pVM);
152 return rc;
153}
154
155
156/**
157 * Terminates and cleans up resources allocated by the DBGF.
158 *
159 * @returns VBox status code.
160 * @param pVM Pointer to the VM.
161 */
162VMMR3_INT_DECL(int) DBGFR3Term(PVM pVM)
163{
164 PUVM pUVM = pVM->pUVM;
165
166 dbgfR3OSTerm(pUVM);
167 dbgfR3AsTerm(pUVM);
168 dbgfR3RegTerm(pUVM);
169 dbgfR3TraceTerm(pVM);
170 dbgfR3InfoTerm(pUVM);
171
172 return VINF_SUCCESS;
173}
174
175
176/**
177 * Called when the VM is powered off to detach debuggers.
178 *
179 * @param pVM The VM handle.
180 */
181VMMR3_INT_DECL(void) DBGFR3PowerOff(PVM pVM)
182{
183
184 /*
185 * Send a termination event to any attached debugger.
186 */
187 /* wait to become the speaker (we should already be that). */
188 if ( pVM->dbgf.s.fAttached
189 && RTSemPingShouldWait(&pVM->dbgf.s.PingPong))
190 RTSemPingWait(&pVM->dbgf.s.PingPong, 5000);
191
192 if (pVM->dbgf.s.fAttached)
193 {
194 /* Just mark it as detached if we're not in a position to send a power
195 off event. It should fail later on. */
196 if (!RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
197 {
198 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
199 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
200 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
201 }
202
203 if (RTSemPingIsSpeaker(&pVM->dbgf.s.PingPong))
204 {
205 /* Try send the power off event. */
206 int rc;
207 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
208 if (enmCmd == DBGFCMD_DETACH_DEBUGGER)
209 /* the debugger beat us to initiating the detaching. */
210 rc = VINF_SUCCESS;
211 else
212 {
213 /* ignore the command (if any). */
214 enmCmd = DBGFCMD_NO_COMMAND;
215 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_POWERING_OFF;
216 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
217 rc = RTSemPing(&pVM->dbgf.s.PingPong);
218 }
219
220 /*
221 * Process commands and priority requests until we get a command
222 * indicating that the debugger has detached.
223 */
224 uint32_t cPollHack = 1;
225 PVMCPU pVCpu = VMMGetCpu(pVM);
226 while (RT_SUCCESS(rc))
227 {
228 if (enmCmd != DBGFCMD_NO_COMMAND)
229 {
230 /* process command */
231 bool fResumeExecution;
232 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
233 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
234 if (enmCmd == DBGFCMD_DETACHED_DEBUGGER)
235 break;
236 enmCmd = DBGFCMD_NO_COMMAND;
237 }
238 else
239 {
240 /* Wait for new command, processing pending priority requests
241 first. The request processing is a bit crazy, but
242 unfortunately required by plugin unloading. */
243 if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
244 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
245 {
246 LogFlow(("DBGFR3PowerOff: Processes priority requests...\n"));
247 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
248 if (rc == VINF_SUCCESS)
249 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, true /*fPriorityOnly*/);
250 LogFlow(("DBGFR3PowerOff: VMR3ReqProcess -> %Rrc\n", rc));
251 cPollHack = 1;
252 }
253 else if (cPollHack < 120)
254 cPollHack++;
255
256 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
257 if (RT_SUCCESS(rc))
258 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
259 else if (rc == VERR_TIMEOUT)
260 rc = VINF_SUCCESS;
261 }
262 }
263
264 /*
265 * Clear the FF so we won't get confused later on.
266 */
267 VM_FF_CLEAR(pVM, VM_FF_DBGF);
268 }
269 }
270}
271
272
273/**
274 * Applies relocations to data and code managed by this
275 * component. This function will be called at init and
276 * whenever the VMM need to relocate it self inside the GC.
277 *
278 * @param pVM Pointer to the VM.
279 * @param offDelta Relocation delta relative to old location.
280 */
281VMMR3_INT_DECL(void) DBGFR3Relocate(PVM pVM, RTGCINTPTR offDelta)
282{
283 dbgfR3TraceRelocate(pVM);
284 dbgfR3AsRelocate(pVM->pUVM, offDelta);
285}
286
287
288/**
289 * Waits a little while for a debuggger to attach.
290 *
291 * @returns True is a debugger have attached.
292 * @param pVM Pointer to the VM.
293 * @param enmEvent Event.
294 */
295bool dbgfR3WaitForAttach(PVM pVM, DBGFEVENTTYPE enmEvent)
296{
297 /*
298 * First a message.
299 */
300#ifndef RT_OS_L4
301
302# if !defined(DEBUG) || defined(DEBUG_sandervl) || defined(DEBUG_frank) || defined(IEM_VERIFICATION_MODE)
303 int cWait = 10;
304# else
305 int cWait = HMIsEnabled(pVM)
306 && ( enmEvent == DBGFEVENT_ASSERTION_HYPER
307 || enmEvent == DBGFEVENT_FATAL_ERROR)
308 && !RTEnvExist("VBOX_DBGF_WAIT_FOR_ATTACH")
309 ? 10
310 : 150;
311# endif
312 RTStrmPrintf(g_pStdErr, "DBGF: No debugger attached, waiting %d second%s for one to attach (event=%d)\n",
313 cWait / 10, cWait != 10 ? "s" : "", enmEvent);
314 RTStrmFlush(g_pStdErr);
315 while (cWait > 0)
316 {
317 RTThreadSleep(100);
318 if (pVM->dbgf.s.fAttached)
319 {
320 RTStrmPrintf(g_pStdErr, "Attached!\n");
321 RTStrmFlush(g_pStdErr);
322 return true;
323 }
324
325 /* next */
326 if (!(cWait % 10))
327 {
328 RTStrmPrintf(g_pStdErr, "%d.", cWait / 10);
329 RTStrmFlush(g_pStdErr);
330 }
331 cWait--;
332 }
333#endif
334
335 RTStrmPrintf(g_pStdErr, "Stopping the VM!\n");
336 RTStrmFlush(g_pStdErr);
337 return false;
338}
339
340
341/**
342 * Forced action callback.
343 * The VMM will call this from it's main loop when VM_FF_DBGF is set.
344 *
345 * The function checks and executes pending commands from the debugger.
346 *
347 * @returns VINF_SUCCESS normally.
348 * @returns VERR_DBGF_RAISE_FATAL_ERROR to pretend a fatal error happened.
349 * @param pVM Pointer to the VM.
350 */
351VMMR3_INT_DECL(int) DBGFR3VMMForcedAction(PVM pVM)
352{
353 int rc = VINF_SUCCESS;
354
355 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_DBGF))
356 {
357 PVMCPU pVCpu = VMMGetCpu(pVM);
358
359 /*
360 * Command pending? Process it.
361 */
362 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
363 {
364 bool fResumeExecution;
365 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
366 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
367 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
368 if (!fResumeExecution)
369 rc = dbgfR3VMMWait(pVM);
370 }
371 }
372 return rc;
373}
374
375
376/**
377 * Flag whether the event implies that we're stopped in the hypervisor code
378 * and have to block certain operations.
379 *
380 * @param pVM Pointer to the VM.
381 * @param enmEvent The event.
382 */
383static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
384{
385 switch (enmEvent)
386 {
387 case DBGFEVENT_STEPPED_HYPER:
388 case DBGFEVENT_ASSERTION_HYPER:
389 case DBGFEVENT_BREAKPOINT_HYPER:
390 pVM->dbgf.s.fStoppedInHyper = true;
391 break;
392 default:
393 pVM->dbgf.s.fStoppedInHyper = false;
394 break;
395 }
396}
397
398
399/**
400 * Try to determine the event context.
401 *
402 * @returns debug event context.
403 * @param pVM Pointer to the VM.
404 */
405static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
406{
407 /** @todo SMP support! */
408 PVMCPU pVCpu = &pVM->aCpus[0];
409
410 switch (EMGetState(pVCpu))
411 {
412 case EMSTATE_RAW:
413 case EMSTATE_DEBUG_GUEST_RAW:
414 return DBGFEVENTCTX_RAW;
415
416 case EMSTATE_REM:
417 case EMSTATE_DEBUG_GUEST_REM:
418 return DBGFEVENTCTX_REM;
419
420 case EMSTATE_DEBUG_HYPER:
421 case EMSTATE_GURU_MEDITATION:
422 return DBGFEVENTCTX_HYPER;
423
424 default:
425 return DBGFEVENTCTX_OTHER;
426 }
427}
428
429/**
430 * The common event prologue code.
431 * It will set the 'stopped-in-hyper' flag, make sure someone is attached,
432 * and perhaps process any high priority pending actions (none yet).
433 *
434 * @returns VBox status.
435 * @param pVM Pointer to the VM.
436 * @param enmEvent The event to be sent.
437 */
438static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
439{
440 /** @todo SMP */
441 PVMCPU pVCpu = VMMGetCpu(pVM);
442
443 /*
444 * Check if a debugger is attached.
445 */
446 if ( !pVM->dbgf.s.fAttached
447 && !dbgfR3WaitForAttach(pVM, enmEvent))
448 {
449 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
450 return VERR_DBGF_NOT_ATTACHED;
451 }
452
453 /*
454 * Sync back the state from the REM.
455 */
456 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
457#ifdef VBOX_WITH_REM
458 if (!pVM->dbgf.s.fStoppedInHyper)
459 REMR3StateUpdate(pVM, pVCpu);
460#endif
461
462 /*
463 * Look thru pending commands and finish those which make sense now.
464 */
465 /** @todo Process/purge pending commands. */
466 //int rc = DBGFR3VMMForcedAction(pVM);
467 return VINF_SUCCESS;
468}
469
470
471/**
472 * Sends the event in the event buffer.
473 *
474 * @returns VBox status code.
475 * @param pVM Pointer to the VM.
476 */
477static int dbgfR3SendEvent(PVM pVM)
478{
479 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
480 if (RT_SUCCESS(rc))
481 rc = dbgfR3VMMWait(pVM);
482
483 pVM->dbgf.s.fStoppedInHyper = false;
484 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
485 return rc;
486}
487
488
489/**
490 * Send a generic debugger event which takes no data.
491 *
492 * @returns VBox status.
493 * @param pVM Pointer to the VM.
494 * @param enmEvent The event to send.
495 * @internal
496 */
497VMMR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
498{
499 int rc = dbgfR3EventPrologue(pVM, enmEvent);
500 if (RT_FAILURE(rc))
501 return rc;
502
503 /*
504 * Send the event and process the reply communication.
505 */
506 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
507 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
508 return dbgfR3SendEvent(pVM);
509}
510
511
512/**
513 * Send a debugger event which takes the full source file location.
514 *
515 * @returns VBox status.
516 * @param pVM Pointer to the VM.
517 * @param enmEvent The event to send.
518 * @param pszFile Source file.
519 * @param uLine Line number in source file.
520 * @param pszFunction Function name.
521 * @param pszFormat Message which accompanies the event.
522 * @param ... Message arguments.
523 * @internal
524 */
525VMMR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
526{
527 va_list args;
528 va_start(args, pszFormat);
529 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
530 va_end(args);
531 return rc;
532}
533
534
535/**
536 * Send a debugger event which takes the full source file location.
537 *
538 * @returns VBox status.
539 * @param pVM Pointer to the VM.
540 * @param enmEvent The event to send.
541 * @param pszFile Source file.
542 * @param uLine Line number in source file.
543 * @param pszFunction Function name.
544 * @param pszFormat Message which accompanies the event.
545 * @param args Message arguments.
546 * @internal
547 */
548VMMR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
549{
550 int rc = dbgfR3EventPrologue(pVM, enmEvent);
551 if (RT_FAILURE(rc))
552 return rc;
553
554 /*
555 * Format the message.
556 */
557 char *pszMessage = NULL;
558 char szMessage[8192];
559 if (pszFormat && *pszFormat)
560 {
561 pszMessage = &szMessage[0];
562 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
563 }
564
565 /*
566 * Send the event and process the reply communication.
567 */
568 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
569 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
570 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
571 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
572 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
573 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
574 return dbgfR3SendEvent(pVM);
575}
576
577
578/**
579 * Send a debugger event which takes the two assertion messages.
580 *
581 * @returns VBox status.
582 * @param pVM Pointer to the VM.
583 * @param enmEvent The event to send.
584 * @param pszMsg1 First assertion message.
585 * @param pszMsg2 Second assertion message.
586 */
587VMMR3_INT_DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
588{
589 int rc = dbgfR3EventPrologue(pVM, enmEvent);
590 if (RT_FAILURE(rc))
591 return rc;
592
593 /*
594 * Send the event and process the reply communication.
595 */
596 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
597 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
598 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg1 = pszMsg1;
599 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
600 return dbgfR3SendEvent(pVM);
601}
602
603
604/**
605 * Breakpoint was hit somewhere.
606 * Figure out which breakpoint it is and notify the debugger.
607 *
608 * @returns VBox status.
609 * @param pVM Pointer to the VM.
610 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
611 */
612VMMR3_INT_DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
613{
614 int rc = dbgfR3EventPrologue(pVM, enmEvent);
615 if (RT_FAILURE(rc))
616 return rc;
617
618 /*
619 * Send the event and process the reply communication.
620 */
621 /** @todo SMP */
622 PVMCPU pVCpu = VMMGetCpu0(pVM);
623
624 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
625 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVCpu->dbgf.s.iActiveBp;
626 pVCpu->dbgf.s.iActiveBp = ~0U;
627 if (iBp != ~0U)
628 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
629 else
630 {
631 /* REM breakpoints has be been searched for. */
632#if 0 /** @todo get flat PC api! */
633 uint32_t eip = CPUMGetGuestEIP(pVM);
634#else
635 /* @todo SMP support!! */
636 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(VMMGetCpu(pVM));
637 RTGCPTR eip = pCtx->rip + pCtx->cs.u64Base;
638#endif
639 for (size_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.aBreakpoints); i++)
640 if ( pVM->dbgf.s.aBreakpoints[i].enmType == DBGFBPTYPE_REM
641 && pVM->dbgf.s.aBreakpoints[i].GCPtr == eip)
642 {
643 pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVM->dbgf.s.aBreakpoints[i].iBp;
644 break;
645 }
646 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
647 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
648 }
649 return dbgfR3SendEvent(pVM);
650}
651
652
653/**
654 * Waits for the debugger to respond.
655 *
656 * @returns VBox status. (clearify)
657 * @param pVM Pointer to the VM.
658 */
659static int dbgfR3VMMWait(PVM pVM)
660{
661 PVMCPU pVCpu = VMMGetCpu(pVM);
662
663 LogFlow(("dbgfR3VMMWait:\n"));
664 int rcRet = VINF_SUCCESS;
665
666 /*
667 * Waits for the debugger to reply (i.e. issue an command).
668 */
669 for (;;)
670 {
671 /*
672 * Wait.
673 */
674 uint32_t cPollHack = 1; /** @todo this interface is horrible now that we're using lots of VMR3ReqCall stuff all over DBGF. */
675 for (;;)
676 {
677 int rc;
678 if ( !VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_REQUEST)
679 && !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
680 {
681 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
682 if (RT_SUCCESS(rc))
683 break;
684 if (rc != VERR_TIMEOUT)
685 {
686 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
687 return rc;
688 }
689 }
690
691 if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
692 {
693 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
694 cPollHack = 1;
695 }
696 else if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
697 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
698 {
699 LogFlow(("dbgfR3VMMWait: Processes requests...\n"));
700 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
701 if (rc == VINF_SUCCESS)
702 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, false /*fPriorityOnly*/);
703 LogFlow(("dbgfR3VMMWait: VMR3ReqProcess -> %Rrc rcRet=%Rrc\n", rc, rcRet));
704 cPollHack = 1;
705 }
706 else
707 {
708 rc = VINF_SUCCESS;
709 if (cPollHack < 120)
710 cPollHack++;
711 }
712
713 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
714 {
715 switch (rc)
716 {
717 case VINF_EM_DBG_BREAKPOINT:
718 case VINF_EM_DBG_STEPPED:
719 case VINF_EM_DBG_STEP:
720 case VINF_EM_DBG_STOP:
721 AssertMsgFailed(("rc=%Rrc\n", rc));
722 break;
723
724 /* return straight away */
725 case VINF_EM_TERMINATE:
726 case VINF_EM_OFF:
727 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
728 return rc;
729
730 /* remember return code. */
731 default:
732 AssertReleaseMsgFailed(("rc=%Rrc is not in the switch!\n", rc));
733 case VINF_EM_RESET:
734 case VINF_EM_SUSPEND:
735 case VINF_EM_HALT:
736 case VINF_EM_RESUME:
737 case VINF_EM_RESCHEDULE:
738 case VINF_EM_RESCHEDULE_REM:
739 case VINF_EM_RESCHEDULE_RAW:
740 if (rc < rcRet || rcRet == VINF_SUCCESS)
741 rcRet = rc;
742 break;
743 }
744 }
745 else if (RT_FAILURE(rc))
746 {
747 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
748 return rc;
749 }
750 }
751
752 /*
753 * Process the command.
754 */
755 bool fResumeExecution;
756 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
757 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
758 int rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
759 if (fResumeExecution)
760 {
761 if (RT_FAILURE(rc))
762 rcRet = rc;
763 else if ( rc >= VINF_EM_FIRST
764 && rc <= VINF_EM_LAST
765 && (rc < rcRet || rcRet == VINF_SUCCESS))
766 rcRet = rc;
767 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rcRet));
768 return rcRet;
769 }
770 }
771}
772
773
774/**
775 * Executes command from debugger.
776 * The caller is responsible for waiting or resuming execution based on the
777 * value returned in the *pfResumeExecution indicator.
778 *
779 * @returns VBox status. (clearify!)
780 * @param pVM Pointer to the VM.
781 * @param enmCmd The command in question.
782 * @param pCmdData Pointer to the command data.
783 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
784 */
785static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
786{
787 bool fSendEvent;
788 bool fResume;
789 int rc = VINF_SUCCESS;
790
791 NOREF(pCmdData); /* for later */
792
793 switch (enmCmd)
794 {
795 /*
796 * Halt is answered by an event say that we've halted.
797 */
798 case DBGFCMD_HALT:
799 {
800 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
801 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
802 fSendEvent = true;
803 fResume = false;
804 break;
805 }
806
807
808 /*
809 * Resume is not answered we'll just resume execution.
810 */
811 case DBGFCMD_GO:
812 {
813 /** @todo SMP */
814 PVMCPU pVCpu = VMMGetCpu0(pVM);
815 pVCpu->dbgf.s.fSingleSteppingRaw = false;
816 fSendEvent = false;
817 fResume = true;
818 break;
819 }
820
821 /** @todo implement (and define) the rest of the commands. */
822
823 /*
824 * Disable breakpoints and stuff.
825 * Send an everythings cool event to the debugger thread and resume execution.
826 */
827 case DBGFCMD_DETACH_DEBUGGER:
828 {
829 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
830 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
831 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
832 fSendEvent = true;
833 fResume = true;
834 break;
835 }
836
837 /*
838 * The debugger has detached successfully.
839 * There is no reply to this event.
840 */
841 case DBGFCMD_DETACHED_DEBUGGER:
842 {
843 fSendEvent = false;
844 fResume = true;
845 break;
846 }
847
848 /*
849 * Single step, with trace into.
850 */
851 case DBGFCMD_SINGLE_STEP:
852 {
853 Log2(("Single step\n"));
854 rc = VINF_EM_DBG_STEP;
855 /** @todo SMP */
856 PVMCPU pVCpu = VMMGetCpu0(pVM);
857 pVCpu->dbgf.s.fSingleSteppingRaw = true;
858 fSendEvent = false;
859 fResume = true;
860 break;
861 }
862
863 /*
864 * Default is to send an invalid command event.
865 */
866 default:
867 {
868 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
869 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
870 fSendEvent = true;
871 fResume = false;
872 break;
873 }
874 }
875
876 /*
877 * Send pending event.
878 */
879 if (fSendEvent)
880 {
881 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
882 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
883 if (RT_FAILURE(rc2))
884 {
885 AssertRC(rc2);
886 *pfResumeExecution = true;
887 return rc2;
888 }
889 }
890
891 /*
892 * Return.
893 */
894 *pfResumeExecution = fResume;
895 return rc;
896}
897
898
899/**
900 * Attaches a debugger to the specified VM.
901 *
902 * Only one debugger at a time.
903 *
904 * @returns VBox status code.
905 * @param pUVM The user mode VM handle.
906 */
907VMMR3DECL(int) DBGFR3Attach(PUVM pUVM)
908{
909 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
910 PVM pVM = pUVM->pVM;
911 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
912
913 /*
914 * Call the VM, use EMT for serialization.
915 */
916 /** @todo SMP */
917 return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)dbgfR3Attach, 1, pVM);
918}
919
920
921/**
922 * EMT worker for DBGFR3Attach.
923 *
924 * @returns VBox status code.
925 * @param pVM Pointer to the VM.
926 */
927static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
928{
929 if (pVM->dbgf.s.fAttached)
930 {
931 Log(("dbgR3Attach: Debugger already attached\n"));
932 return VERR_DBGF_ALREADY_ATTACHED;
933 }
934
935 /*
936 * Create the Ping-Pong structure.
937 */
938 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
939 AssertRCReturn(rc, rc);
940
941 /*
942 * Set the attached flag.
943 */
944 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
945 return VINF_SUCCESS;
946}
947
948
949/**
950 * Detaches a debugger from the specified VM.
951 *
952 * Caller must be attached to the VM.
953 *
954 * @returns VBox status code.
955 * @param pUVM The user mode VM handle.
956 */
957VMMR3DECL(int) DBGFR3Detach(PUVM pUVM)
958{
959 LogFlow(("DBGFR3Detach:\n"));
960 int rc;
961
962 /*
963 * Validate input. The UVM handle shall be valid, the VM handle might be
964 * in the processes of being destroyed already, so deal quietly with that.
965 */
966 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
967 PVM pVM = pUVM->pVM;
968 if (!VM_IS_VALID_EXT(pVM))
969 return VERR_INVALID_VM_HANDLE;
970
971 /*
972 * Check if attached.
973 */
974 if (!pVM->dbgf.s.fAttached)
975 return VERR_DBGF_NOT_ATTACHED;
976
977 /*
978 * Try send the detach command.
979 * Keep in mind that we might be racing EMT, so, be extra careful.
980 */
981 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
982 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
983 {
984 rc = RTSemPong(&pVM->dbgf.s.PingPong);
985 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
986 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
987 }
988
989 /*
990 * Wait for the OK event.
991 */
992 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
993 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
994
995 /*
996 * Send the notification command indicating that we're really done.
997 */
998 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
999 rc = RTSemPong(&pVM->dbgf.s.PingPong);
1000 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
1001
1002 LogFlowFunc(("returns VINF_SUCCESS\n"));
1003 return VINF_SUCCESS;
1004}
1005
1006
1007/**
1008 * Wait for a debug event.
1009 *
1010 * @returns VBox status. Will not return VBOX_INTERRUPTED.
1011 * @param pUVM The user mode VM handle.
1012 * @param cMillies Number of millis to wait.
1013 * @param ppEvent Where to store the event pointer.
1014 */
1015VMMR3DECL(int) DBGFR3EventWait(PUVM pUVM, RTMSINTERVAL cMillies, PCDBGFEVENT *ppEvent)
1016{
1017 /*
1018 * Check state.
1019 */
1020 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1021 PVM pVM = pUVM->pVM;
1022 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1023 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1024 *ppEvent = NULL;
1025
1026 /*
1027 * Wait.
1028 */
1029 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
1030 if (RT_SUCCESS(rc))
1031 {
1032 *ppEvent = &pVM->dbgf.s.DbgEvent;
1033 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
1034 return VINF_SUCCESS;
1035 }
1036
1037 return rc;
1038}
1039
1040
1041/**
1042 * Halts VM execution.
1043 *
1044 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
1045 * arrives. Until that time it's not possible to issue any new commands.
1046 *
1047 * @returns VBox status.
1048 * @param pUVM The user mode VM handle.
1049 */
1050VMMR3DECL(int) DBGFR3Halt(PUVM pUVM)
1051{
1052 /*
1053 * Check state.
1054 */
1055 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1056 PVM pVM = pUVM->pVM;
1057 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1058 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1059 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1060 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
1061 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
1062 return VWRN_DBGF_ALREADY_HALTED;
1063
1064 /*
1065 * Send command.
1066 */
1067 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
1068
1069 return VINF_SUCCESS;
1070}
1071
1072
1073/**
1074 * Checks if the VM is halted by the debugger.
1075 *
1076 * @returns True if halted.
1077 * @returns False if not halted.
1078 * @param pUVM The user mode VM handle.
1079 */
1080VMMR3DECL(bool) DBGFR3IsHalted(PUVM pUVM)
1081{
1082 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1083 PVM pVM = pUVM->pVM;
1084 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1085 AssertReturn(pVM->dbgf.s.fAttached, false);
1086
1087 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1088 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1089 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1090}
1091
1092
1093/**
1094 * Checks if the debugger can wait for events or not.
1095 *
1096 * This function is only used by lazy, multiplexing debuggers. :-)
1097 *
1098 * @returns VBox status code.
1099 * @retval VINF_SUCCESS if waitable.
1100 * @retval VERR_SEM_OUT_OF_TURN if not waitable.
1101 * @retval VERR_INVALID_VM_HANDLE if the VM is being (/ has been) destroyed
1102 * (not asserted) or if the handle is invalid (asserted).
1103 * @retval VERR_DBGF_NOT_ATTACHED if not attached.
1104 *
1105 * @param pUVM The user mode VM handle.
1106 */
1107VMMR3DECL(int) DBGFR3QueryWaitable(PUVM pUVM)
1108{
1109 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1110
1111 /* Note! There is a slight race here, unfortunately. */
1112 PVM pVM = pUVM->pVM;
1113 if (!RT_VALID_PTR(pVM))
1114 return VERR_INVALID_VM_HANDLE;
1115 if (pVM->enmVMState >= VMSTATE_DESTROYING)
1116 return VERR_INVALID_VM_HANDLE;
1117 if (!pVM->dbgf.s.fAttached)
1118 return VERR_DBGF_NOT_ATTACHED;
1119
1120 if (!RTSemPongShouldWait(&pVM->dbgf.s.PingPong))
1121 return VERR_SEM_OUT_OF_TURN;
1122
1123 return VINF_SUCCESS;
1124}
1125
1126
1127/**
1128 * Resumes VM execution.
1129 *
1130 * There is no receipt event on this command.
1131 *
1132 * @returns VBox status.
1133 * @param pUVM The user mode VM handle.
1134 */
1135VMMR3DECL(int) DBGFR3Resume(PUVM pUVM)
1136{
1137 /*
1138 * Check state.
1139 */
1140 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1141 PVM pVM = pUVM->pVM;
1142 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1143 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1144 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1145
1146 /*
1147 * Send the ping back to the emulation thread telling it to run.
1148 */
1149 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1150 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1151 AssertRC(rc);
1152
1153 return rc;
1154}
1155
1156
1157/**
1158 * Step Into.
1159 *
1160 * A single step event is generated from this command.
1161 * The current implementation is not reliable, so don't rely on the event coming.
1162 *
1163 * @returns VBox status.
1164 * @param pUVM The user mode VM handle.
1165 * @param idCpu The ID of the CPU to single step on.
1166 */
1167VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
1168{
1169 /*
1170 * Check state.
1171 */
1172 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1173 PVM pVM = pUVM->pVM;
1174 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1175 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1176 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1177 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1178
1179 /*
1180 * Send the ping back to the emulation thread telling it to run.
1181 */
1182/** @todo SMP (idCpu) */
1183 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1184 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1185 AssertRC(rc);
1186 return rc;
1187}
1188
1189
1190/**
1191 * Call this to single step programmatically.
1192 *
1193 * You must pass down the return code to the EM loop! That's
1194 * where the actual single stepping take place (at least in the
1195 * current implementation).
1196 *
1197 * @returns VINF_EM_DBG_STEP
1198 *
1199 * @param pVCpu Pointer to the VMCPU.
1200 *
1201 * @thread VCpu EMT
1202 * @internal
1203 */
1204VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
1205{
1206 VMCPU_ASSERT_EMT(pVCpu);
1207
1208 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1209 return VINF_EM_DBG_STEP;
1210}
1211
1212
1213/**
1214 * Inject an NMI into a running VM (only VCPU 0!)
1215 *
1216 * @returns VBox status code.
1217 * @param pVM Pointer to the VM.
1218 */
1219VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
1220{
1221 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1222 PVM pVM = pUVM->pVM;
1223 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1224 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1225
1226 /** @todo Implement generic NMI injection. */
1227 if (!HMIsEnabled(pVM))
1228 return VERR_NOT_SUP_IN_RAW_MODE;
1229
1230 VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_INTERRUPT_NMI);
1231 return VINF_SUCCESS;
1232}
1233
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