VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxInternalManage.cpp@ 2491

Last change on this file since 2491 was 1475, checked in by vboxsync, 18 years ago

enums are 32-bit according to the C standard

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 KB
Line 
1/** @file
2 *
3 * VBox frontends: VBoxManage (command-line interface):
4 * VBoxInternalManage
5 *
6 * VBoxInternalManage used to be a second CLI for doing special tricks,
7 * not intended for general usage, only for assisting VBox developers.
8 * It is now integrated into VBoxManage.
9 */
10
11/*
12 * Copyright (C) 2006 InnoTek Systemberatung GmbH
13 *
14 * This file is part of VirtualBox Open Source Edition (OSE), as
15 * available from http://www.virtualbox.org. This file is free software;
16 * you can redistribute it and/or modify it under the terms of the GNU
17 * General Public License as published by the Free Software Foundation,
18 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
19 * distribution. VirtualBox OSE is distributed in the hope that it will
20 * be useful, but WITHOUT ANY WARRANTY of any kind.
21 *
22 * If you received this file as part of a commercial VirtualBox
23 * distribution, then only the terms of your commercial VirtualBox
24 * license agreement apply instead of the previous paragraph.
25 */
26
27
28
29/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#include <VBox/com/com.h>
33#include <VBox/com/string.h>
34#include <VBox/com/Guid.h>
35#include <VBox/com/ErrorInfo.h>
36
37#include <VBox/com/VirtualBox.h>
38
39#include <iprt/runtime.h>
40#include <iprt/stream.h>
41#include <iprt/string.h>
42#include <iprt/uuid.h>
43#include <VBox/err.h>
44
45#include <VBox/VBoxHDD.h>
46
47#include "VBoxManage.h"
48
49using namespace com;
50
51/** flag whether we're in internal mode */
52bool fInternalMode;
53
54/**
55 * Print the usage info.
56 */
57void printUsageInternal(USAGECATEGORY u64Cmd)
58{
59 RTPrintf("Usage: VBoxManage internalcommands <command> [command arguments]\n"
60 "\n"
61 "Commands:\n"
62 "\n"
63 "%s%s%s"
64 "WARNING: This is a development tool and shall only be used to analyse\n"
65 " problems. It is completely unsupported and will change in\n"
66 " incompatible ways without warning.\n",
67 (u64Cmd & USAGE_LOADSYMS) ?
68 " loadsyms <vmname>|<uuid> <symfile> [delta] [module] [module address]\n"
69 " This will instruct DBGF to load the given symbolfile\n"
70 " during initialization.\n"
71 "\n"
72 : "",
73 (u64Cmd & USAGE_UNLOADSYMS) ?
74 " unloadsyms <vmname>|<uuid> <symfile>\n"
75 " Removes <symfile> from the list of symbol files that\n"
76 " should be loaded during DBF initialization.\n"
77 "\n"
78 : "",
79 (u64Cmd & USAGE_SETVDIUUID) ?
80 " setvdiuuid <filepath>\n"
81 " Assigns a new UUID to the given VDI file. This way, multiple copies\n"
82 " of VDI containers can be registered.\n"
83 "\n"
84 : ""
85 );
86}
87
88/** @todo this is no longer necessary, we can enumerate extra data */
89/**
90 * Finds a new unique key name.
91 *
92 * I don't think this is 100% race condition proof, but we assumes
93 * the user is not trying to push this point.
94 *
95 * @returns Result from the insert.
96 * @param pMachine The Machine object.
97 * @param pszKeyBase The base key.
98 * @param rKey Reference to the string object in which we will return the key.
99 */
100static HRESULT NewUniqueKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, Utf8Str &rKey)
101{
102 Bstr Keys;
103 HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
104 if (FAILED(hrc))
105 return hrc;
106
107 /* if there are no keys, it's simple. */
108 if (Keys.isEmpty())
109 {
110 rKey = "1";
111 return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr("1"));
112 }
113
114 /* find a unique number - brute force rulez. */
115 Utf8Str KeysUtf8(Keys);
116 const char *pszKeys = RTStrStripL(KeysUtf8.raw());
117 for (unsigned i = 1; i < 1000000; i++)
118 {
119 char szKey[32];
120 size_t cchKey = RTStrPrintf(szKey, sizeof(szKey), "%#x", i);
121 const char *psz = strstr(pszKeys, szKey);
122 while (psz)
123 {
124 if ( ( psz == pszKeys
125 || psz[-1] == ' ')
126 && ( psz[cchKey] == ' '
127 || !psz[cchKey])
128 )
129 break;
130 psz = strstr(psz + cchKey, szKey);
131 }
132 if (!psz)
133 {
134 rKey = szKey;
135 Utf8StrFmt NewKeysUtf8("%s %s", pszKeys, szKey);
136 return pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(NewKeysUtf8));
137 }
138 }
139 RTPrintf("Error: Cannot find unique key for '%s'!\n", pszKeyBase);
140 return E_FAIL;
141}
142
143
144#if 0
145/**
146 * Remove a key.
147 *
148 * I don't think this isn't 100% race condition proof, but we assumes
149 * the user is not trying to push this point.
150 *
151 * @returns Result from the insert.
152 * @param pMachine The machine object.
153 * @param pszKeyBase The base key.
154 * @param pszKey The key to remove.
155 */
156static HRESULT RemoveKey(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey)
157{
158 Bstr Keys;
159 HRESULT hrc = pMachine->GetExtraData(Bstr(pszKeyBase), Keys.asOutParam());
160 if (FAILED(hrc))
161 return hrc;
162
163 /* if there are no keys, it's simple. */
164 if (Keys.isEmpty())
165 return S_OK;
166
167 char *pszKeys;
168 int rc = RTStrUcs2ToUtf8(&pszKeys, Keys.raw());
169 if (VBOX_SUCCESS(rc))
170 {
171 /* locate it */
172 size_t cchKey = strlen(pszKey);
173 char *psz = strstr(pszKeys, pszKey);
174 while (psz)
175 {
176 if ( ( psz == pszKeys
177 || psz[-1] == ' ')
178 && ( psz[cchKey] == ' '
179 || !psz[cchKey])
180 )
181 break;
182 psz = strstr(psz + cchKey, pszKey);
183 }
184 if (psz)
185 {
186 /* remove it */
187 char *pszNext = RTStrStripL(psz + cchKey);
188 if (*pszNext)
189 memmove(psz, pszNext, strlen(pszNext) + 1);
190 else
191 *psz = '\0';
192 psz = RTStrStrip(pszKeys);
193
194 /* update */
195 hrc = pMachine->SetExtraData(Bstr(pszKeyBase), Bstr(psz));
196 }
197
198 RTStrFree(pszKeys);
199 return hrc;
200 }
201 else
202 RTPrintf("error: failed to delete key '%s' from '%s', string conversion error %Vrc!\n",
203 pszKey, pszKeyBase, rc);
204
205 return E_FAIL;
206}
207#endif
208
209
210/**
211 * Sets a key value, does necessary error bitching.
212 *
213 * @returns COM status code.
214 * @param pMachine The Machine object.
215 * @param pszKeyBase The key base.
216 * @param pszKey The key.
217 * @param pszAttribute The attribute name.
218 * @param pszValue The string value.
219 */
220static HRESULT SetString(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, const char *pszValue)
221{
222 HRESULT hrc = pMachine->SetExtraData(Bstr(Utf8StrFmt("%s/%s/%s", pszKeyBase, pszKey, pszAttribute)), Bstr(pszValue));
223 if (FAILED(hrc))
224 RTPrintf("error: Failed to set '%s/%s/%s' to '%s'! hrc=%#x\n",
225 pszKeyBase, pszKey, pszAttribute, pszValue, hrc);
226 return hrc;
227}
228
229
230/**
231 * Sets a key value, does necessary error bitching.
232 *
233 * @returns COM status code.
234 * @param pMachine The Machine object.
235 * @param pszKeyBase The key base.
236 * @param pszKey The key.
237 * @param pszAttribute The attribute name.
238 * @param u64Value The value.
239 */
240static HRESULT SetUInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, uint64_t u64Value)
241{
242 char szValue[64];
243 RTStrPrintf(szValue, sizeof(szValue), "%#RX64", u64Value);
244 return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
245}
246
247
248/**
249 * Sets a key value, does necessary error bitching.
250 *
251 * @returns COM status code.
252 * @param pMachine The Machine object.
253 * @param pszKeyBase The key base.
254 * @param pszKey The key.
255 * @param pszAttribute The attribute name.
256 * @param i64Value The value.
257 */
258static HRESULT SetInt64(ComPtr<IMachine> pMachine, const char *pszKeyBase, const char *pszKey, const char *pszAttribute, int64_t i64Value)
259{
260 char szValue[64];
261 RTStrPrintf(szValue, sizeof(szValue), "%RI64", i64Value);
262 return SetString(pMachine, pszKeyBase, pszKey, pszAttribute, szValue);
263}
264
265
266/**
267 * Identical to the 'loadsyms' command.
268 */
269static int CmdLoadSyms(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
270{
271 HRESULT rc;
272
273 /*
274 * Get the VM
275 */
276 ComPtr<IMachine> machine;
277 /* assume it's a UUID */
278 rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
279 if (FAILED(rc) || !machine)
280 {
281 /* must be a name */
282 CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()), 1);
283 }
284
285 /*
286 * Parse the command.
287 */
288 const char *pszFilename;
289 int64_t offDelta = 0;
290 const char *pszModule = NULL;
291 uint64_t ModuleAddress = ~0;
292 uint64_t ModuleSize = 0;
293
294 /* filename */
295 if (argc < 2)
296 return errorArgument("Missing the filename argument!\n");
297 pszFilename = argv[1];
298
299 /* offDelta */
300 if (argc >= 3)
301 {
302 int rc = RTStrToInt64Ex(argv[2], NULL, 0, &offDelta);
303 if (VBOX_FAILURE(rc))
304 return errorArgument(argv[0], "Failed to read delta '%s', rc=%Vrc\n", argv[2], rc);
305 }
306
307 /* pszModule */
308 if (argc >= 4)
309 pszModule = argv[3];
310
311 /* ModuleAddress */
312 if (argc >= 5)
313 {
314 int rc = RTStrToUInt64Ex(argv[4], NULL, 0, &ModuleAddress);
315 if (VBOX_FAILURE(rc))
316 return errorArgument(argv[0], "Failed to read module address '%s', rc=%Vrc\n", argv[4], rc);
317 }
318
319 /* ModuleSize */
320 if (argc >= 6)
321 {
322 int rc = RTStrToUInt64Ex(argv[5], NULL, 0, &ModuleSize);
323 if (VBOX_FAILURE(rc))
324 return errorArgument(argv[0], "Failed to read module size '%s', rc=%Vrc\n", argv[5], rc);
325 }
326
327 /*
328 * Add extra data.
329 */
330 Utf8Str KeyStr;
331 HRESULT hrc = NewUniqueKey(machine, "VBoxInternal/DBGF/loadsyms", KeyStr);
332 if (SUCCEEDED(hrc))
333 hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Filename", pszFilename);
334 if (SUCCEEDED(hrc) && argc >= 3)
335 hrc = SetInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Delta", offDelta);
336 if (SUCCEEDED(hrc) && argc >= 4)
337 hrc = SetString(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "Module", pszModule);
338 if (SUCCEEDED(hrc) && argc >= 5)
339 hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleAddress", ModuleAddress);
340 if (SUCCEEDED(hrc) && argc >= 6)
341 hrc = SetUInt64(machine, "VBoxInternal/DBGF/loadsyms", KeyStr, "ModuleSize", ModuleSize);
342
343 return FAILED(hrc);
344}
345
346static int handleSetVDIUUID(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
347{
348 /* we need exactly one parameter: the vdi file */
349 if (argc != 1)
350 {
351 return errorSyntax(USAGE_SETVDIUUID, "Not enough parameters");
352 }
353
354 /* generate a new UUID */
355 Guid uuid;
356 uuid.create();
357
358 /* just try it */
359 int rc = VDISetImageUUIDs(argv[0], uuid.raw(), NULL, NULL, NULL);
360 if (VBOX_FAILURE(rc))
361 {
362 RTPrintf("Error while setting a new UUID: %Vrc (%d)\n", rc, rc);
363 }
364 else
365 {
366 RTPrintf("UUID changed to: %s\n", uuid.toString().raw());
367 }
368
369 return 0;
370}
371
372/**
373 * Wrapper for handling internal commands
374 */
375int handleInternalCommands(int argc, char *argv[],
376 ComPtr <IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
377{
378 fInternalMode = true;
379
380 /* at least a command is required */
381 if (argc < 1)
382 return errorSyntax(USAGE_ALL, "Command missing");
383
384 /*
385 * The 'string switch' on command name.
386 */
387 const char *pszCmd = argv[0];
388 if (!strcmp(pszCmd, "loadsyms"))
389 return CmdLoadSyms(argc - 1, &argv[1], aVirtualBox, aSession);
390 //if (!strcmp(pszCmd, "unloadsyms"))
391 // return CmdUnloadSyms(argc - 1 , &argv[1]);
392 if (!strcmp(pszCmd, "setvdiuuid"))
393 return handleSetVDIUUID(argc - 1, &argv[1], aVirtualBox, aSession);
394
395 /* default: */
396 return errorSyntax(USAGE_ALL, "Invalid command '%s'", Utf8Str(argv[0]).raw());
397}
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