VirtualBox

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

Last change on this file since 45028 was 45006, checked in by vboxsync, 12 years ago

DBGF/DBGC: Fixing power off problems.

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