VirtualBox

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

Last change on this file since 14493 was 14299, checked in by vboxsync, 16 years ago

Corrected grammos in comments. No code changes.

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