VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/MachineDebuggerImpl.cpp@ 63475

Last change on this file since 63475 was 63244, checked in by vboxsync, 8 years ago

Main: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 49.7 KB
Line 
1/* $Id: MachineDebuggerImpl.cpp 63244 2016-08-10 10:36:09Z vboxsync $ */
2/** @file
3 * VBox IMachineDebugger COM class implementation (VBoxC).
4 */
5
6/*
7 * Copyright (C) 2006-2016 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "MachineDebuggerImpl.h"
23
24#include "Global.h"
25#include "ConsoleImpl.h"
26
27#include "AutoCaller.h"
28#include "Logging.h"
29
30#include <VBox/vmm/em.h>
31#include <VBox/vmm/patm.h>
32#include <VBox/vmm/csam.h>
33#include <VBox/vmm/uvm.h>
34#include <VBox/vmm/tm.h>
35#include <VBox/vmm/hm.h>
36#include <VBox/err.h>
37#include <iprt/cpp/utils.h>
38
39
40// constructor / destructor
41/////////////////////////////////////////////////////////////////////////////
42
43MachineDebugger::MachineDebugger()
44 : mParent(NULL)
45{
46}
47
48MachineDebugger::~MachineDebugger()
49{
50}
51
52HRESULT MachineDebugger::FinalConstruct()
53{
54 unconst(mParent) = NULL;
55 return BaseFinalConstruct();
56}
57
58void MachineDebugger::FinalRelease()
59{
60 uninit();
61 BaseFinalRelease();
62}
63
64// public initializer/uninitializer for internal purposes only
65/////////////////////////////////////////////////////////////////////////////
66
67/**
68 * Initializes the machine debugger object.
69 *
70 * @returns COM result indicator
71 * @param aParent handle of our parent object
72 */
73HRESULT MachineDebugger::init(Console *aParent)
74{
75 LogFlowThisFunc(("aParent=%p\n", aParent));
76
77 ComAssertRet(aParent, E_INVALIDARG);
78
79 /* Enclose the state transition NotReady->InInit->Ready */
80 AutoInitSpan autoInitSpan(this);
81 AssertReturn(autoInitSpan.isOk(), E_FAIL);
82
83 unconst(mParent) = aParent;
84
85 for (unsigned i = 0; i < RT_ELEMENTS(maiQueuedEmExecPolicyParams); i++)
86 maiQueuedEmExecPolicyParams[i] = UINT8_MAX;
87 mSingleStepQueued = -1;
88 mRecompileUserQueued = -1;
89 mRecompileSupervisorQueued = -1;
90 mPatmEnabledQueued = -1;
91 mCsamEnabledQueued = -1;
92 mLogEnabledQueued = -1;
93 mVirtualTimeRateQueued = UINT32_MAX;
94 mFlushMode = false;
95
96 /* Confirm a successful initialization */
97 autoInitSpan.setSucceeded();
98
99 return S_OK;
100}
101
102/**
103 * Uninitializes the instance and sets the ready flag to FALSE.
104 * Called either from FinalRelease() or by the parent when it gets destroyed.
105 */
106void MachineDebugger::uninit()
107{
108 LogFlowThisFunc(("\n"));
109
110 /* Enclose the state transition Ready->InUninit->NotReady */
111 AutoUninitSpan autoUninitSpan(this);
112 if (autoUninitSpan.uninitDone())
113 return;
114
115 unconst(mParent) = NULL;
116 mFlushMode = false;
117}
118
119// IMachineDebugger properties
120/////////////////////////////////////////////////////////////////////////////
121
122/**
123 * Returns the current singlestepping flag.
124 *
125 * @returns COM status code
126 * @param a_fEnabled Where to store the result.
127 */
128HRESULT MachineDebugger::getSingleStep(BOOL *aSingleStep)
129{
130 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
131 Console::SafeVMPtr ptrVM(mParent);
132 HRESULT hrc = ptrVM.rc();
133 if (SUCCEEDED(hrc))
134 {
135 RT_NOREF(aSingleStep); /** @todo */
136 ReturnComNotImplemented();
137 }
138 return hrc;
139}
140
141/**
142 * Sets the singlestepping flag.
143 *
144 * @returns COM status code
145 * @param a_fEnable The new state.
146 */
147HRESULT MachineDebugger::setSingleStep(BOOL aSingleStep)
148{
149 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
150 Console::SafeVMPtr ptrVM(mParent);
151 HRESULT hrc = ptrVM.rc();
152 if (SUCCEEDED(hrc))
153 {
154 NOREF(aSingleStep); /** @todo */
155 ReturnComNotImplemented();
156 }
157 return hrc;
158}
159
160/**
161 * Internal worker for getting an EM executable policy setting.
162 *
163 * @returns COM status code.
164 * @param enmPolicy Which EM policy.
165 * @param pfEnforced Where to return the policy setting.
166 */
167HRESULT MachineDebugger::i_getEmExecPolicyProperty(EMEXECPOLICY enmPolicy, BOOL *pfEnforced)
168{
169 CheckComArgOutPointerValid(pfEnforced);
170
171 AutoCaller autoCaller(this);
172 HRESULT hrc = autoCaller.rc();
173 if (SUCCEEDED(hrc))
174 {
175 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
176 if (i_queueSettings())
177 *pfEnforced = maiQueuedEmExecPolicyParams[enmPolicy] == 1;
178 else
179 {
180 bool fEnforced = false;
181 Console::SafeVMPtrQuiet ptrVM(mParent);
182 hrc = ptrVM.rc();
183 if (SUCCEEDED(hrc))
184 EMR3QueryExecutionPolicy(ptrVM.rawUVM(), enmPolicy, &fEnforced);
185 *pfEnforced = fEnforced;
186 }
187 }
188 return hrc;
189}
190
191/**
192 * Internal worker for setting an EM executable policy.
193 *
194 * @returns COM status code.
195 * @param enmPolicy Which policy to change.
196 * @param fEnforce Whether to enforce the policy or not.
197 */
198HRESULT MachineDebugger::i_setEmExecPolicyProperty(EMEXECPOLICY enmPolicy, BOOL fEnforce)
199{
200 AutoCaller autoCaller(this);
201 HRESULT hrc = autoCaller.rc();
202 if (SUCCEEDED(hrc))
203 {
204 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
205 if (i_queueSettings())
206 maiQueuedEmExecPolicyParams[enmPolicy] = fEnforce ? 1 : 0;
207 else
208 {
209 Console::SafeVMPtrQuiet ptrVM(mParent);
210 hrc = ptrVM.rc();
211 if (SUCCEEDED(hrc))
212 {
213 int vrc = EMR3SetExecutionPolicy(ptrVM.rawUVM(), enmPolicy, fEnforce != FALSE);
214 if (RT_FAILURE(vrc))
215 hrc = setError(VBOX_E_VM_ERROR, tr("EMR3SetExecutionPolicy failed with %Rrc"), vrc);
216 }
217 }
218 }
219 return hrc;
220}
221
222/**
223 * Returns the current recompile user mode code flag.
224 *
225 * @returns COM status code
226 * @param a_fEnabled address of result variable
227 */
228HRESULT MachineDebugger::getRecompileUser(BOOL *aRecompileUser)
229{
230 return i_getEmExecPolicyProperty(EMEXECPOLICY_RECOMPILE_RING3, aRecompileUser);
231}
232
233/**
234 * Sets the recompile user mode code flag.
235 *
236 * @returns COM status
237 * @param aEnable new user mode code recompile flag.
238 */
239HRESULT MachineDebugger::setRecompileUser(BOOL aRecompileUser)
240{
241 LogFlowThisFunc(("enable=%d\n", aRecompileUser));
242 return i_setEmExecPolicyProperty(EMEXECPOLICY_RECOMPILE_RING3, aRecompileUser);
243}
244
245/**
246 * Returns the current recompile supervisor code flag.
247 *
248 * @returns COM status code
249 * @param aEnabled address of result variable
250 */
251HRESULT MachineDebugger::getRecompileSupervisor(BOOL *aRecompileSupervisor)
252{
253 return i_getEmExecPolicyProperty(EMEXECPOLICY_RECOMPILE_RING0, aRecompileSupervisor);
254}
255
256/**
257 * Sets the new recompile supervisor code flag.
258 *
259 * @returns COM status code
260 * @param aEnable new recompile supervisor code flag
261 */
262HRESULT MachineDebugger::setRecompileSupervisor(BOOL aRecompileSupervisor)
263{
264 LogFlowThisFunc(("enable=%d\n", aRecompileSupervisor));
265 return i_setEmExecPolicyProperty(EMEXECPOLICY_RECOMPILE_RING0, aRecompileSupervisor);
266}
267
268/**
269 * Returns the current execute-all-in-IEM setting.
270 *
271 * @returns COM status code
272 * @param aEnabled Address of result variable.
273 */
274HRESULT MachineDebugger::getExecuteAllInIEM(BOOL *aExecuteAllInIEM)
275{
276 return i_getEmExecPolicyProperty(EMEXECPOLICY_IEM_ALL, aExecuteAllInIEM);
277}
278
279/**
280 * Changes the execute-all-in-IEM setting.
281 *
282 * @returns COM status code
283 * @param aEnable New setting.
284 */
285HRESULT MachineDebugger::setExecuteAllInIEM(BOOL aExecuteAllInIEM)
286{
287 LogFlowThisFunc(("enable=%d\n", aExecuteAllInIEM));
288 return i_setEmExecPolicyProperty(EMEXECPOLICY_IEM_ALL, aExecuteAllInIEM);
289}
290
291/**
292 * Returns the current patch manager enabled flag.
293 *
294 * @returns COM status code
295 * @param aEnabled address of result variable
296 */
297HRESULT MachineDebugger::getPATMEnabled(BOOL *aPATMEnabled)
298{
299#ifdef VBOX_WITH_RAW_MODE
300 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
301
302 Console::SafeVMPtrQuiet ptrVM(mParent);
303 if (ptrVM.isOk())
304 *aPATMEnabled = PATMR3IsEnabled(ptrVM.rawUVM());
305 else
306#endif
307 *aPATMEnabled = false;
308
309 return S_OK;
310}
311
312/**
313 * Set the new patch manager enabled flag.
314 *
315 * @returns COM status code
316 * @param aEnable new patch manager enabled flag
317 */
318HRESULT MachineDebugger::setPATMEnabled(BOOL aPATMEnabled)
319{
320 LogFlowThisFunc(("enable=%d\n", aPATMEnabled));
321
322#ifdef VBOX_WITH_RAW_MODE
323 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
324
325 if (i_queueSettings())
326 {
327 // queue the request
328 mPatmEnabledQueued = aPATMEnabled;
329 return S_OK;
330 }
331
332 Console::SafeVMPtr ptrVM(mParent);
333 if (FAILED(ptrVM.rc()))
334 return ptrVM.rc();
335
336 int vrc = PATMR3AllowPatching(ptrVM.rawUVM(), RT_BOOL(aPATMEnabled));
337 if (RT_FAILURE(vrc))
338 return setError(VBOX_E_VM_ERROR, tr("PATMR3AllowPatching returned %Rrc"), vrc);
339
340#else /* !VBOX_WITH_RAW_MODE */
341 if (aPATMEnabled)
342 return setError(VBOX_E_VM_ERROR, tr("PATM not present"), VERR_NOT_SUPPORTED);
343#endif /* !VBOX_WITH_RAW_MODE */
344 return S_OK;
345}
346
347/**
348 * Returns the current code scanner enabled flag.
349 *
350 * @returns COM status code
351 * @param aEnabled address of result variable
352 */
353HRESULT MachineDebugger::getCSAMEnabled(BOOL *aCSAMEnabled)
354{
355#ifdef VBOX_WITH_RAW_MODE
356 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
357
358 Console::SafeVMPtrQuiet ptrVM(mParent);
359
360 if (ptrVM.isOk())
361 *aCSAMEnabled = CSAMR3IsEnabled(ptrVM.rawUVM());
362 else
363#endif /* VBOX_WITH_RAW_MODE */
364 *aCSAMEnabled = false;
365
366 return S_OK;
367}
368
369/**
370 * Sets the new code scanner enabled flag.
371 *
372 * @returns COM status code
373 * @param aEnable new code scanner enabled flag
374 */
375HRESULT MachineDebugger::setCSAMEnabled(BOOL aCSAMEnabled)
376{
377 LogFlowThisFunc(("enable=%d\n", aCSAMEnabled));
378
379#ifdef VBOX_WITH_RAW_MODE
380 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
381
382 if (i_queueSettings())
383 {
384 // queue the request
385 mCsamEnabledQueued = aCSAMEnabled;
386 return S_OK;
387 }
388
389 Console::SafeVMPtr ptrVM(mParent);
390 if (FAILED(ptrVM.rc()))
391 return ptrVM.rc();
392
393 int vrc = CSAMR3SetScanningEnabled(ptrVM.rawUVM(), aCSAMEnabled != FALSE);
394 if (RT_FAILURE(vrc))
395 return setError(VBOX_E_VM_ERROR, tr("CSAMR3SetScanningEnabled returned %Rrc"), vrc);
396
397#else /* !VBOX_WITH_RAW_MODE */
398 if (aCSAMEnabled)
399 return setError(VBOX_E_VM_ERROR, tr("CASM not present"), VERR_NOT_SUPPORTED);
400#endif /* !VBOX_WITH_RAW_MODE */
401 return S_OK;
402}
403
404/**
405 * Returns the log enabled / disabled status.
406 *
407 * @returns COM status code
408 * @param aLogEnabled address of result variable
409 */
410HRESULT MachineDebugger::getLogEnabled(BOOL *aLogEnabled)
411{
412#ifdef LOG_ENABLED
413 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
414
415 const PRTLOGGER pLogInstance = RTLogDefaultInstance();
416 *aLogEnabled = pLogInstance && !(pLogInstance->fFlags & RTLOGFLAGS_DISABLED);
417#else
418 *aLogEnabled = false;
419#endif
420
421 return S_OK;
422}
423
424/**
425 * Enables or disables logging.
426 *
427 * @returns COM status code
428 * @param aLogEnabled The new code log state.
429 */
430HRESULT MachineDebugger::setLogEnabled(BOOL aLogEnabled)
431{
432 LogFlowThisFunc(("aLogEnabled=%d\n", aLogEnabled));
433
434 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
435
436 if (i_queueSettings())
437 {
438 // queue the request
439 mLogEnabledQueued = aLogEnabled;
440 return S_OK;
441 }
442
443 Console::SafeVMPtr ptrVM(mParent);
444 if (FAILED(ptrVM.rc())) return ptrVM.rc();
445
446#ifdef LOG_ENABLED
447 int vrc = DBGFR3LogModifyFlags(ptrVM.rawUVM(), aLogEnabled ? "enabled" : "disabled");
448 if (RT_FAILURE(vrc))
449 {
450 /** @todo handle error code. */
451 }
452#endif
453
454 return S_OK;
455}
456
457HRESULT MachineDebugger::i_logStringProps(PRTLOGGER pLogger, PFNLOGGETSTR pfnLogGetStr,
458 const char *pszLogGetStr, Utf8Str *pstrSettings)
459{
460 /* Make sure the VM is powered up. */
461 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
462 Console::SafeVMPtr ptrVM(mParent);
463 HRESULT hrc = ptrVM.rc();
464 if (FAILED(hrc))
465 return hrc;
466
467 /* Make sure we've got a logger. */
468 if (!pLogger)
469 {
470 *pstrSettings = "";
471 return S_OK;
472 }
473
474 /* Do the job. */
475 size_t cbBuf = _1K;
476 for (;;)
477 {
478 char *pszBuf = (char *)RTMemTmpAlloc(cbBuf);
479 AssertReturn(pszBuf, E_OUTOFMEMORY);
480 int vrc = pstrSettings->reserveNoThrow(cbBuf);
481 if (RT_SUCCESS(vrc))
482 {
483 vrc = pfnLogGetStr(pLogger, pstrSettings->mutableRaw(), cbBuf);
484 if (RT_SUCCESS(vrc))
485 {
486 pstrSettings->jolt();
487 return S_OK;
488 }
489 *pstrSettings = "";
490 AssertReturn(vrc == VERR_BUFFER_OVERFLOW, setError(VBOX_E_IPRT_ERROR, tr("%s returned %Rrc"), pszLogGetStr, vrc));
491 }
492 else
493 return E_OUTOFMEMORY;
494
495 /* try again with a bigger buffer. */
496 cbBuf *= 2;
497 AssertReturn(cbBuf <= _256K, setError(E_FAIL, tr("%s returns too much data"), pszLogGetStr));
498 }
499}
500
501HRESULT MachineDebugger::getLogDbgFlags(com::Utf8Str &aLogDbgFlags)
502{
503 return i_logStringProps(RTLogGetDefaultInstance(), RTLogGetFlags, "RTGetFlags", &aLogDbgFlags);
504}
505
506HRESULT MachineDebugger::getLogDbgGroups(com::Utf8Str &aLogDbgGroups)
507{
508 return i_logStringProps(RTLogGetDefaultInstance(), RTLogGetGroupSettings, "RTLogGetGroupSettings", &aLogDbgGroups);
509}
510
511HRESULT MachineDebugger::getLogDbgDestinations(com::Utf8Str &aLogDbgDestinations)
512{
513 return i_logStringProps(RTLogGetDefaultInstance(), RTLogGetDestinations, "RTLogGetDestinations", &aLogDbgDestinations);
514}
515
516HRESULT MachineDebugger::getLogRelFlags(com::Utf8Str &aLogRelFlags)
517{
518 return i_logStringProps(RTLogRelGetDefaultInstance(), RTLogGetFlags, "RTGetFlags", &aLogRelFlags);
519}
520
521HRESULT MachineDebugger::getLogRelGroups(com::Utf8Str &aLogRelGroups)
522{
523 return i_logStringProps(RTLogRelGetDefaultInstance(), RTLogGetGroupSettings, "RTLogGetGroupSettings", &aLogRelGroups);
524}
525
526HRESULT MachineDebugger::getLogRelDestinations(com::Utf8Str &aLogRelDestinations)
527{
528 return i_logStringProps(RTLogRelGetDefaultInstance(), RTLogGetDestinations, "RTLogGetDestinations", &aLogRelDestinations);
529}
530
531/**
532 * Returns the current hardware virtualization flag.
533 *
534 * @returns COM status code
535 * @param aEnabled address of result variable
536 */
537HRESULT MachineDebugger::getHWVirtExEnabled(BOOL *aHWVirtExEnabled)
538{
539 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
540
541 Console::SafeVMPtrQuiet ptrVM(mParent);
542
543 if (ptrVM.isOk())
544 *aHWVirtExEnabled = HMR3IsEnabled(ptrVM.rawUVM());
545 else
546 *aHWVirtExEnabled = false;
547
548 return S_OK;
549}
550
551/**
552 * Returns the current nested paging flag.
553 *
554 * @returns COM status code
555 * @param aEnabled address of result variable
556 */
557HRESULT MachineDebugger::getHWVirtExNestedPagingEnabled(BOOL *aHWVirtExNestedPagingEnabled)
558{
559 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
560
561 Console::SafeVMPtrQuiet ptrVM(mParent);
562
563 if (ptrVM.isOk())
564 *aHWVirtExNestedPagingEnabled = HMR3IsNestedPagingActive(ptrVM.rawUVM());
565 else
566 *aHWVirtExNestedPagingEnabled = false;
567
568 return S_OK;
569}
570
571/**
572 * Returns the current VPID flag.
573 *
574 * @returns COM status code
575 * @param aEnabled address of result variable
576 */
577HRESULT MachineDebugger::getHWVirtExVPIDEnabled(BOOL *aHWVirtExVPIDEnabled)
578{
579 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
580
581 Console::SafeVMPtrQuiet ptrVM(mParent);
582
583 if (ptrVM.isOk())
584 *aHWVirtExVPIDEnabled = HMR3IsVpidActive(ptrVM.rawUVM());
585 else
586 *aHWVirtExVPIDEnabled = false;
587
588 return S_OK;
589}
590
591/**
592 * Returns the current unrestricted execution setting.
593 *
594 * @returns COM status code
595 * @param aEnabled address of result variable
596 */
597HRESULT MachineDebugger::getHWVirtExUXEnabled(BOOL *aHWVirtExUXEnabled)
598{
599 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
600
601 Console::SafeVMPtrQuiet ptrVM(mParent);
602
603 if (ptrVM.isOk())
604 *aHWVirtExUXEnabled = HMR3IsUXActive(ptrVM.rawUVM());
605 else
606 *aHWVirtExUXEnabled = false;
607
608 return S_OK;
609}
610
611HRESULT MachineDebugger::getOSName(com::Utf8Str &aOSName)
612{
613 LogFlowThisFunc(("\n"));
614 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
615 Console::SafeVMPtr ptrVM(mParent);
616 HRESULT hrc = ptrVM.rc();
617 if (SUCCEEDED(hrc))
618 {
619 /*
620 * Do the job and try convert the name.
621 */
622 char szName[64];
623 int vrc = DBGFR3OSQueryNameAndVersion(ptrVM.rawUVM(), szName, sizeof(szName), NULL, 0);
624 if (RT_SUCCESS(vrc))
625 {
626 try
627 {
628 Bstr bstrName(szName);
629 aOSName = Utf8Str(bstrName);
630 }
631 catch (std::bad_alloc)
632 {
633 hrc = E_OUTOFMEMORY;
634 }
635 }
636 else
637 hrc = setError(VBOX_E_VM_ERROR, tr("DBGFR3OSQueryNameAndVersion failed with %Rrc"), vrc);
638 }
639 return hrc;
640}
641
642HRESULT MachineDebugger::getOSVersion(com::Utf8Str &aOSVersion)
643{
644 LogFlowThisFunc(("\n"));
645 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
646 Console::SafeVMPtr ptrVM(mParent);
647 HRESULT hrc = ptrVM.rc();
648 if (SUCCEEDED(hrc))
649 {
650 /*
651 * Do the job and try convert the name.
652 */
653 char szVersion[256];
654 int vrc = DBGFR3OSQueryNameAndVersion(ptrVM.rawUVM(), NULL, 0, szVersion, sizeof(szVersion));
655 if (RT_SUCCESS(vrc))
656 {
657 try
658 {
659 Bstr bstrVersion(szVersion);
660 aOSVersion = Utf8Str(bstrVersion);
661 }
662 catch (std::bad_alloc)
663 {
664 hrc = E_OUTOFMEMORY;
665 }
666 }
667 else
668 hrc = setError(VBOX_E_VM_ERROR, tr("DBGFR3OSQueryNameAndVersion failed with %Rrc"), vrc);
669 }
670 return hrc;
671}
672
673/**
674 * Returns the current PAE flag.
675 *
676 * @returns COM status code
677 * @param aEnabled address of result variable
678 */
679HRESULT MachineDebugger::getPAEEnabled(BOOL *aPAEEnabled)
680{
681 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
682
683 Console::SafeVMPtrQuiet ptrVM(mParent);
684
685 if (ptrVM.isOk())
686 {
687 uint32_t cr4;
688 int rc = DBGFR3RegCpuQueryU32(ptrVM.rawUVM(), 0 /*idCpu*/, DBGFREG_CR4, &cr4); AssertRC(rc);
689 *aPAEEnabled = RT_BOOL(cr4 & X86_CR4_PAE);
690 }
691 else
692 *aPAEEnabled = false;
693
694 return S_OK;
695}
696
697/**
698 * Returns the current virtual time rate.
699 *
700 * @returns COM status code.
701 * @param a_puPct Where to store the rate.
702 */
703HRESULT MachineDebugger::getVirtualTimeRate(ULONG *aVirtualTimeRate)
704{
705 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
706
707 Console::SafeVMPtr ptrVM(mParent);
708 HRESULT hrc = ptrVM.rc();
709 if (SUCCEEDED(hrc))
710 *aVirtualTimeRate = TMR3GetWarpDrive(ptrVM.rawUVM());
711
712 return hrc;
713}
714
715/**
716 * Returns the current virtual time rate.
717 *
718 * @returns COM status code.
719 * @param aPct Where to store the rate.
720 */
721HRESULT MachineDebugger::setVirtualTimeRate(ULONG aVirtualTimeRate)
722{
723 HRESULT hrc = S_OK;
724
725 if (aVirtualTimeRate < 2 || aVirtualTimeRate > 20000)
726 return setError(E_INVALIDARG, tr("%u is out of range [2..20000]"), aVirtualTimeRate);
727
728 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
729 if (i_queueSettings())
730 mVirtualTimeRateQueued = aVirtualTimeRate;
731 else
732 {
733 Console::SafeVMPtr ptrVM(mParent);
734 hrc = ptrVM.rc();
735 if (SUCCEEDED(hrc))
736 {
737 int vrc = TMR3SetWarpDrive(ptrVM.rawUVM(), aVirtualTimeRate);
738 if (RT_FAILURE(vrc))
739 hrc = setError(VBOX_E_VM_ERROR, tr("TMR3SetWarpDrive(, %u) failed with rc=%Rrc"), aVirtualTimeRate, vrc);
740 }
741 }
742
743 return hrc;
744}
745
746/**
747 * Hack for getting the user mode VM handle (UVM).
748 *
749 * This is only temporary (promise) while prototyping the debugger.
750 *
751 * @returns COM status code
752 * @param aVM Where to store the vm handle. Since there is no
753 * uintptr_t in COM, we're using the max integer.
754 * (No, ULONG is not pointer sized!)
755 * @remarks The returned handle must be passed to VMR3ReleaseUVM()!
756 * @remarks Prior to 4.3 this returned PVM.
757 */
758HRESULT MachineDebugger::getVM(LONG64 *aVM)
759{
760 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
761
762 Console::SafeVMPtr ptrVM(mParent);
763 HRESULT hrc = ptrVM.rc();
764 if (SUCCEEDED(hrc))
765 {
766 VMR3RetainUVM(ptrVM.rawUVM());
767 *aVM = (intptr_t)ptrVM.rawUVM();
768 }
769
770 /*
771 * Note! ptrVM protection provided by SafeVMPtr is no long effective
772 * after we return from this method.
773 */
774 return hrc;
775}
776
777/**
778 * Get the VM uptime in milliseconds.
779 *
780 * @returns COM status code
781 * @param aUptime Where to store the uptime.
782 */
783HRESULT MachineDebugger::getUptime(LONG64 *aUptime)
784{
785 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
786
787 Console::SafeVMPtr ptrVM(mParent);
788 HRESULT hrc = ptrVM.rc();
789 if (SUCCEEDED(hrc))
790 *aUptime = (int64_t)TMR3TimeVirtGetMilli(ptrVM.rawUVM());
791
792 return hrc;
793}
794
795// IMachineDebugger methods
796/////////////////////////////////////////////////////////////////////////////
797
798HRESULT MachineDebugger::dumpGuestCore(const com::Utf8Str &aFilename, const com::Utf8Str &aCompression)
799{
800 if (aCompression.length())
801 return setError(E_INVALIDARG, tr("The compression parameter must be empty"));
802
803 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
804 Console::SafeVMPtr ptrVM(mParent);
805 HRESULT hrc = ptrVM.rc();
806 if (SUCCEEDED(hrc))
807 {
808 int vrc = DBGFR3CoreWrite(ptrVM.rawUVM(), aFilename.c_str(), false /*fReplaceFile*/);
809 if (RT_SUCCESS(vrc))
810 hrc = S_OK;
811 else
812 hrc = setError(E_FAIL, tr("DBGFR3CoreWrite failed with %Rrc"), vrc);
813 }
814
815 return hrc;
816}
817
818HRESULT MachineDebugger::dumpHostProcessCore(const com::Utf8Str &aFilename, const com::Utf8Str &aCompression)
819{
820 RT_NOREF(aFilename, aCompression);
821 ReturnComNotImplemented();
822}
823
824/**
825 * Debug info string buffer formatter.
826 */
827typedef struct MACHINEDEBUGGERINOFHLP
828{
829 /** The core info helper structure. */
830 DBGFINFOHLP Core;
831 /** Pointer to the buffer. */
832 char *pszBuf;
833 /** The size of the buffer. */
834 size_t cbBuf;
835 /** The offset into the buffer */
836 size_t offBuf;
837 /** Indicates an out-of-memory condition. */
838 bool fOutOfMemory;
839} MACHINEDEBUGGERINOFHLP;
840/** Pointer to a Debug info string buffer formatter. */
841typedef MACHINEDEBUGGERINOFHLP *PMACHINEDEBUGGERINOFHLP;
842
843
844/**
845 * @callback_method_impl{FNRTSTROUTPUT}
846 */
847static DECLCALLBACK(size_t) MachineDebuggerInfoOutput(void *pvArg, const char *pachChars, size_t cbChars)
848{
849 PMACHINEDEBUGGERINOFHLP pHlp = (PMACHINEDEBUGGERINOFHLP)pvArg;
850
851 /*
852 * Grow the buffer if required.
853 */
854 size_t const cbRequired = cbChars + pHlp->offBuf + 1;
855 if (cbRequired > pHlp->cbBuf)
856 {
857 if (RT_UNLIKELY(pHlp->fOutOfMemory))
858 return 0;
859
860 size_t cbBufNew = pHlp->cbBuf * 2;
861 if (cbRequired > cbBufNew)
862 cbBufNew = RT_ALIGN_Z(cbRequired, 256);
863 void *pvBufNew = RTMemRealloc(pHlp->pszBuf, cbBufNew);
864 if (RT_UNLIKELY(!pvBufNew))
865 {
866 pHlp->fOutOfMemory = true;
867 RTMemFree(pHlp->pszBuf);
868 pHlp->pszBuf = NULL;
869 pHlp->cbBuf = 0;
870 pHlp->offBuf = 0;
871 return 0;
872 }
873
874 pHlp->pszBuf = (char *)pvBufNew;
875 pHlp->cbBuf = cbBufNew;
876 }
877
878 /*
879 * Copy the bytes into the buffer and terminate it.
880 */
881 memcpy(&pHlp->pszBuf[pHlp->offBuf], pachChars, cbChars);
882 pHlp->offBuf += cbChars;
883 pHlp->pszBuf[pHlp->offBuf] = '\0';
884 Assert(pHlp->offBuf < pHlp->cbBuf);
885 return cbChars;
886}
887
888/**
889 * @interface_method_impl{DBGFINFOHLP,pfnPrintfV}
890 */
891static DECLCALLBACK(void) MachineDebuggerInfoPrintfV(PCDBGFINFOHLP pHlp, const char *pszFormat, va_list va)
892{
893 RTStrFormatV(MachineDebuggerInfoOutput, (void *)pHlp, NULL, NULL, pszFormat, va);
894}
895
896/**
897 * @interface_method_impl{DBGFINFOHLP,pfnPrintf}
898 */
899static DECLCALLBACK(void) MachineDebuggerInfoPrintf(PCDBGFINFOHLP pHlp, const char *pszFormat, ...)
900{
901 va_list va;
902 va_start(va, pszFormat);
903 MachineDebuggerInfoPrintfV(pHlp, pszFormat, va);
904 va_end(va);
905}
906
907/**
908 * Initializes the debug info string buffer formatter
909 *
910 * @param pHlp The help structure to init.
911 */
912static void MachineDebuggerInfoInit(PMACHINEDEBUGGERINOFHLP pHlp)
913{
914 pHlp->Core.pfnPrintf = MachineDebuggerInfoPrintf;
915 pHlp->Core.pfnPrintfV = MachineDebuggerInfoPrintfV;
916 pHlp->pszBuf = NULL;
917 pHlp->cbBuf = 0;
918 pHlp->offBuf = 0;
919 pHlp->fOutOfMemory = false;
920}
921
922/**
923 * Deletes the debug info string buffer formatter.
924 * @param pHlp The helper structure to delete.
925 */
926static void MachineDebuggerInfoDelete(PMACHINEDEBUGGERINOFHLP pHlp)
927{
928 RTMemFree(pHlp->pszBuf);
929 pHlp->pszBuf = NULL;
930}
931
932HRESULT MachineDebugger::info(const com::Utf8Str &aName, const com::Utf8Str &aArgs, com::Utf8Str &aInfo)
933{
934 LogFlowThisFunc(("\n"));
935
936 /*
937 * Do the autocaller and lock bits.
938 */
939 AutoCaller autoCaller(this);
940 HRESULT hrc = autoCaller.rc();
941 if (SUCCEEDED(hrc))
942 {
943 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
944 Console::SafeVMPtr ptrVM(mParent);
945 hrc = ptrVM.rc();
946 if (SUCCEEDED(hrc))
947 {
948 /*
949 * Create a helper and call DBGFR3Info.
950 */
951 MACHINEDEBUGGERINOFHLP Hlp;
952 MachineDebuggerInfoInit(&Hlp);
953 int vrc = DBGFR3Info(ptrVM.rawUVM(), aName.c_str(), aArgs.c_str(), &Hlp.Core);
954 if (RT_SUCCESS(vrc))
955 {
956 if (!Hlp.fOutOfMemory)
957 {
958 /*
959 * Convert the info string, watching out for allocation errors.
960 */
961 try
962 {
963 Bstr bstrInfo(Hlp.pszBuf);
964 aInfo = bstrInfo;
965 }
966 catch (std::bad_alloc)
967 {
968 hrc = E_OUTOFMEMORY;
969 }
970 }
971 else
972 hrc = E_OUTOFMEMORY;
973 }
974 else
975 hrc = setError(VBOX_E_VM_ERROR, tr("DBGFR3Info failed with %Rrc"), vrc);
976 MachineDebuggerInfoDelete(&Hlp);
977 }
978 }
979 return hrc;
980}
981
982HRESULT MachineDebugger::injectNMI()
983{
984 LogFlowThisFunc(("\n"));
985
986 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
987 Console::SafeVMPtr ptrVM(mParent);
988 HRESULT hrc = ptrVM.rc();
989 if (SUCCEEDED(hrc))
990 {
991 int vrc = DBGFR3InjectNMI(ptrVM.rawUVM(), 0);
992 if (RT_SUCCESS(vrc))
993 hrc = S_OK;
994 else
995 hrc = setError(E_FAIL, tr("DBGFR3InjectNMI failed with %Rrc"), vrc);
996 }
997 return hrc;
998}
999
1000HRESULT MachineDebugger::modifyLogFlags(const com::Utf8Str &aSettings)
1001{
1002 LogFlowThisFunc(("aSettings=%s\n", aSettings.c_str()));
1003 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1004 Console::SafeVMPtr ptrVM(mParent);
1005 HRESULT hrc = ptrVM.rc();
1006 if (SUCCEEDED(hrc))
1007 {
1008 int vrc = DBGFR3LogModifyFlags(ptrVM.rawUVM(), aSettings.c_str());
1009 if (RT_SUCCESS(vrc))
1010 hrc = S_OK;
1011 else
1012 hrc = setError(E_FAIL, tr("DBGFR3LogModifyFlags failed with %Rrc"), vrc);
1013 }
1014 return hrc;
1015}
1016
1017HRESULT MachineDebugger::modifyLogGroups(const com::Utf8Str &aSettings)
1018{
1019 LogFlowThisFunc(("aSettings=%s\n", aSettings.c_str()));
1020 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1021 Console::SafeVMPtr ptrVM(mParent);
1022 HRESULT hrc = ptrVM.rc();
1023 if (SUCCEEDED(hrc))
1024 {
1025 int vrc = DBGFR3LogModifyGroups(ptrVM.rawUVM(), aSettings.c_str());
1026 if (RT_SUCCESS(vrc))
1027 hrc = S_OK;
1028 else
1029 hrc = setError(E_FAIL, tr("DBGFR3LogModifyGroups failed with %Rrc"), vrc);
1030 }
1031 return hrc;
1032}
1033
1034HRESULT MachineDebugger::modifyLogDestinations(const com::Utf8Str &aSettings)
1035{
1036 LogFlowThisFunc(("aSettings=%s\n", aSettings.c_str()));
1037 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1038 Console::SafeVMPtr ptrVM(mParent);
1039 HRESULT hrc = ptrVM.rc();
1040 if (SUCCEEDED(hrc))
1041 {
1042 int vrc = DBGFR3LogModifyDestinations(ptrVM.rawUVM(), aSettings.c_str());
1043 if (RT_SUCCESS(vrc))
1044 hrc = S_OK;
1045 else
1046 hrc = setError(E_FAIL, tr("DBGFR3LogModifyDestinations failed with %Rrc"), vrc);
1047 }
1048 return hrc;
1049}
1050
1051HRESULT MachineDebugger::readPhysicalMemory(LONG64 aAddress, ULONG aSize, std::vector<BYTE> &aBytes)
1052{
1053 RT_NOREF(aAddress, aSize, aBytes);
1054 ReturnComNotImplemented();
1055}
1056
1057HRESULT MachineDebugger::writePhysicalMemory(LONG64 aAddress, ULONG aSize, const std::vector<BYTE> &aBytes)
1058{
1059 RT_NOREF(aAddress, aSize, aBytes);
1060 ReturnComNotImplemented();
1061}
1062
1063HRESULT MachineDebugger::readVirtualMemory(ULONG aCpuId, LONG64 aAddress, ULONG aSize, std::vector<BYTE> &aBytes)
1064{
1065 RT_NOREF(aCpuId, aAddress, aSize, aBytes);
1066 ReturnComNotImplemented();
1067}
1068
1069HRESULT MachineDebugger::writeVirtualMemory(ULONG aCpuId, LONG64 aAddress, ULONG aSize, const std::vector<BYTE> &aBytes)
1070{
1071 RT_NOREF(aCpuId, aAddress, aSize, aBytes);
1072 ReturnComNotImplemented();
1073}
1074
1075HRESULT MachineDebugger::loadPlugIn(const com::Utf8Str &aName, com::Utf8Str &aPlugInName)
1076{
1077 /*
1078 * Lock the debugger and get the VM pointer
1079 */
1080 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1081 Console::SafeVMPtr ptrVM(mParent);
1082 HRESULT hrc = ptrVM.rc();
1083 if (SUCCEEDED(hrc))
1084 {
1085 /*
1086 * Do the job and try convert the name.
1087 */
1088 if (aName.equals("all"))
1089 {
1090 DBGFR3PlugInLoadAll(ptrVM.rawUVM());
1091 try
1092 {
1093 aPlugInName = "all";
1094 hrc = S_OK;
1095 }
1096 catch (std::bad_alloc)
1097 {
1098 hrc = E_OUTOFMEMORY;
1099 }
1100 }
1101 else
1102 {
1103 RTERRINFOSTATIC ErrInfo;
1104 char szName[80];
1105 int vrc = DBGFR3PlugInLoad(ptrVM.rawUVM(), aName.c_str(), szName, sizeof(szName), RTErrInfoInitStatic(&ErrInfo));
1106 if (RT_SUCCESS(vrc))
1107 {
1108 try
1109 {
1110 aPlugInName = szName;
1111 hrc = S_OK;
1112 }
1113 catch (std::bad_alloc)
1114 {
1115 hrc = E_OUTOFMEMORY;
1116 }
1117 }
1118 else
1119 hrc = setErrorVrc(vrc, "%s", ErrInfo.szMsg);
1120 }
1121 }
1122 return hrc;
1123
1124}
1125
1126HRESULT MachineDebugger::unloadPlugIn(const com::Utf8Str &aName)
1127{
1128 /*
1129 * Lock the debugger and get the VM pointer
1130 */
1131 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1132 Console::SafeVMPtr ptrVM(mParent);
1133 HRESULT hrc = ptrVM.rc();
1134 if (SUCCEEDED(hrc))
1135 {
1136 /*
1137 * Do the job and try convert the name.
1138 */
1139 if (aName.equals("all"))
1140 {
1141 DBGFR3PlugInUnloadAll(ptrVM.rawUVM());
1142 hrc = S_OK;
1143 }
1144 else
1145 {
1146 int vrc = DBGFR3PlugInUnload(ptrVM.rawUVM(), aName.c_str());
1147 if (RT_SUCCESS(vrc))
1148 hrc = S_OK;
1149 else if (vrc == VERR_NOT_FOUND)
1150 hrc = setErrorBoth(E_FAIL, vrc, "Plug-in '%s' was not found", aName.c_str());
1151 else
1152 hrc = setErrorVrc(vrc, "Error unloading '%s': %Rrc", aName.c_str(), vrc);
1153 }
1154 }
1155 return hrc;
1156
1157}
1158
1159HRESULT MachineDebugger::detectOS(com::Utf8Str &aOs)
1160{
1161 LogFlowThisFunc(("\n"));
1162
1163 /*
1164 * Lock the debugger and get the VM pointer
1165 */
1166 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1167 Console::SafeVMPtr ptrVM(mParent);
1168 HRESULT hrc = ptrVM.rc();
1169 if (SUCCEEDED(hrc))
1170 {
1171 /*
1172 * Do the job.
1173 */
1174 char szName[64];
1175 int vrc = DBGFR3OSDetect(ptrVM.rawUVM(), szName, sizeof(szName));
1176 if (RT_SUCCESS(vrc) && vrc != VINF_DBGF_OS_NOT_DETCTED)
1177 {
1178 try
1179 {
1180 aOs = szName;
1181 }
1182 catch (std::bad_alloc)
1183 {
1184 hrc = E_OUTOFMEMORY;
1185 }
1186 }
1187 else
1188 hrc = setError(VBOX_E_VM_ERROR, tr("DBGFR3OSDetect failed with %Rrc"), vrc);
1189 }
1190 return hrc;
1191}
1192
1193HRESULT MachineDebugger::queryOSKernelLog(ULONG aMaxMessages, com::Utf8Str &aDmesg)
1194{
1195 /*
1196 * Lock the debugger and get the VM pointer
1197 */
1198 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1199 Console::SafeVMPtr ptrVM(mParent);
1200 HRESULT hrc = ptrVM.rc();
1201 if (SUCCEEDED(hrc))
1202 {
1203 PDBGFOSIDMESG pDmesg = (PDBGFOSIDMESG)DBGFR3OSQueryInterface(ptrVM.rawUVM(), DBGFOSINTERFACE_DMESG);
1204 if (pDmesg)
1205 {
1206 size_t cbActual;
1207 size_t cbBuf = _512K;
1208 int vrc = aDmesg.reserveNoThrow(cbBuf);
1209 if (RT_SUCCESS(vrc))
1210 {
1211 uint32_t cMessages = aMaxMessages == 0 ? UINT32_MAX : aMaxMessages;
1212 vrc = pDmesg->pfnQueryKernelLog(pDmesg, ptrVM.rawUVM(), 0 /*fFlags*/, cMessages,
1213 aDmesg.mutableRaw(), cbBuf, &cbActual);
1214
1215 uint32_t cTries = 10;
1216 while (vrc == VERR_BUFFER_OVERFLOW && cbBuf < 16*_1M && cTries-- > 0)
1217 {
1218 cbBuf = RT_ALIGN_Z(cbActual + _4K, _4K);
1219 vrc = aDmesg.reserveNoThrow(cbBuf);
1220 if (RT_SUCCESS(vrc))
1221 vrc = pDmesg->pfnQueryKernelLog(pDmesg, ptrVM.rawUVM(), 0 /*fFlags*/, cMessages,
1222 aDmesg.mutableRaw(), cbBuf, &cbActual);
1223 }
1224 if (RT_SUCCESS(vrc))
1225 aDmesg.jolt();
1226 else if (vrc == VERR_BUFFER_OVERFLOW)
1227 hrc = setError(E_FAIL, "Too much log available, must use the maxMessages parameter to restrict.");
1228 else
1229 hrc = setErrorVrc(vrc);
1230 }
1231 else
1232 hrc = setErrorBoth(E_OUTOFMEMORY, vrc);
1233 }
1234 else
1235 hrc = setError(E_FAIL, "The dmesg interface isn't implemented by guest OS digger, or detectOS() has not been called.");
1236 }
1237 return hrc;
1238}
1239
1240/**
1241 * Formats a register value.
1242 *
1243 * This is used by both register getter methods.
1244 *
1245 * @returns
1246 * @param a_pbstr The output Bstr variable.
1247 * @param a_pValue The value to format.
1248 * @param a_enmType The type of the value.
1249 */
1250DECLINLINE(HRESULT) formatRegisterValue(Bstr *a_pbstr, PCDBGFREGVAL a_pValue, DBGFREGVALTYPE a_enmType)
1251{
1252 char szHex[160];
1253 ssize_t cch = DBGFR3RegFormatValue(szHex, sizeof(szHex), a_pValue, a_enmType, true /*fSpecial*/);
1254 if (RT_UNLIKELY(cch <= 0))
1255 return E_UNEXPECTED;
1256 *a_pbstr = szHex;
1257 return S_OK;
1258}
1259
1260HRESULT MachineDebugger::getRegister(ULONG aCpuId, const com::Utf8Str &aName, com::Utf8Str &aValue)
1261{
1262 /*
1263 * The prologue.
1264 */
1265 LogFlowThisFunc(("\n"));
1266 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1267 Console::SafeVMPtr ptrVM(mParent);
1268 HRESULT hrc = ptrVM.rc();
1269 if (SUCCEEDED(hrc))
1270 {
1271 /*
1272 * Real work.
1273 */
1274 DBGFREGVAL Value;
1275 DBGFREGVALTYPE enmType;
1276 int vrc = DBGFR3RegNmQuery(ptrVM.rawUVM(), aCpuId, aName.c_str(), &Value, &enmType);
1277 if (RT_SUCCESS(vrc))
1278 {
1279 try
1280 {
1281 Bstr bstrValue;
1282 hrc = formatRegisterValue(&bstrValue, &Value, enmType);
1283 if (SUCCEEDED(hrc))
1284 aValue = Utf8Str(bstrValue);
1285 }
1286 catch (std::bad_alloc)
1287 {
1288 hrc = E_OUTOFMEMORY;
1289 }
1290 }
1291 else if (vrc == VERR_DBGF_REGISTER_NOT_FOUND)
1292 hrc = setError(E_FAIL, tr("Register '%s' was not found"), aName.c_str());
1293 else if (vrc == VERR_INVALID_CPU_ID)
1294 hrc = setError(E_FAIL, tr("Invalid CPU ID: %u"), aCpuId);
1295 else
1296 hrc = setError(VBOX_E_VM_ERROR,
1297 tr("DBGFR3RegNmQuery failed with rc=%Rrc querying register '%s' with default cpu set to %u"),
1298 vrc, aName.c_str(), aCpuId);
1299 }
1300
1301 return hrc;
1302}
1303
1304HRESULT MachineDebugger::getRegisters(ULONG aCpuId, std::vector<com::Utf8Str> &aNames, std::vector<com::Utf8Str> &aValues)
1305{
1306 RT_NOREF(aCpuId); /** @todo fix missing aCpuId usage! */
1307
1308 /*
1309 * The prologue.
1310 */
1311 LogFlowThisFunc(("\n"));
1312 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1313 Console::SafeVMPtr ptrVM(mParent);
1314 HRESULT hrc = ptrVM.rc();
1315 if (SUCCEEDED(hrc))
1316 {
1317 /*
1318 * Real work.
1319 */
1320 size_t cRegs;
1321 int vrc = DBGFR3RegNmQueryAllCount(ptrVM.rawUVM(), &cRegs);
1322 if (RT_SUCCESS(vrc))
1323 {
1324 PDBGFREGENTRYNM paRegs = (PDBGFREGENTRYNM)RTMemAllocZ(sizeof(paRegs[0]) * cRegs);
1325 if (paRegs)
1326 {
1327 vrc = DBGFR3RegNmQueryAll(ptrVM.rawUVM(), paRegs, cRegs);
1328 if (RT_SUCCESS(vrc))
1329 {
1330 try
1331 {
1332 aValues.resize(cRegs);
1333 aNames.resize(cRegs);
1334 for (uint32_t iReg = 0; iReg < cRegs; iReg++)
1335 {
1336 char szHex[160];
1337 szHex[159] = szHex[0] = '\0';
1338 ssize_t cch = DBGFR3RegFormatValue(szHex, sizeof(szHex), &paRegs[iReg].Val,
1339 paRegs[iReg].enmType, true /*fSpecial*/);
1340 Assert(cch > 0); NOREF(cch);
1341 aNames[iReg] = Utf8Str(paRegs[iReg].pszName);
1342 aValues[iReg] = Utf8Str(szHex);
1343 }
1344 }
1345 catch (std::bad_alloc)
1346 {
1347 hrc = E_OUTOFMEMORY;
1348 }
1349 }
1350 else
1351 hrc = setError(E_FAIL, tr("DBGFR3RegNmQueryAll failed with %Rrc"), vrc);
1352
1353 RTMemFree(paRegs);
1354 }
1355 else
1356 hrc = E_OUTOFMEMORY;
1357 }
1358 else
1359 hrc = setError(E_FAIL, tr("DBGFR3RegNmQueryAllCount failed with %Rrc"), vrc);
1360 }
1361 return hrc;
1362}
1363
1364HRESULT MachineDebugger::setRegister(ULONG aCpuId, const com::Utf8Str &aName, const com::Utf8Str &aValue)
1365{
1366 RT_NOREF(aCpuId, aName, aValue);
1367 ReturnComNotImplemented();
1368}
1369
1370HRESULT MachineDebugger::setRegisters(ULONG aCpuId, const std::vector<com::Utf8Str> &aNames,
1371 const std::vector<com::Utf8Str> &aValues)
1372{
1373 RT_NOREF(aCpuId, aNames, aValues);
1374 ReturnComNotImplemented();
1375}
1376
1377HRESULT MachineDebugger::dumpGuestStack(ULONG aCpuId, com::Utf8Str &aStack)
1378{
1379 /*
1380 * The prologue.
1381 */
1382 LogFlowThisFunc(("\n"));
1383 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1384 Console::SafeVMPtr ptrVM(mParent);
1385 HRESULT hrc = ptrVM.rc();
1386 if (SUCCEEDED(hrc))
1387 {
1388 /*
1389 * There is currently a problem with the windows diggers and SMP, where
1390 * guest driver memory is being read from CPU zero in order to ensure that
1391 * we've got a consisten virtual memory view. If one of the other CPUs
1392 * initiates a rendezvous while we're unwinding the stack and trying to
1393 * read guest driver memory, we will deadlock.
1394 *
1395 * So, check the VM state and maybe suspend the VM before we continue.
1396 */
1397 int vrc = VINF_SUCCESS;
1398 bool fPaused = false;
1399 if (aCpuId != 0)
1400 {
1401 VMSTATE enmVmState = VMR3GetStateU(ptrVM.rawUVM());
1402 if ( enmVmState == VMSTATE_RUNNING
1403 || enmVmState == VMSTATE_RUNNING_LS
1404 || enmVmState == VMSTATE_RUNNING_FT)
1405 {
1406 alock.release();
1407 vrc = VMR3Suspend(ptrVM.rawUVM(), VMSUSPENDREASON_USER);
1408 alock.acquire();
1409 fPaused = RT_SUCCESS(vrc);
1410 }
1411 }
1412 if (RT_SUCCESS(vrc))
1413 {
1414 PCDBGFSTACKFRAME pFirstFrame;
1415 vrc = DBGFR3StackWalkBegin(ptrVM.rawUVM(), aCpuId, DBGFCODETYPE_GUEST, &pFirstFrame);
1416 if (RT_SUCCESS(vrc))
1417 {
1418 /*
1419 * Print header.
1420 */
1421 try
1422 {
1423 uint32_t fBitFlags = 0;
1424 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
1425 pFrame;
1426 pFrame = DBGFR3StackWalkNext(pFrame))
1427 {
1428 uint32_t const fCurBitFlags = pFrame->fFlags & (DBGFSTACKFRAME_FLAGS_16BIT | DBGFSTACKFRAME_FLAGS_32BIT | DBGFSTACKFRAME_FLAGS_64BIT);
1429 if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_16BIT)
1430 {
1431 if (fCurBitFlags != fBitFlags)
1432 aStack.append("SS:BP Ret SS:BP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
1433 aStack.append(Utf8StrFmt("%04RX16:%04RX16 %04RX16:%04RX16 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
1434 pFrame->AddrFrame.Sel,
1435 (uint16_t)pFrame->AddrFrame.off,
1436 pFrame->AddrReturnFrame.Sel,
1437 (uint16_t)pFrame->AddrReturnFrame.off,
1438 (uint32_t)pFrame->AddrReturnPC.Sel,
1439 (uint32_t)pFrame->AddrReturnPC.off,
1440 pFrame->Args.au32[0],
1441 pFrame->Args.au32[1],
1442 pFrame->Args.au32[2],
1443 pFrame->Args.au32[3]));
1444 }
1445 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT)
1446 {
1447 if (fCurBitFlags != fBitFlags)
1448 aStack.append("EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
1449 aStack.append(Utf8StrFmt("%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
1450 (uint32_t)pFrame->AddrFrame.off,
1451 (uint32_t)pFrame->AddrReturnFrame.off,
1452 (uint32_t)pFrame->AddrReturnPC.Sel,
1453 (uint32_t)pFrame->AddrReturnPC.off,
1454 pFrame->Args.au32[0],
1455 pFrame->Args.au32[1],
1456 pFrame->Args.au32[2],
1457 pFrame->Args.au32[3]));
1458 }
1459 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT)
1460 {
1461 if (fCurBitFlags != fBitFlags)
1462 aStack.append("RBP Ret SS:RBP Ret RIP CS:RIP / Symbol [line]\n");
1463 aStack.append(Utf8StrFmt("%016RX64 %04RX16:%016RX64 %016RX64",
1464 (uint64_t)pFrame->AddrFrame.off,
1465 pFrame->AddrReturnFrame.Sel,
1466 (uint64_t)pFrame->AddrReturnFrame.off,
1467 (uint64_t)pFrame->AddrReturnPC.off));
1468 }
1469
1470 if (!pFrame->pSymPC)
1471 aStack.append(Utf8StrFmt(fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT
1472 ? " %RTsel:%016RGv"
1473 : fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT
1474 ? " %RTsel:%08RGv"
1475 : " %RTsel:%04RGv"
1476 , pFrame->AddrPC.Sel, pFrame->AddrPC.off));
1477 else
1478 {
1479 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value; /** @todo this isn't 100% correct for segmented stuff. */
1480 if (offDisp > 0)
1481 aStack.append(Utf8StrFmt(" %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp));
1482 else if (offDisp < 0)
1483 aStack.append(Utf8StrFmt(" %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp));
1484 else
1485 aStack.append(Utf8StrFmt(" %s", pFrame->pSymPC->szName));
1486 }
1487 if (pFrame->pLinePC)
1488 aStack.append(Utf8StrFmt(" [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo));
1489 aStack.append(Utf8StrFmt("\n"));
1490
1491 fBitFlags = fCurBitFlags;
1492 }
1493 }
1494 catch (std::bad_alloc)
1495 {
1496 hrc = E_OUTOFMEMORY;
1497 }
1498
1499 DBGFR3StackWalkEnd(pFirstFrame);
1500 }
1501 else
1502 hrc = setError(E_FAIL, tr("DBGFR3StackWalkBegin failed with %Rrc"), vrc);
1503
1504 /*
1505 * Resume the VM if we suspended it.
1506 */
1507 if (fPaused)
1508 {
1509 alock.release();
1510 VMR3Resume(ptrVM.rawUVM(), VMRESUMEREASON_USER);
1511 }
1512 }
1513 else
1514 hrc = setError(E_FAIL, tr("Suspending the VM failed with %Rrc\n"), vrc);
1515 }
1516
1517 return hrc;
1518}
1519
1520/**
1521 * Resets VM statistics.
1522 *
1523 * @returns COM status code.
1524 * @param aPattern The selection pattern. A bit similar to filename globbing.
1525 */
1526HRESULT MachineDebugger::resetStats(const com::Utf8Str &aPattern)
1527{
1528 Console::SafeVMPtrQuiet ptrVM(mParent);
1529
1530 if (!ptrVM.isOk())
1531 return setError(VBOX_E_INVALID_VM_STATE, "Machine is not running");
1532
1533 STAMR3Reset(ptrVM.rawUVM(), aPattern.c_str());
1534
1535 return S_OK;
1536}
1537
1538/**
1539 * Dumps VM statistics to the log.
1540 *
1541 * @returns COM status code.
1542 * @param aPattern The selection pattern. A bit similar to filename globbing.
1543 */
1544HRESULT MachineDebugger::dumpStats(const com::Utf8Str &aPattern)
1545{
1546 Console::SafeVMPtrQuiet ptrVM(mParent);
1547
1548 if (!ptrVM.isOk())
1549 return setError(VBOX_E_INVALID_VM_STATE, "Machine is not running");
1550
1551 STAMR3Dump(ptrVM.rawUVM(), aPattern.c_str());
1552
1553 return S_OK;
1554}
1555
1556/**
1557 * Get the VM statistics in an XML format.
1558 *
1559 * @returns COM status code.
1560 * @param aPattern The selection pattern. A bit similar to filename globbing.
1561 * @param aWithDescriptions Whether to include the descriptions.
1562 * @param aStats The XML document containing the statistics.
1563 */
1564HRESULT MachineDebugger::getStats(const com::Utf8Str &aPattern, BOOL aWithDescriptions, com::Utf8Str &aStats)
1565{
1566 Console::SafeVMPtrQuiet ptrVM(mParent);
1567
1568 if (!ptrVM.isOk())
1569 return setError(VBOX_E_INVALID_VM_STATE, "Machine is not running");
1570
1571 char *pszSnapshot;
1572 int vrc = STAMR3Snapshot(ptrVM.rawUVM(), aPattern.c_str(), &pszSnapshot, NULL,
1573 !!aWithDescriptions);
1574 if (RT_FAILURE(vrc))
1575 return vrc == VERR_NO_MEMORY ? E_OUTOFMEMORY : E_FAIL;
1576
1577 /** @todo this is horribly inefficient! And it's kinda difficult to tell whether it failed...
1578 * Must use UTF-8 or ASCII here and completely avoid these two extra copy operations.
1579 * Until that's done, this method is kind of useless for debugger statistics GUI because
1580 * of the amount statistics in a debug build. */
1581 aStats = Utf8Str(pszSnapshot);
1582 STAMR3SnapshotFree(ptrVM.rawUVM(), pszSnapshot);
1583
1584 return S_OK;
1585}
1586
1587
1588// public methods only for internal purposes
1589/////////////////////////////////////////////////////////////////////////////
1590
1591void MachineDebugger::i_flushQueuedSettings()
1592{
1593 mFlushMode = true;
1594 if (mSingleStepQueued != -1)
1595 {
1596 COMSETTER(SingleStep)(mSingleStepQueued);
1597 mSingleStepQueued = -1;
1598 }
1599 for (unsigned i = 0; i < EMEXECPOLICY_END; i++)
1600 if (maiQueuedEmExecPolicyParams[i] != UINT8_MAX)
1601 {
1602 i_setEmExecPolicyProperty((EMEXECPOLICY)i, RT_BOOL(maiQueuedEmExecPolicyParams[i]));
1603 maiQueuedEmExecPolicyParams[i] = UINT8_MAX;
1604 }
1605 if (mPatmEnabledQueued != -1)
1606 {
1607 COMSETTER(PATMEnabled)(mPatmEnabledQueued);
1608 mPatmEnabledQueued = -1;
1609 }
1610 if (mCsamEnabledQueued != -1)
1611 {
1612 COMSETTER(CSAMEnabled)(mCsamEnabledQueued);
1613 mCsamEnabledQueued = -1;
1614 }
1615 if (mLogEnabledQueued != -1)
1616 {
1617 COMSETTER(LogEnabled)(mLogEnabledQueued);
1618 mLogEnabledQueued = -1;
1619 }
1620 if (mVirtualTimeRateQueued != UINT32_MAX)
1621 {
1622 COMSETTER(VirtualTimeRate)(mVirtualTimeRateQueued);
1623 mVirtualTimeRateQueued = UINT32_MAX;
1624 }
1625 mFlushMode = false;
1626}
1627
1628// private methods
1629/////////////////////////////////////////////////////////////////////////////
1630
1631bool MachineDebugger::i_queueSettings() const
1632{
1633 if (!mFlushMode)
1634 {
1635 // check if the machine is running
1636 MachineState_T machineState;
1637 mParent->COMGETTER(State)(&machineState);
1638 switch (machineState)
1639 {
1640 // queue the request
1641 default:
1642 return true;
1643
1644 case MachineState_Running:
1645 case MachineState_Paused:
1646 case MachineState_Stuck:
1647 case MachineState_LiveSnapshotting:
1648 case MachineState_Teleporting:
1649 break;
1650 }
1651 }
1652 return false;
1653}
1654/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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