VirtualBox

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

Last change on this file since 55705 was 54218, checked in by vboxsync, 10 years ago

DBGF,DBGC: Added dmesg command and implemented it for linux guests.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 36.3 KB
Line 
1/* $Id: DBGF.cpp 54218 2015-02-16 15:17:05Z 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 if (RT_SUCCESS(rc))
153 rc = dbgfR3OSInit(pUVM);
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_IS_PENDING(pVM, VM_FF_REQUEST)
246 || VMCPU_FF_IS_PENDING(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_TEST_AND_CLEAR(pVM, VM_FF_DBGF))
358 {
359 PVMCPU pVCpu = VMMGetCpu(pVM);
360
361 /*
362 * Command pending? Process it.
363 */
364 if (pVM->dbgf.s.enmVMMCmd != DBGFCMD_NO_COMMAND)
365 {
366 bool fResumeExecution;
367 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
368 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
369 rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
370 if (!fResumeExecution)
371 rc = dbgfR3VMMWait(pVM);
372 }
373 }
374 return rc;
375}
376
377
378/**
379 * Flag whether the event implies that we're stopped in the hypervisor code
380 * and have to block certain operations.
381 *
382 * @param pVM Pointer to the VM.
383 * @param enmEvent The event.
384 */
385static void dbgfR3EventSetStoppedInHyperFlag(PVM pVM, DBGFEVENTTYPE enmEvent)
386{
387 switch (enmEvent)
388 {
389 case DBGFEVENT_STEPPED_HYPER:
390 case DBGFEVENT_ASSERTION_HYPER:
391 case DBGFEVENT_BREAKPOINT_HYPER:
392 pVM->dbgf.s.fStoppedInHyper = true;
393 break;
394 default:
395 pVM->dbgf.s.fStoppedInHyper = false;
396 break;
397 }
398}
399
400
401/**
402 * Try to determine the event context.
403 *
404 * @returns debug event context.
405 * @param pVM Pointer to the VM.
406 */
407static DBGFEVENTCTX dbgfR3FigureEventCtx(PVM pVM)
408{
409 /** @todo SMP support! */
410 PVMCPU pVCpu = &pVM->aCpus[0];
411
412 switch (EMGetState(pVCpu))
413 {
414 case EMSTATE_RAW:
415 case EMSTATE_DEBUG_GUEST_RAW:
416 return DBGFEVENTCTX_RAW;
417
418 case EMSTATE_REM:
419 case EMSTATE_DEBUG_GUEST_REM:
420 return DBGFEVENTCTX_REM;
421
422 case EMSTATE_DEBUG_HYPER:
423 case EMSTATE_GURU_MEDITATION:
424 return DBGFEVENTCTX_HYPER;
425
426 default:
427 return DBGFEVENTCTX_OTHER;
428 }
429}
430
431/**
432 * The common event prologue code.
433 * It will set the 'stopped-in-hyper' flag, make sure someone is attached,
434 * and perhaps process any high priority pending actions (none yet).
435 *
436 * @returns VBox status.
437 * @param pVM Pointer to the VM.
438 * @param enmEvent The event to be sent.
439 */
440static int dbgfR3EventPrologue(PVM pVM, DBGFEVENTTYPE enmEvent)
441{
442 /** @todo SMP */
443 PVMCPU pVCpu = VMMGetCpu(pVM);
444
445 /*
446 * Check if a debugger is attached.
447 */
448 if ( !pVM->dbgf.s.fAttached
449 && !dbgfR3WaitForAttach(pVM, enmEvent))
450 {
451 Log(("DBGFR3VMMEventSrc: enmEvent=%d - debugger not attached\n", enmEvent));
452 return VERR_DBGF_NOT_ATTACHED;
453 }
454
455 /*
456 * Sync back the state from the REM.
457 */
458 dbgfR3EventSetStoppedInHyperFlag(pVM, enmEvent);
459#ifdef VBOX_WITH_REM
460 if (!pVM->dbgf.s.fStoppedInHyper)
461 REMR3StateUpdate(pVM, pVCpu);
462#endif
463
464 /*
465 * Look thru pending commands and finish those which make sense now.
466 */
467 /** @todo Process/purge pending commands. */
468 //int rc = DBGFR3VMMForcedAction(pVM);
469 return VINF_SUCCESS;
470}
471
472
473/**
474 * Sends the event in the event buffer.
475 *
476 * @returns VBox status code.
477 * @param pVM Pointer to the VM.
478 */
479static int dbgfR3SendEvent(PVM pVM)
480{
481 int rc = RTSemPing(&pVM->dbgf.s.PingPong);
482 if (RT_SUCCESS(rc))
483 rc = dbgfR3VMMWait(pVM);
484
485 pVM->dbgf.s.fStoppedInHyper = false;
486 /** @todo sync VMM -> REM after exitting the debugger. everything may change while in the debugger! */
487 return rc;
488}
489
490
491/**
492 * Send a generic debugger event which takes no data.
493 *
494 * @returns VBox status.
495 * @param pVM Pointer to the VM.
496 * @param enmEvent The event to send.
497 * @internal
498 */
499VMMR3DECL(int) DBGFR3Event(PVM pVM, DBGFEVENTTYPE enmEvent)
500{
501 int rc = dbgfR3EventPrologue(pVM, enmEvent);
502 if (RT_FAILURE(rc))
503 return rc;
504
505 /*
506 * Send the event and process the reply communication.
507 */
508 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
509 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
510 return dbgfR3SendEvent(pVM);
511}
512
513
514/**
515 * Send a debugger event which takes the full source file location.
516 *
517 * @returns VBox status.
518 * @param pVM Pointer to the VM.
519 * @param enmEvent The event to send.
520 * @param pszFile Source file.
521 * @param uLine Line number in source file.
522 * @param pszFunction Function name.
523 * @param pszFormat Message which accompanies the event.
524 * @param ... Message arguments.
525 * @internal
526 */
527VMMR3DECL(int) DBGFR3EventSrc(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, ...)
528{
529 va_list args;
530 va_start(args, pszFormat);
531 int rc = DBGFR3EventSrcV(pVM, enmEvent, pszFile, uLine, pszFunction, pszFormat, args);
532 va_end(args);
533 return rc;
534}
535
536
537/**
538 * Send a debugger event which takes the full source file location.
539 *
540 * @returns VBox status.
541 * @param pVM Pointer to the VM.
542 * @param enmEvent The event to send.
543 * @param pszFile Source file.
544 * @param uLine Line number in source file.
545 * @param pszFunction Function name.
546 * @param pszFormat Message which accompanies the event.
547 * @param args Message arguments.
548 * @internal
549 */
550VMMR3DECL(int) DBGFR3EventSrcV(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszFile, unsigned uLine, const char *pszFunction, const char *pszFormat, va_list args)
551{
552 int rc = dbgfR3EventPrologue(pVM, enmEvent);
553 if (RT_FAILURE(rc))
554 return rc;
555
556 /*
557 * Format the message.
558 */
559 char *pszMessage = NULL;
560 char szMessage[8192];
561 if (pszFormat && *pszFormat)
562 {
563 pszMessage = &szMessage[0];
564 RTStrPrintfV(szMessage, sizeof(szMessage), pszFormat, args);
565 }
566
567 /*
568 * Send the event and process the reply communication.
569 */
570 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
571 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
572 pVM->dbgf.s.DbgEvent.u.Src.pszFile = pszFile;
573 pVM->dbgf.s.DbgEvent.u.Src.uLine = uLine;
574 pVM->dbgf.s.DbgEvent.u.Src.pszFunction = pszFunction;
575 pVM->dbgf.s.DbgEvent.u.Src.pszMessage = pszMessage;
576 return dbgfR3SendEvent(pVM);
577}
578
579
580/**
581 * Send a debugger event which takes the two assertion messages.
582 *
583 * @returns VBox status.
584 * @param pVM Pointer to the VM.
585 * @param enmEvent The event to send.
586 * @param pszMsg1 First assertion message.
587 * @param pszMsg2 Second assertion message.
588 */
589VMMR3_INT_DECL(int) DBGFR3EventAssertion(PVM pVM, DBGFEVENTTYPE enmEvent, const char *pszMsg1, const char *pszMsg2)
590{
591 int rc = dbgfR3EventPrologue(pVM, enmEvent);
592 if (RT_FAILURE(rc))
593 return rc;
594
595 /*
596 * Send the event and process the reply communication.
597 */
598 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
599 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
600 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg1 = pszMsg1;
601 pVM->dbgf.s.DbgEvent.u.Assert.pszMsg2 = pszMsg2;
602 return dbgfR3SendEvent(pVM);
603}
604
605
606/**
607 * Breakpoint was hit somewhere.
608 * Figure out which breakpoint it is and notify the debugger.
609 *
610 * @returns VBox status.
611 * @param pVM Pointer to the VM.
612 * @param enmEvent DBGFEVENT_BREAKPOINT_HYPER or DBGFEVENT_BREAKPOINT.
613 */
614VMMR3_INT_DECL(int) DBGFR3EventBreakpoint(PVM pVM, DBGFEVENTTYPE enmEvent)
615{
616 int rc = dbgfR3EventPrologue(pVM, enmEvent);
617 if (RT_FAILURE(rc))
618 return rc;
619
620 /*
621 * Send the event and process the reply communication.
622 */
623 /** @todo SMP */
624 PVMCPU pVCpu = VMMGetCpu0(pVM);
625
626 pVM->dbgf.s.DbgEvent.enmType = enmEvent;
627 RTUINT iBp = pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVCpu->dbgf.s.iActiveBp;
628 pVCpu->dbgf.s.iActiveBp = ~0U;
629 if (iBp != ~0U)
630 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_RAW;
631 else
632 {
633 /* REM breakpoints has be been searched for. */
634#if 0 /** @todo get flat PC api! */
635 uint32_t eip = CPUMGetGuestEIP(pVM);
636#else
637 /* @todo SMP support!! */
638 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(VMMGetCpu(pVM));
639 RTGCPTR eip = pCtx->rip + pCtx->cs.u64Base;
640#endif
641 for (size_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.aBreakpoints); i++)
642 if ( pVM->dbgf.s.aBreakpoints[i].enmType == DBGFBPTYPE_REM
643 && pVM->dbgf.s.aBreakpoints[i].GCPtr == eip)
644 {
645 pVM->dbgf.s.DbgEvent.u.Bp.iBp = pVM->dbgf.s.aBreakpoints[i].iBp;
646 break;
647 }
648 AssertMsg(pVM->dbgf.s.DbgEvent.u.Bp.iBp != ~0U, ("eip=%08x\n", eip));
649 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_REM;
650 }
651 return dbgfR3SendEvent(pVM);
652}
653
654
655/**
656 * Waits for the debugger to respond.
657 *
658 * @returns VBox status. (clearify)
659 * @param pVM Pointer to the VM.
660 */
661static int dbgfR3VMMWait(PVM pVM)
662{
663 PVMCPU pVCpu = VMMGetCpu(pVM);
664
665 LogFlow(("dbgfR3VMMWait:\n"));
666 int rcRet = VINF_SUCCESS;
667
668 /*
669 * Waits for the debugger to reply (i.e. issue an command).
670 */
671 for (;;)
672 {
673 /*
674 * Wait.
675 */
676 uint32_t cPollHack = 1; /** @todo this interface is horrible now that we're using lots of VMR3ReqCall stuff all over DBGF. */
677 for (;;)
678 {
679 int rc;
680 if ( !VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_REQUEST)
681 && !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
682 {
683 rc = RTSemPingWait(&pVM->dbgf.s.PingPong, cPollHack);
684 if (RT_SUCCESS(rc))
685 break;
686 if (rc != VERR_TIMEOUT)
687 {
688 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
689 return rc;
690 }
691 }
692
693 if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
694 {
695 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
696 cPollHack = 1;
697 }
698 else if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
699 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
700 {
701 LogFlow(("dbgfR3VMMWait: Processes requests...\n"));
702 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
703 if (rc == VINF_SUCCESS)
704 rc = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, false /*fPriorityOnly*/);
705 LogFlow(("dbgfR3VMMWait: VMR3ReqProcess -> %Rrc rcRet=%Rrc\n", rc, rcRet));
706 cPollHack = 1;
707 }
708 else
709 {
710 rc = VINF_SUCCESS;
711 if (cPollHack < 120)
712 cPollHack++;
713 }
714
715 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
716 {
717 switch (rc)
718 {
719 case VINF_EM_DBG_BREAKPOINT:
720 case VINF_EM_DBG_STEPPED:
721 case VINF_EM_DBG_STEP:
722 case VINF_EM_DBG_STOP:
723 AssertMsgFailed(("rc=%Rrc\n", rc));
724 break;
725
726 /* return straight away */
727 case VINF_EM_TERMINATE:
728 case VINF_EM_OFF:
729 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
730 return rc;
731
732 /* remember return code. */
733 default:
734 AssertReleaseMsgFailed(("rc=%Rrc is not in the switch!\n", rc));
735 case VINF_EM_RESET:
736 case VINF_EM_SUSPEND:
737 case VINF_EM_HALT:
738 case VINF_EM_RESUME:
739 case VINF_EM_RESCHEDULE:
740 case VINF_EM_RESCHEDULE_REM:
741 case VINF_EM_RESCHEDULE_RAW:
742 if (rc < rcRet || rcRet == VINF_SUCCESS)
743 rcRet = rc;
744 break;
745 }
746 }
747 else if (RT_FAILURE(rc))
748 {
749 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rc));
750 return rc;
751 }
752 }
753
754 /*
755 * Process the command.
756 */
757 bool fResumeExecution;
758 DBGFCMDDATA CmdData = pVM->dbgf.s.VMMCmdData;
759 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
760 int rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
761 if (fResumeExecution)
762 {
763 if (RT_FAILURE(rc))
764 rcRet = rc;
765 else if ( rc >= VINF_EM_FIRST
766 && rc <= VINF_EM_LAST
767 && (rc < rcRet || rcRet == VINF_SUCCESS))
768 rcRet = rc;
769 LogFlow(("dbgfR3VMMWait: returns %Rrc\n", rcRet));
770 return rcRet;
771 }
772 }
773}
774
775
776/**
777 * Executes command from debugger.
778 * The caller is responsible for waiting or resuming execution based on the
779 * value returned in the *pfResumeExecution indicator.
780 *
781 * @returns VBox status. (clearify!)
782 * @param pVM Pointer to the VM.
783 * @param enmCmd The command in question.
784 * @param pCmdData Pointer to the command data.
785 * @param pfResumeExecution Where to store the resume execution / continue waiting indicator.
786 */
787static int dbgfR3VMMCmd(PVM pVM, DBGFCMD enmCmd, PDBGFCMDDATA pCmdData, bool *pfResumeExecution)
788{
789 bool fSendEvent;
790 bool fResume;
791 int rc = VINF_SUCCESS;
792
793 NOREF(pCmdData); /* for later */
794
795 switch (enmCmd)
796 {
797 /*
798 * Halt is answered by an event say that we've halted.
799 */
800 case DBGFCMD_HALT:
801 {
802 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_HALT_DONE;
803 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
804 fSendEvent = true;
805 fResume = false;
806 break;
807 }
808
809
810 /*
811 * Resume is not answered we'll just resume execution.
812 */
813 case DBGFCMD_GO:
814 {
815 /** @todo SMP */
816 PVMCPU pVCpu = VMMGetCpu0(pVM);
817 pVCpu->dbgf.s.fSingleSteppingRaw = false;
818 fSendEvent = false;
819 fResume = true;
820 break;
821 }
822
823 /** @todo implement (and define) the rest of the commands. */
824
825 /*
826 * Disable breakpoints and stuff.
827 * Send an everythings cool event to the debugger thread and resume execution.
828 */
829 case DBGFCMD_DETACH_DEBUGGER:
830 {
831 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, false);
832 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_DETACH_DONE;
833 pVM->dbgf.s.DbgEvent.enmCtx = DBGFEVENTCTX_OTHER;
834 fSendEvent = true;
835 fResume = true;
836 break;
837 }
838
839 /*
840 * The debugger has detached successfully.
841 * There is no reply to this event.
842 */
843 case DBGFCMD_DETACHED_DEBUGGER:
844 {
845 fSendEvent = false;
846 fResume = true;
847 break;
848 }
849
850 /*
851 * Single step, with trace into.
852 */
853 case DBGFCMD_SINGLE_STEP:
854 {
855 Log2(("Single step\n"));
856 rc = VINF_EM_DBG_STEP;
857 /** @todo SMP */
858 PVMCPU pVCpu = VMMGetCpu0(pVM);
859 pVCpu->dbgf.s.fSingleSteppingRaw = true;
860 fSendEvent = false;
861 fResume = true;
862 break;
863 }
864
865 /*
866 * Default is to send an invalid command event.
867 */
868 default:
869 {
870 pVM->dbgf.s.DbgEvent.enmType = DBGFEVENT_INVALID_COMMAND;
871 pVM->dbgf.s.DbgEvent.enmCtx = dbgfR3FigureEventCtx(pVM);
872 fSendEvent = true;
873 fResume = false;
874 break;
875 }
876 }
877
878 /*
879 * Send pending event.
880 */
881 if (fSendEvent)
882 {
883 Log2(("DBGF: Emulation thread: sending event %d\n", pVM->dbgf.s.DbgEvent.enmType));
884 int rc2 = RTSemPing(&pVM->dbgf.s.PingPong);
885 if (RT_FAILURE(rc2))
886 {
887 AssertRC(rc2);
888 *pfResumeExecution = true;
889 return rc2;
890 }
891 }
892
893 /*
894 * Return.
895 */
896 *pfResumeExecution = fResume;
897 return rc;
898}
899
900
901/**
902 * Attaches a debugger to the specified VM.
903 *
904 * Only one debugger at a time.
905 *
906 * @returns VBox status code.
907 * @param pUVM The user mode VM handle.
908 */
909VMMR3DECL(int) DBGFR3Attach(PUVM pUVM)
910{
911 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
912 PVM pVM = pUVM->pVM;
913 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
914
915 /*
916 * Call the VM, use EMT for serialization.
917 */
918 /** @todo SMP */
919 return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)dbgfR3Attach, 1, pVM);
920}
921
922
923/**
924 * EMT worker for DBGFR3Attach.
925 *
926 * @returns VBox status code.
927 * @param pVM Pointer to the VM.
928 */
929static DECLCALLBACK(int) dbgfR3Attach(PVM pVM)
930{
931 if (pVM->dbgf.s.fAttached)
932 {
933 Log(("dbgR3Attach: Debugger already attached\n"));
934 return VERR_DBGF_ALREADY_ATTACHED;
935 }
936
937 /*
938 * Create the Ping-Pong structure.
939 */
940 int rc = RTSemPingPongInit(&pVM->dbgf.s.PingPong);
941 AssertRCReturn(rc, rc);
942
943 /*
944 * Set the attached flag.
945 */
946 ASMAtomicWriteBool(&pVM->dbgf.s.fAttached, true);
947 return VINF_SUCCESS;
948}
949
950
951/**
952 * Detaches a debugger from the specified VM.
953 *
954 * Caller must be attached to the VM.
955 *
956 * @returns VBox status code.
957 * @param pUVM The user mode VM handle.
958 */
959VMMR3DECL(int) DBGFR3Detach(PUVM pUVM)
960{
961 LogFlow(("DBGFR3Detach:\n"));
962 int rc;
963
964 /*
965 * Validate input. The UVM handle shall be valid, the VM handle might be
966 * in the processes of being destroyed already, so deal quietly with that.
967 */
968 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
969 PVM pVM = pUVM->pVM;
970 if (!VM_IS_VALID_EXT(pVM))
971 return VERR_INVALID_VM_HANDLE;
972
973 /*
974 * Check if attached.
975 */
976 if (!pVM->dbgf.s.fAttached)
977 return VERR_DBGF_NOT_ATTACHED;
978
979 /*
980 * Try send the detach command.
981 * Keep in mind that we might be racing EMT, so, be extra careful.
982 */
983 DBGFCMD enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACH_DEBUGGER);
984 if (RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong))
985 {
986 rc = RTSemPong(&pVM->dbgf.s.PingPong);
987 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
988 LogRel(("DBGFR3Detach: enmCmd=%d (pong -> ping)\n", enmCmd));
989 }
990
991 /*
992 * Wait for the OK event.
993 */
994 rc = RTSemPongWait(&pVM->dbgf.s.PingPong, RT_INDEFINITE_WAIT);
995 AssertLogRelMsgRCReturn(rc, ("Wait on detach command failed, rc=%Rrc\n", rc), rc);
996
997 /*
998 * Send the notification command indicating that we're really done.
999 */
1000 enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_DETACHED_DEBUGGER);
1001 rc = RTSemPong(&pVM->dbgf.s.PingPong);
1002 AssertMsgRCReturn(rc, ("Failed to signal emulation thread. rc=%Rrc\n", rc), rc);
1003
1004 LogFlowFunc(("returns VINF_SUCCESS\n"));
1005 return VINF_SUCCESS;
1006}
1007
1008
1009/**
1010 * Wait for a debug event.
1011 *
1012 * @returns VBox status. Will not return VBOX_INTERRUPTED.
1013 * @param pUVM The user mode VM handle.
1014 * @param cMillies Number of millis to wait.
1015 * @param ppEvent Where to store the event pointer.
1016 */
1017VMMR3DECL(int) DBGFR3EventWait(PUVM pUVM, RTMSINTERVAL cMillies, PCDBGFEVENT *ppEvent)
1018{
1019 /*
1020 * Check state.
1021 */
1022 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1023 PVM pVM = pUVM->pVM;
1024 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1025 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1026 *ppEvent = NULL;
1027
1028 /*
1029 * Wait.
1030 */
1031 int rc = RTSemPongWait(&pVM->dbgf.s.PingPong, cMillies);
1032 if (RT_SUCCESS(rc))
1033 {
1034 *ppEvent = &pVM->dbgf.s.DbgEvent;
1035 Log2(("DBGF: Debugger thread: receiving event %d\n", (*ppEvent)->enmType));
1036 return VINF_SUCCESS;
1037 }
1038
1039 return rc;
1040}
1041
1042
1043/**
1044 * Halts VM execution.
1045 *
1046 * After calling this the VM isn't actually halted till an DBGFEVENT_HALT_DONE
1047 * arrives. Until that time it's not possible to issue any new commands.
1048 *
1049 * @returns VBox status.
1050 * @param pUVM The user mode VM handle.
1051 */
1052VMMR3DECL(int) DBGFR3Halt(PUVM pUVM)
1053{
1054 /*
1055 * Check state.
1056 */
1057 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1058 PVM pVM = pUVM->pVM;
1059 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1060 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1061 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1062 if ( enmSpeaker == RTPINGPONGSPEAKER_PONG
1063 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED)
1064 return VWRN_DBGF_ALREADY_HALTED;
1065
1066 /*
1067 * Send command.
1068 */
1069 dbgfR3SetCmd(pVM, DBGFCMD_HALT);
1070
1071 return VINF_SUCCESS;
1072}
1073
1074
1075/**
1076 * Checks if the VM is halted by the debugger.
1077 *
1078 * @returns True if halted.
1079 * @returns False if not halted.
1080 * @param pUVM The user mode VM handle.
1081 */
1082VMMR3DECL(bool) DBGFR3IsHalted(PUVM pUVM)
1083{
1084 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1085 PVM pVM = pUVM->pVM;
1086 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1087 AssertReturn(pVM->dbgf.s.fAttached, false);
1088
1089 RTPINGPONGSPEAKER enmSpeaker = pVM->dbgf.s.PingPong.enmSpeaker;
1090 return enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1091 || enmSpeaker == RTPINGPONGSPEAKER_PONG;
1092}
1093
1094
1095/**
1096 * Checks if the debugger can wait for events or not.
1097 *
1098 * This function is only used by lazy, multiplexing debuggers. :-)
1099 *
1100 * @returns VBox status code.
1101 * @retval VINF_SUCCESS if waitable.
1102 * @retval VERR_SEM_OUT_OF_TURN if not waitable.
1103 * @retval VERR_INVALID_VM_HANDLE if the VM is being (/ has been) destroyed
1104 * (not asserted) or if the handle is invalid (asserted).
1105 * @retval VERR_DBGF_NOT_ATTACHED if not attached.
1106 *
1107 * @param pUVM The user mode VM handle.
1108 */
1109VMMR3DECL(int) DBGFR3QueryWaitable(PUVM pUVM)
1110{
1111 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1112
1113 /* Note! There is a slight race here, unfortunately. */
1114 PVM pVM = pUVM->pVM;
1115 if (!RT_VALID_PTR(pVM))
1116 return VERR_INVALID_VM_HANDLE;
1117 if (pVM->enmVMState >= VMSTATE_DESTROYING)
1118 return VERR_INVALID_VM_HANDLE;
1119 if (!pVM->dbgf.s.fAttached)
1120 return VERR_DBGF_NOT_ATTACHED;
1121
1122 if (!RTSemPongShouldWait(&pVM->dbgf.s.PingPong))
1123 return VERR_SEM_OUT_OF_TURN;
1124
1125 return VINF_SUCCESS;
1126}
1127
1128
1129/**
1130 * Resumes VM execution.
1131 *
1132 * There is no receipt event on this command.
1133 *
1134 * @returns VBox status.
1135 * @param pUVM The user mode VM handle.
1136 */
1137VMMR3DECL(int) DBGFR3Resume(PUVM pUVM)
1138{
1139 /*
1140 * Check state.
1141 */
1142 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1143 PVM pVM = pUVM->pVM;
1144 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1145 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1146 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1147
1148 /*
1149 * Send the ping back to the emulation thread telling it to run.
1150 */
1151 dbgfR3SetCmd(pVM, DBGFCMD_GO);
1152 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1153 AssertRC(rc);
1154
1155 return rc;
1156}
1157
1158
1159/**
1160 * Step Into.
1161 *
1162 * A single step event is generated from this command.
1163 * The current implementation is not reliable, so don't rely on the event coming.
1164 *
1165 * @returns VBox status.
1166 * @param pUVM The user mode VM handle.
1167 * @param idCpu The ID of the CPU to single step on.
1168 */
1169VMMR3DECL(int) DBGFR3Step(PUVM pUVM, VMCPUID idCpu)
1170{
1171 /*
1172 * Check state.
1173 */
1174 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1175 PVM pVM = pUVM->pVM;
1176 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1177 AssertReturn(pVM->dbgf.s.fAttached, VERR_DBGF_NOT_ATTACHED);
1178 AssertReturn(RTSemPongIsSpeaker(&pVM->dbgf.s.PingPong), VERR_SEM_OUT_OF_TURN);
1179 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
1180
1181 /*
1182 * Send the ping back to the emulation thread telling it to run.
1183 */
1184/** @todo SMP (idCpu) */
1185 dbgfR3SetCmd(pVM, DBGFCMD_SINGLE_STEP);
1186 int rc = RTSemPong(&pVM->dbgf.s.PingPong);
1187 AssertRC(rc);
1188 return rc;
1189}
1190
1191
1192/**
1193 * Call this to single step programmatically.
1194 *
1195 * You must pass down the return code to the EM loop! That's
1196 * where the actual single stepping take place (at least in the
1197 * current implementation).
1198 *
1199 * @returns VINF_EM_DBG_STEP
1200 *
1201 * @param pVCpu Pointer to the VMCPU.
1202 *
1203 * @thread VCpu EMT
1204 * @internal
1205 */
1206VMMR3_INT_DECL(int) DBGFR3PrgStep(PVMCPU pVCpu)
1207{
1208 VMCPU_ASSERT_EMT(pVCpu);
1209
1210 pVCpu->dbgf.s.fSingleSteppingRaw = true;
1211 return VINF_EM_DBG_STEP;
1212}
1213
1214
1215/**
1216 * Inject an NMI into a running VM (only VCPU 0!)
1217 *
1218 * @returns VBox status code.
1219 * @param pVM Pointer to the VM.
1220 */
1221VMMR3DECL(int) DBGFR3InjectNMI(PUVM pUVM, VMCPUID idCpu)
1222{
1223 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1224 PVM pVM = pUVM->pVM;
1225 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1226 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1227
1228 /** @todo Implement generic NMI injection. */
1229 if (!HMIsEnabled(pVM))
1230 return VERR_NOT_SUP_IN_RAW_MODE;
1231
1232 VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_INTERRUPT_NMI);
1233 return VINF_SUCCESS;
1234}
1235
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