VirtualBox

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

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

Action flag updates

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