1 | /* $Id: VBoxBugReport.cpp 98477 2023-02-03 22:37:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxBugReport - VirtualBox command-line diagnostics tool, main file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | #include <VBox/com/com.h>
|
---|
30 | #include <VBox/com/string.h>
|
---|
31 | #include <VBox/com/array.h>
|
---|
32 | #include <VBox/com/ErrorInfo.h>
|
---|
33 | #include <VBox/com/errorprint.h>
|
---|
34 | #include <VBox/com/VirtualBox.h>
|
---|
35 |
|
---|
36 | #include <VBox/version.h>
|
---|
37 |
|
---|
38 | #include <iprt/buildconfig.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/env.h>
|
---|
41 | #include <iprt/file.h>
|
---|
42 | #include <iprt/getopt.h>
|
---|
43 | #include <iprt/initterm.h>
|
---|
44 | #include <iprt/path.h>
|
---|
45 | #include <iprt/process.h>
|
---|
46 | #include <iprt/zip.h>
|
---|
47 | #include <iprt/cpp/exception.h>
|
---|
48 |
|
---|
49 | #include <list>
|
---|
50 |
|
---|
51 | #include "VBoxBugReport.h"
|
---|
52 |
|
---|
53 | /* Implementation - Base */
|
---|
54 |
|
---|
55 | #ifndef RT_OS_WINDOWS
|
---|
56 | /** @todo Replace with platform-specific implementations. */
|
---|
57 | void createBugReportOsSpecific(BugReport *pReport, const char *pszHome)
|
---|
58 | {
|
---|
59 | RT_NOREF(pReport, pszHome);
|
---|
60 | }
|
---|
61 | #endif /* !RT_OS_WINDOWS */
|
---|
62 |
|
---|
63 |
|
---|
64 | /* Globals */
|
---|
65 |
|
---|
66 | static char *g_pszVBoxManage = NULL;
|
---|
67 |
|
---|
68 | static const RTGETOPTDEF g_aOptions[] =
|
---|
69 | {
|
---|
70 | { "-all", 'A', RTGETOPT_REQ_NOTHING },
|
---|
71 | { "--all", 'A', RTGETOPT_REQ_NOTHING },
|
---|
72 | { "-output", 'o', RTGETOPT_REQ_STRING },
|
---|
73 | { "--output", 'o', RTGETOPT_REQ_STRING },
|
---|
74 | { "-text", 't', RTGETOPT_REQ_NOTHING },
|
---|
75 | { "--text", 't', RTGETOPT_REQ_NOTHING }
|
---|
76 | };
|
---|
77 |
|
---|
78 | static const char g_szUsage[] =
|
---|
79 | "Usage: %s [-h|-?|--help] [-A|--all|<vmname>...] [-o <file>|--output=<file>]\n"
|
---|
80 | " Several VM names can be specified at once to be included into single report.\n"
|
---|
81 | " If none is given then no machines will be included. Specifying -A overrides\n"
|
---|
82 | " any VM names provided and includes all registered machines.\n"
|
---|
83 | "Options:\n"
|
---|
84 | " -h, -help, --help Print usage information\n"
|
---|
85 | " -A, -all, --all Include all registered machines\n"
|
---|
86 | " -o, -output, --output Specifies the name of the output file\n"
|
---|
87 | " -t, -text, --text Produce a single text file instead of compressed TAR\n"
|
---|
88 | " -V, -version, --version Print version information\n"
|
---|
89 | "\n";
|
---|
90 |
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * This class stores machine-specific file paths that are obtained via
|
---|
94 | * VirtualBox API. In case API is not functioning properly these paths
|
---|
95 | * will be deduced on the best effort basis.
|
---|
96 | */
|
---|
97 | class MachineInfo
|
---|
98 | {
|
---|
99 | public:
|
---|
100 | MachineInfo(const char *name, const char *logFolder, const char *settingsFile);
|
---|
101 | ~MachineInfo();
|
---|
102 | const char *getName() const { return m_name; };
|
---|
103 | const char *getLogPath() const { return m_logpath; };
|
---|
104 | const char *getSettingsFile() const { return m_settings; };
|
---|
105 | private:
|
---|
106 | char *m_name;
|
---|
107 | char *m_logpath;
|
---|
108 | char *m_settings;
|
---|
109 | };
|
---|
110 |
|
---|
111 | MachineInfo::MachineInfo(const char *name, const char *logFolder, const char *settingsFile)
|
---|
112 | {
|
---|
113 | m_name = RTStrDup(name);
|
---|
114 | m_logpath = RTStrDup(logFolder);
|
---|
115 | m_settings = RTStrDup(settingsFile);
|
---|
116 | }
|
---|
117 |
|
---|
118 | MachineInfo::~MachineInfo()
|
---|
119 | {
|
---|
120 | RTStrFree(m_logpath);
|
---|
121 | RTStrFree(m_name);
|
---|
122 | RTStrFree(m_settings);
|
---|
123 | m_logpath = m_name = m_settings = 0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | typedef std::list<MachineInfo*> MachineInfoList;
|
---|
127 |
|
---|
128 |
|
---|
129 | class VBRDir
|
---|
130 | {
|
---|
131 | public:
|
---|
132 | VBRDir(const char *pcszPath) : m_hDir(NIL_RTDIR)
|
---|
133 | {
|
---|
134 | int rc = RTDirOpenFiltered(&m_hDir, pcszPath, RTDIRFILTER_WINNT, 0);
|
---|
135 | if (RT_FAILURE(rc) && rc != VERR_FILE_NOT_FOUND && rc != VERR_PATH_NOT_FOUND)
|
---|
136 | throw RTCError(com::Utf8StrFmt("Failed to open directory '%s'\n", pcszPath));
|
---|
137 | };
|
---|
138 | ~VBRDir()
|
---|
139 | {
|
---|
140 | if (RT_VALID_PTR(m_hDir))
|
---|
141 | {
|
---|
142 | int rc = RTDirClose(m_hDir);
|
---|
143 | AssertRC(rc);
|
---|
144 | }
|
---|
145 | };
|
---|
146 | const char *next(void)
|
---|
147 | {
|
---|
148 | if (!RT_VALID_PTR(m_hDir))
|
---|
149 | return NULL;
|
---|
150 |
|
---|
151 | int rc = RTDirRead(m_hDir, &m_DirEntry, NULL);
|
---|
152 | if (RT_SUCCESS(rc))
|
---|
153 | return m_DirEntry.szName;
|
---|
154 | if (rc == VERR_NO_MORE_FILES)
|
---|
155 | return NULL;
|
---|
156 | throw RTCError("Failed to read directory element\n");
|
---|
157 | };
|
---|
158 |
|
---|
159 | private:
|
---|
160 | RTDIR m_hDir;
|
---|
161 | RTDIRENTRY m_DirEntry;
|
---|
162 | };
|
---|
163 |
|
---|
164 |
|
---|
165 | BugReportFilter::BugReportFilter() : m_pvBuffer(0), m_cbBuffer(0)
|
---|
166 | {
|
---|
167 | }
|
---|
168 |
|
---|
169 | BugReportFilter::~BugReportFilter()
|
---|
170 | {
|
---|
171 | if (m_pvBuffer)
|
---|
172 | RTMemFree(m_pvBuffer);
|
---|
173 | }
|
---|
174 |
|
---|
175 | void *BugReportFilter::allocateBuffer(size_t cbNeeded)
|
---|
176 | {
|
---|
177 | if (m_pvBuffer)
|
---|
178 | {
|
---|
179 | if (cbNeeded > m_cbBuffer)
|
---|
180 | RTMemFree(m_pvBuffer);
|
---|
181 | else
|
---|
182 | return m_pvBuffer;
|
---|
183 | }
|
---|
184 | m_pvBuffer = RTMemAlloc(cbNeeded);
|
---|
185 | if (!m_pvBuffer)
|
---|
186 | throw RTCError(com::Utf8StrFmt("Failed to allocate %ld bytes\n", cbNeeded));
|
---|
187 | m_cbBuffer = cbNeeded;
|
---|
188 | return m_pvBuffer;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * An abstract class serving as the root of the bug report item tree.
|
---|
194 | */
|
---|
195 | BugReportItem::BugReportItem(const char *pszTitle)
|
---|
196 | {
|
---|
197 | m_pszTitle = RTStrDup(pszTitle);
|
---|
198 | m_filter = 0;
|
---|
199 | }
|
---|
200 |
|
---|
201 | BugReportItem::~BugReportItem()
|
---|
202 | {
|
---|
203 | if (m_filter)
|
---|
204 | delete m_filter;
|
---|
205 | RTStrFree(m_pszTitle);
|
---|
206 | }
|
---|
207 |
|
---|
208 | void BugReportItem::addFilter(BugReportFilter *filter)
|
---|
209 | {
|
---|
210 | m_filter = filter;
|
---|
211 | }
|
---|
212 |
|
---|
213 | void *BugReportItem::applyFilter(void *pvSource, size_t *pcbInOut)
|
---|
214 | {
|
---|
215 | if (m_filter)
|
---|
216 | return m_filter->apply(pvSource, pcbInOut);
|
---|
217 | return pvSource;
|
---|
218 | }
|
---|
219 |
|
---|
220 | const char * BugReportItem::getTitle(void)
|
---|
221 | {
|
---|
222 | return m_pszTitle;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | BugReport::BugReport(const char *pszFileName)
|
---|
227 | {
|
---|
228 | m_pszFileName = RTStrDup(pszFileName);
|
---|
229 | }
|
---|
230 |
|
---|
231 | BugReport::~BugReport()
|
---|
232 | {
|
---|
233 | for (unsigned i = 0; i < m_Items.size(); ++i)
|
---|
234 | {
|
---|
235 | delete m_Items[i];
|
---|
236 | }
|
---|
237 | RTStrFree(m_pszFileName);
|
---|
238 | }
|
---|
239 |
|
---|
240 | int BugReport::getItemCount(void)
|
---|
241 | {
|
---|
242 | return (int)m_Items.size();
|
---|
243 | }
|
---|
244 |
|
---|
245 | void BugReport::addItem(BugReportItem* item, BugReportFilter *filter)
|
---|
246 | {
|
---|
247 | if (filter)
|
---|
248 | item->addFilter(filter);
|
---|
249 | if (item)
|
---|
250 | m_Items.append(item);
|
---|
251 | }
|
---|
252 |
|
---|
253 | void BugReport::process(void)
|
---|
254 | {
|
---|
255 | for (unsigned i = 0; i < m_Items.size(); ++i)
|
---|
256 | {
|
---|
257 | BugReportItem *pItem = m_Items[i];
|
---|
258 | RTPrintf("%3u%% - collecting %s...\n", i * 100 / m_Items.size(), pItem->getTitle());
|
---|
259 | processItem(pItem);
|
---|
260 | }
|
---|
261 | RTPrintf("100%% - compressing...\n\n");
|
---|
262 | }
|
---|
263 |
|
---|
264 | void *BugReport::applyFilters(BugReportItem* item, void *pvSource, size_t *pcbInOut)
|
---|
265 | {
|
---|
266 | return item->applyFilter(pvSource, pcbInOut);
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | BugReportStream::BugReportStream(const char *pszTitle) : BugReportItem(pszTitle)
|
---|
271 | {
|
---|
272 | m_hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
273 | handleRtError(RTPathTemp(m_szFileName, RTPATH_MAX),
|
---|
274 | "Failed to obtain path to temporary folder");
|
---|
275 | handleRtError(RTPathAppend(m_szFileName, RTPATH_MAX, "BugRepXXXXX.tmp"),
|
---|
276 | "Failed to append path");
|
---|
277 | handleRtError(RTFileCreateTemp(m_szFileName, 0600),
|
---|
278 | "Failed to create temporary file '%s'", m_szFileName);
|
---|
279 | handleRtError(RTVfsIoStrmOpenNormal(m_szFileName, RTFILE_O_OPEN | RTFILE_O_WRITE | RTFILE_O_DENY_NONE, &m_hVfsIos),
|
---|
280 | "Failed to open '%s'", m_szFileName);
|
---|
281 | }
|
---|
282 |
|
---|
283 | BugReportStream::~BugReportStream()
|
---|
284 | {
|
---|
285 | if (m_hVfsIos != NIL_RTVFSIOSTREAM)
|
---|
286 | RTVfsIoStrmRelease(m_hVfsIos);
|
---|
287 | RTFileDelete(m_szFileName);
|
---|
288 | }
|
---|
289 |
|
---|
290 | int BugReportStream::printf(const char *pszFmt, ...)
|
---|
291 | {
|
---|
292 | va_list va;
|
---|
293 | va_start(va, pszFmt);
|
---|
294 | int cb = RTVfsIoStrmPrintfV(m_hVfsIos, pszFmt, va);
|
---|
295 | va_end(va);
|
---|
296 | return cb;
|
---|
297 | }
|
---|
298 |
|
---|
299 | int BugReportStream::putStr(const char *pszString)
|
---|
300 | {
|
---|
301 | return RTVfsIoStrmPrintf(m_hVfsIos, "%s", pszString);
|
---|
302 | }
|
---|
303 |
|
---|
304 | RTVFSIOSTREAM BugReportStream::getStream(void)
|
---|
305 | {
|
---|
306 | RTVfsIoStrmRelease(m_hVfsIos);
|
---|
307 | handleRtError(RTVfsIoStrmOpenNormal(m_szFileName, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &m_hVfsIos),
|
---|
308 | "Failed to open '%s'", m_szFileName);
|
---|
309 | return m_hVfsIos;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | /* Implementation - Generic */
|
---|
314 |
|
---|
315 | BugReportFile::BugReportFile(const char *pszPath, const char *pszShortName) : BugReportItem(pszShortName)
|
---|
316 | {
|
---|
317 | m_hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
318 | m_pszPath = RTStrDup(pszPath);
|
---|
319 | }
|
---|
320 |
|
---|
321 | BugReportFile::~BugReportFile()
|
---|
322 | {
|
---|
323 | if (m_hVfsIos != NIL_RTVFSIOSTREAM)
|
---|
324 | RTVfsIoStrmRelease(m_hVfsIos);
|
---|
325 | if (m_pszPath)
|
---|
326 | RTStrFree(m_pszPath);
|
---|
327 | }
|
---|
328 |
|
---|
329 | RTVFSIOSTREAM BugReportFile::getStream(void)
|
---|
330 | {
|
---|
331 | handleRtError(RTVfsIoStrmOpenNormal(m_pszPath, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &m_hVfsIos),
|
---|
332 | "Failed to open '%s'", m_pszPath);
|
---|
333 | return m_hVfsIos;
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 | BugReportCommand::BugReportCommand(const char *pszTitle, const char *pszExec, ...)
|
---|
338 | : BugReportItem(pszTitle), m_hVfsIos(NIL_RTVFSIOSTREAM)
|
---|
339 | {
|
---|
340 | unsigned cArgs = 0;
|
---|
341 | m_papszArgs[cArgs++] = RTStrDup(pszExec);
|
---|
342 |
|
---|
343 | const char *pszArg;
|
---|
344 | va_list va;
|
---|
345 | va_start(va, pszExec);
|
---|
346 | do
|
---|
347 | {
|
---|
348 | if (cArgs >= RT_ELEMENTS(m_papszArgs))
|
---|
349 | {
|
---|
350 | va_end(va);
|
---|
351 | throw RTCError(com::Utf8StrFmt("Too many arguments (%u > %u)\n", cArgs+1, RT_ELEMENTS(m_papszArgs)));
|
---|
352 | }
|
---|
353 | pszArg = va_arg(va, const char *);
|
---|
354 | m_papszArgs[cArgs++] = pszArg ? RTStrDup(pszArg) : NULL;
|
---|
355 | } while (pszArg);
|
---|
356 | va_end(va);
|
---|
357 | }
|
---|
358 |
|
---|
359 | BugReportCommand::~BugReportCommand()
|
---|
360 | {
|
---|
361 | if (m_hVfsIos != NIL_RTVFSIOSTREAM)
|
---|
362 | RTVfsIoStrmRelease(m_hVfsIos);
|
---|
363 | RTFileDelete(m_szFileName);
|
---|
364 | for (size_t i = 0; i < RT_ELEMENTS(m_papszArgs) && m_papszArgs[i]; ++i)
|
---|
365 | RTStrFree(m_papszArgs[i]);
|
---|
366 | }
|
---|
367 |
|
---|
368 | RTVFSIOSTREAM BugReportCommand::getStream(void)
|
---|
369 | {
|
---|
370 | handleRtError(RTPathTemp(m_szFileName, RTPATH_MAX),
|
---|
371 | "Failed to obtain path to temporary folder");
|
---|
372 | handleRtError(RTPathAppend(m_szFileName, RTPATH_MAX, "BugRepXXXXX.tmp"),
|
---|
373 | "Failed to append path");
|
---|
374 | handleRtError(RTFileCreateTemp(m_szFileName, 0600),
|
---|
375 | "Failed to create temporary file '%s'", m_szFileName);
|
---|
376 |
|
---|
377 | RTHANDLE hStdOutErr;
|
---|
378 | hStdOutErr.enmType = RTHANDLETYPE_FILE;
|
---|
379 | handleRtError(RTFileOpen(&hStdOutErr.u.hFile, m_szFileName,
|
---|
380 | RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE),
|
---|
381 | "Failed to open temporary file '%s'", m_szFileName);
|
---|
382 |
|
---|
383 | RTPROCESS hProcess;
|
---|
384 | handleRtError(RTProcCreateEx(m_papszArgs[0], m_papszArgs, RTENV_DEFAULT, 0,
|
---|
385 | NULL, &hStdOutErr, &hStdOutErr,
|
---|
386 | NULL, NULL, NULL, &hProcess),
|
---|
387 | "Failed to create process '%s'", m_papszArgs[0]);
|
---|
388 | RTPROCSTATUS status;
|
---|
389 | handleRtError(RTProcWait(hProcess, RTPROCWAIT_FLAGS_BLOCK, &status),
|
---|
390 | "Process wait failed");
|
---|
391 | //if (status.enmReason == RTPROCEXITREASON_NORMAL) {}
|
---|
392 | RTFileClose(hStdOutErr.u.hFile);
|
---|
393 |
|
---|
394 | handleRtError(RTVfsIoStrmOpenNormal(m_szFileName, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &m_hVfsIos),
|
---|
395 | "Failed to open '%s'", m_szFileName);
|
---|
396 | return m_hVfsIos;
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | BugReportCommandTemp::BugReportCommandTemp(const char *pszTitle, const char *pszExec, ...)
|
---|
401 | : BugReportItem(pszTitle), m_hVfsIos(NIL_RTVFSIOSTREAM)
|
---|
402 | {
|
---|
403 | handleRtError(RTPathTemp(m_szFileName, RTPATH_MAX),
|
---|
404 | "Failed to obtain path to temporary folder");
|
---|
405 | handleRtError(RTPathAppend(m_szFileName, RTPATH_MAX, "BugRepXXXXX.tmp"),
|
---|
406 | "Failed to append path");
|
---|
407 | handleRtError(RTFileCreateTemp(m_szFileName, 0600),
|
---|
408 | "Failed to create temporary file '%s'", m_szFileName);
|
---|
409 |
|
---|
410 | unsigned cArgs = 0;
|
---|
411 | m_papszArgs[cArgs++] = RTStrDup(pszExec);
|
---|
412 |
|
---|
413 | const char *pszArg;
|
---|
414 | va_list va;
|
---|
415 | va_start(va, pszExec);
|
---|
416 | do
|
---|
417 | {
|
---|
418 | if (cArgs >= RT_ELEMENTS(m_papszArgs) - 1)
|
---|
419 | {
|
---|
420 | va_end(va);
|
---|
421 | throw RTCError(com::Utf8StrFmt("Too many arguments (%u > %u)\n", cArgs+1, RT_ELEMENTS(m_papszArgs)));
|
---|
422 | }
|
---|
423 | pszArg = va_arg(va, const char *);
|
---|
424 | m_papszArgs[cArgs++] = RTStrDup(pszArg ? pszArg : m_szFileName);
|
---|
425 | } while (pszArg);
|
---|
426 | va_end(va);
|
---|
427 |
|
---|
428 | m_papszArgs[cArgs++] = NULL;
|
---|
429 | }
|
---|
430 |
|
---|
431 | BugReportCommandTemp::~BugReportCommandTemp()
|
---|
432 | {
|
---|
433 | if (m_hVfsIos != NIL_RTVFSIOSTREAM)
|
---|
434 | RTVfsIoStrmRelease(m_hVfsIos);
|
---|
435 | RTFileDelete(m_szErrFileName);
|
---|
436 | RTFileDelete(m_szFileName);
|
---|
437 | for (size_t i = 0; i < RT_ELEMENTS(m_papszArgs) && m_papszArgs[i]; ++i)
|
---|
438 | RTStrFree(m_papszArgs[i]);
|
---|
439 | }
|
---|
440 |
|
---|
441 | RTVFSIOSTREAM BugReportCommandTemp::getStream(void)
|
---|
442 | {
|
---|
443 | handleRtError(RTPathTemp(m_szErrFileName, RTPATH_MAX),
|
---|
444 | "Failed to obtain path to temporary folder");
|
---|
445 | handleRtError(RTPathAppend(m_szErrFileName, RTPATH_MAX, "BugRepErrXXXXX.tmp"),
|
---|
446 | "Failed to append path");
|
---|
447 | handleRtError(RTFileCreateTemp(m_szErrFileName, 0600),
|
---|
448 | "Failed to create temporary file '%s'", m_szErrFileName);
|
---|
449 |
|
---|
450 | RTHANDLE hStdOutErr;
|
---|
451 | hStdOutErr.enmType = RTHANDLETYPE_FILE;
|
---|
452 | handleRtError(RTFileOpen(&hStdOutErr.u.hFile, m_szErrFileName,
|
---|
453 | RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE),
|
---|
454 | "Failed to open temporary file '%s'", m_szErrFileName);
|
---|
455 |
|
---|
456 | /* Remove the output file to prevent errors or confirmation prompts */
|
---|
457 | handleRtError(RTFileDelete(m_szFileName),
|
---|
458 | "Failed to delete temporary file '%s'", m_szFileName);
|
---|
459 |
|
---|
460 | RTPROCESS hProcess;
|
---|
461 | handleRtError(RTProcCreateEx(m_papszArgs[0], m_papszArgs, RTENV_DEFAULT, 0,
|
---|
462 | NULL, &hStdOutErr, &hStdOutErr,
|
---|
463 | NULL, NULL, NULL, &hProcess),
|
---|
464 | "Failed to create process '%s'", m_papszArgs[0]);
|
---|
465 | RTPROCSTATUS status;
|
---|
466 | handleRtError(RTProcWait(hProcess, RTPROCWAIT_FLAGS_BLOCK, &status),
|
---|
467 | "Process wait failed");
|
---|
468 | RTFileClose(hStdOutErr.u.hFile);
|
---|
469 |
|
---|
470 | if (status.enmReason == RTPROCEXITREASON_NORMAL && status.iStatus == 0)
|
---|
471 | handleRtError(RTVfsIoStrmOpenNormal(m_szFileName, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &m_hVfsIos),
|
---|
472 | "Failed to open '%s'", m_szFileName);
|
---|
473 | else
|
---|
474 | handleRtError(RTVfsIoStrmOpenNormal(m_szErrFileName, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &m_hVfsIos),
|
---|
475 | "Failed to open '%s'", m_szErrFileName);
|
---|
476 | return m_hVfsIos;
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 | BugReportText::BugReportText(const char *pszFileName) : BugReport(pszFileName)
|
---|
481 | {
|
---|
482 | handleRtError(RTStrmOpen(pszFileName, "w", &m_StrmTxt),
|
---|
483 | "Failed to open '%s'", pszFileName);
|
---|
484 | }
|
---|
485 |
|
---|
486 | BugReportText::~BugReportText()
|
---|
487 | {
|
---|
488 | if (m_StrmTxt)
|
---|
489 | RTStrmClose(m_StrmTxt);
|
---|
490 | }
|
---|
491 |
|
---|
492 | void BugReportText::processItem(BugReportItem* item)
|
---|
493 | {
|
---|
494 | int cb = RTStrmPrintf(m_StrmTxt, "[ %s ] -------------------------------------------\n", item->getTitle());
|
---|
495 | if (!cb)
|
---|
496 | throw RTCError(com::Utf8StrFmt("Write failure (cb=%d)\n", cb));
|
---|
497 |
|
---|
498 | RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
499 | try
|
---|
500 | {
|
---|
501 | hVfsIos = item->getStream();
|
---|
502 | }
|
---|
503 | catch (RTCError &e)
|
---|
504 | {
|
---|
505 | hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
506 | RTStrmPutStr(m_StrmTxt, e.what());
|
---|
507 | }
|
---|
508 |
|
---|
509 | int rc = VINF_SUCCESS;
|
---|
510 |
|
---|
511 | if (hVfsIos != NIL_RTVFSIOSTREAM)
|
---|
512 | {
|
---|
513 | char buf[64*1024];
|
---|
514 | size_t cbRead, cbWritten;
|
---|
515 | cbRead = cbWritten = 0;
|
---|
516 | while (RT_SUCCESS(rc = RTVfsIoStrmRead(hVfsIos, buf, sizeof(buf), true /*fBlocking*/, &cbRead)) && cbRead)
|
---|
517 | {
|
---|
518 | rc = RTStrmWriteEx(m_StrmTxt, applyFilters(item, buf, &cbRead), cbRead, &cbWritten);
|
---|
519 | if (RT_FAILURE(rc) || cbRead != cbWritten)
|
---|
520 | throw RTCError(com::Utf8StrFmt("Write failure (rc=%d, cbRead=%lu, cbWritten=%lu)\n",
|
---|
521 | rc, cbRead, cbWritten));
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | handleRtError(RTStrmPutCh(m_StrmTxt, '\n'), "Write failure");
|
---|
526 | }
|
---|
527 |
|
---|
528 |
|
---|
529 | BugReportTarGzip::BugReportTarGzip(const char *pszFileName)
|
---|
530 | : BugReport(pszFileName), m_hTarFss(NIL_RTVFSFSSTREAM)
|
---|
531 | {
|
---|
532 | VfsIoStreamHandle hVfsOut;
|
---|
533 | handleRtError(RTVfsIoStrmOpenNormal(pszFileName, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE,
|
---|
534 | hVfsOut.getPtr()),
|
---|
535 | "Failed to create output file '%s'", pszFileName);
|
---|
536 | handleRtError(RTZipGzipCompressIoStream(hVfsOut.get(), 0, 6, m_hVfsGzip.getPtr()),
|
---|
537 | "Failed to create compressed stream for '%s'", pszFileName);
|
---|
538 |
|
---|
539 | int rc = RTZipTarFsStreamToIoStream(m_hVfsGzip.get(), RTZIPTARFORMAT_DEFAULT, RTZIPTAR_C_SPARSE, &m_hTarFss);
|
---|
540 | handleRtError(rc, "Failed to create TAR file '%s'", m_szTarName);
|
---|
541 | }
|
---|
542 |
|
---|
543 | BugReportTarGzip::~BugReportTarGzip()
|
---|
544 | {
|
---|
545 | if (m_hTarFss != NIL_RTVFSFSSTREAM)
|
---|
546 | RTVfsFsStrmRelease(m_hTarFss);
|
---|
547 | }
|
---|
548 |
|
---|
549 | void BugReportTarGzip::dumpExceptionToArchive(RTCString &strTarFile, RTCError &e)
|
---|
550 | {
|
---|
551 | RTVFSFILE hVfsFile;
|
---|
552 | int rc = RTVfsMemFileCreate(NIL_RTVFSIOSTREAM, _1K/*cbEstimate*/, &hVfsFile);
|
---|
553 | if (RT_SUCCESS(rc))
|
---|
554 | {
|
---|
555 | rc = RTVfsFileWrite(hVfsFile, e.what(), RTStrNLen(e.what(), 1024), NULL /*pcbWritten*/);
|
---|
556 | if (RT_SUCCESS(rc))
|
---|
557 | {
|
---|
558 | rc = RTVfsFileSeek(hVfsFile, 0 /*offSeek*/, RTFILE_SEEK_BEGIN, NULL /*poffActual*/);
|
---|
559 | if (RT_SUCCESS(rc))
|
---|
560 | {
|
---|
561 | RTVFSOBJ hVfsObj = RTVfsObjFromFile(hVfsFile);
|
---|
562 | rc = RTVfsFsStrmAdd(m_hTarFss, strTarFile.c_str(), hVfsObj, 0 /*fFlags*/);
|
---|
563 | RTVfsObjRelease(hVfsObj);
|
---|
564 | }
|
---|
565 | }
|
---|
566 | RTVfsFileRelease(hVfsFile);
|
---|
567 | }
|
---|
568 | handleRtError(rc, "Failed to add exception text to TAR archive '%s'", m_szTarName);
|
---|
569 | }
|
---|
570 |
|
---|
571 | void BugReportTarGzip::processItem(BugReportItem* item)
|
---|
572 | {
|
---|
573 | /*
|
---|
574 | * @todo Our TAR implementation does not support names larger than 100 characters.
|
---|
575 | * We truncate the title to make sure it will fit into 100-character field of TAR header.
|
---|
576 | */
|
---|
577 | RTCString strTarFile = RTCString(item->getTitle()).substr(0, RTStrNLen(item->getTitle(), 99));
|
---|
578 | RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
579 | try
|
---|
580 | {
|
---|
581 | hVfsIos = item->getStream();
|
---|
582 | }
|
---|
583 | catch (RTCError &e)
|
---|
584 | {
|
---|
585 | hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
586 | dumpExceptionToArchive(strTarFile, e);
|
---|
587 | }
|
---|
588 |
|
---|
589 | if (hVfsIos != NIL_RTVFSIOSTREAM)
|
---|
590 | {
|
---|
591 | RTVFSOBJ hVfsObjIos = RTVfsObjFromIoStream(hVfsIos);
|
---|
592 | int rc = RTVfsFsStrmAdd(m_hTarFss, strTarFile.c_str(), hVfsObjIos, 0 /*fFlags*/);
|
---|
593 | RTVfsObjRelease(hVfsObjIos);
|
---|
594 | handleRtError(rc, "Failed to add file to TAR archive '%s'", m_szTarName);
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | void BugReportTarGzip::complete(void)
|
---|
599 | {
|
---|
600 | if (m_hTarFss != NIL_RTVFSFSSTREAM)
|
---|
601 | {
|
---|
602 | RTVfsFsStrmRelease(m_hTarFss);
|
---|
603 | m_hTarFss = NIL_RTVFSFSSTREAM;
|
---|
604 | }
|
---|
605 | handleRtError(RTVfsIoStrmFlush(m_hVfsGzip.get()), "Failed to flush output stream");
|
---|
606 | m_hVfsGzip.release();
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | /* Implementation - Main */
|
---|
611 |
|
---|
612 | void createBugReport(BugReport* report, const char *pszHome, MachineInfoList& machines)
|
---|
613 | {
|
---|
614 | /* Collect all log files from VBoxSVC */
|
---|
615 | VBRDir HomeDir(PathJoin(pszHome, "VBoxSVC.log*"));
|
---|
616 | const char *pcszSvcLogFile = HomeDir.next();
|
---|
617 | while (pcszSvcLogFile)
|
---|
618 | {
|
---|
619 | report->addItem(new BugReportFile(PathJoin(pszHome, pcszSvcLogFile), pcszSvcLogFile));
|
---|
620 | pcszSvcLogFile = HomeDir.next();
|
---|
621 | }
|
---|
622 |
|
---|
623 | report->addItem(new BugReportFile(PathJoin(pszHome, "VirtualBox.xml"), "VirtualBox.xml"));
|
---|
624 | report->addItem(new BugReportCommand("HostUsbDevices", g_pszVBoxManage, "list", "usbhost", NULL));
|
---|
625 | report->addItem(new BugReportCommand("HostUsbFilters", g_pszVBoxManage, "list", "usbfilters", NULL));
|
---|
626 |
|
---|
627 | for (MachineInfoList::iterator it = machines.begin(); it != machines.end(); ++it)
|
---|
628 | {
|
---|
629 | static const char * const s_apszLogFilePatterns[] = { "VBox.log*", "VBoxHardening.log" };
|
---|
630 | for (size_t iPat = 0; iPat < RT_ELEMENTS(s_apszLogFilePatterns); iPat++)
|
---|
631 | {
|
---|
632 | VBRDir VmLogFiles(PathJoin((*it)->getLogPath(), s_apszLogFilePatterns[iPat]));
|
---|
633 | const char *pcszVmLogFile = VmLogFiles.next();
|
---|
634 | while (pcszVmLogFile)
|
---|
635 | {
|
---|
636 | report->addItem(new BugReportFile(PathJoin((*it)->getLogPath(), pcszVmLogFile),
|
---|
637 | PathJoin((*it)->getName(), pcszVmLogFile)));
|
---|
638 | pcszVmLogFile = VmLogFiles.next();
|
---|
639 | }
|
---|
640 | }
|
---|
641 | report->addItem(new BugReportFile((*it)->getSettingsFile(),
|
---|
642 | PathJoin((*it)->getName(), RTPathFilename((*it)->getSettingsFile()))));
|
---|
643 | report->addItem(new BugReportCommand(PathJoin((*it)->getName(), "GuestProperties"),
|
---|
644 | g_pszVBoxManage, "guestproperty", "enumerate",
|
---|
645 | (*it)->getName(), NULL));
|
---|
646 | }
|
---|
647 |
|
---|
648 | createBugReportOsSpecific(report, pszHome);
|
---|
649 | }
|
---|
650 |
|
---|
651 | void addMachine(MachineInfoList& list, ComPtr<IMachine> machine)
|
---|
652 | {
|
---|
653 | BOOL fAccessible = FALSE;
|
---|
654 | HRESULT hrc = machine->COMGETTER(Accessible)(&fAccessible);
|
---|
655 | if (SUCCEEDED(hrc) && !fAccessible)
|
---|
656 | return
|
---|
657 | handleComError(hrc, "Failed to get accessible status of VM");
|
---|
658 |
|
---|
659 | com::Bstr name, logFolder, settingsFile;
|
---|
660 | handleComError(machine->COMGETTER(Name)(name.asOutParam()),
|
---|
661 | "Failed to get VM name");
|
---|
662 | handleComError(machine->COMGETTER(LogFolder)(logFolder.asOutParam()),
|
---|
663 | "Failed to get VM log folder");
|
---|
664 | handleComError(machine->COMGETTER(SettingsFilePath)(settingsFile.asOutParam()),
|
---|
665 | "Failed to get VM settings file path");
|
---|
666 | list.push_back(new MachineInfo(com::Utf8Str(name).c_str(),
|
---|
667 | com::Utf8Str(logFolder).c_str(),
|
---|
668 | com::Utf8Str(settingsFile).c_str()));
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | static void printHeader(void)
|
---|
673 | {
|
---|
674 | RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " Bug Report Tool " VBOX_VERSION_STRING "\n"
|
---|
675 | "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n");
|
---|
676 | }
|
---|
677 |
|
---|
678 | int main(int argc, char *argv[])
|
---|
679 | {
|
---|
680 | /*
|
---|
681 | * Initialize the VBox runtime without loading
|
---|
682 | * the support driver.
|
---|
683 | */
|
---|
684 | RTR3InitExe(argc, &argv, 0);
|
---|
685 |
|
---|
686 | bool fAllMachines = false;
|
---|
687 | bool fTextOutput = false;
|
---|
688 | const char *pszOutputFile = NULL;
|
---|
689 | std::list<const char *> nameList;
|
---|
690 | RTGETOPTUNION ValueUnion;
|
---|
691 | RTGETOPTSTATE GetState;
|
---|
692 | int ret = RTGetOptInit(&GetState, argc, argv,
|
---|
693 | g_aOptions, RT_ELEMENTS(g_aOptions),
|
---|
694 | 1 /* First */, 0 /*fFlags*/);
|
---|
695 | if (RT_FAILURE(ret))
|
---|
696 | return ret;
|
---|
697 | int ch;
|
---|
698 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
699 | {
|
---|
700 | switch(ch)
|
---|
701 | {
|
---|
702 | case 'h':
|
---|
703 | printHeader();
|
---|
704 | RTStrmPrintf(g_pStdErr, g_szUsage, argv[0]);
|
---|
705 | return 0;
|
---|
706 | case 'A':
|
---|
707 | fAllMachines = true;
|
---|
708 | break;
|
---|
709 | case 'o':
|
---|
710 | pszOutputFile = ValueUnion.psz;
|
---|
711 | break;
|
---|
712 | case 't':
|
---|
713 | fTextOutput = true;
|
---|
714 | break;
|
---|
715 | case 'V':
|
---|
716 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
717 | return 0;
|
---|
718 | case VINF_GETOPT_NOT_OPTION:
|
---|
719 | nameList.push_back(ValueUnion.psz);
|
---|
720 | break;
|
---|
721 | default:
|
---|
722 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
723 | }
|
---|
724 | }
|
---|
725 |
|
---|
726 | printHeader();
|
---|
727 |
|
---|
728 | HRESULT hr = S_OK;
|
---|
729 | char homeDir[RTPATH_MAX];
|
---|
730 | com::GetVBoxUserHomeDirectory(homeDir, sizeof(homeDir));
|
---|
731 |
|
---|
732 | try
|
---|
733 | {
|
---|
734 | /* Figure out the full path to VBoxManage */
|
---|
735 | char szVBoxBin[RTPATH_MAX];
|
---|
736 | if (!RTProcGetExecutablePath(szVBoxBin, sizeof(szVBoxBin)))
|
---|
737 | throw RTCError("RTProcGetExecutablePath failed\n");
|
---|
738 | RTPathStripFilename(szVBoxBin);
|
---|
739 | g_pszVBoxManage = RTPathJoinA(szVBoxBin, VBOXMANAGE);
|
---|
740 | if (!g_pszVBoxManage)
|
---|
741 | throw RTCError("Out of memory\n");
|
---|
742 |
|
---|
743 | handleComError(com::Initialize(VBOX_COM_INIT_F_DEFAULT | VBOX_COM_INIT_F_NO_COM_PATCHING), "Failed to initialize COM");
|
---|
744 |
|
---|
745 | MachineInfoList list;
|
---|
746 |
|
---|
747 | do
|
---|
748 | {
|
---|
749 | ComPtr<IVirtualBoxClient> virtualBoxClient;
|
---|
750 | ComPtr<IVirtualBox> virtualBox;
|
---|
751 | ComPtr<ISession> session;
|
---|
752 |
|
---|
753 | hr = virtualBoxClient.createLocalObject(CLSID_VirtualBoxClient);
|
---|
754 | if (SUCCEEDED(hr))
|
---|
755 | hr = virtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
|
---|
756 | else
|
---|
757 | hr = virtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
758 | if (FAILED(hr))
|
---|
759 | RTStrmPrintf(g_pStdErr, "WARNING: Failed to create the VirtualBox object (hr=0x%x)\n", hr);
|
---|
760 | else
|
---|
761 | {
|
---|
762 | hr = session.createInprocObject(CLSID_Session);
|
---|
763 | if (FAILED(hr))
|
---|
764 | RTStrmPrintf(g_pStdErr, "WARNING: Failed to create a session object (hr=0x%x)\n", hr);
|
---|
765 | }
|
---|
766 |
|
---|
767 | if (SUCCEEDED(hr))
|
---|
768 | {
|
---|
769 | if (fAllMachines)
|
---|
770 | {
|
---|
771 | com::SafeIfaceArray<IMachine> machines;
|
---|
772 | hr = virtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(machines));
|
---|
773 | if (SUCCEEDED(hr))
|
---|
774 | {
|
---|
775 | for (size_t i = 0; i < machines.size(); ++i)
|
---|
776 | {
|
---|
777 | if (machines[i])
|
---|
778 | addMachine(list, machines[i]);
|
---|
779 | }
|
---|
780 | }
|
---|
781 | }
|
---|
782 | else
|
---|
783 | {
|
---|
784 | for ( std::list<const char *>::iterator it = nameList.begin(); it != nameList.end(); ++it)
|
---|
785 | {
|
---|
786 | ComPtr<IMachine> machine;
|
---|
787 | handleComError(virtualBox->FindMachine(com::Bstr(*it).raw(), machine.asOutParam()),
|
---|
788 | "No such machine '%s'", *it);
|
---|
789 | addMachine(list, machine);
|
---|
790 | }
|
---|
791 | }
|
---|
792 | }
|
---|
793 |
|
---|
794 | }
|
---|
795 | while(0);
|
---|
796 |
|
---|
797 | RTTIMESPEC TimeSpec;
|
---|
798 | RTTIME Time;
|
---|
799 | RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
|
---|
800 | RTCStringFmt strOutFile("%04d-%02d-%02d-%02d-%02d-%02d-bugreport.%s",
|
---|
801 | Time.i32Year, Time.u8Month, Time.u8MonthDay,
|
---|
802 | Time.u8Hour, Time.u8Minute, Time.u8Second,
|
---|
803 | fTextOutput ? "txt" : "tgz");
|
---|
804 | RTCString strFallbackOutFile;
|
---|
805 | if (!pszOutputFile)
|
---|
806 | {
|
---|
807 | RTFILE tmp;
|
---|
808 | pszOutputFile = strOutFile.c_str();
|
---|
809 | int rc = RTFileOpen(&tmp, pszOutputFile, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
|
---|
810 | if (rc == VERR_ACCESS_DENIED)
|
---|
811 | {
|
---|
812 | char szUserHome[RTPATH_MAX];
|
---|
813 | handleRtError(RTPathUserHome(szUserHome, sizeof(szUserHome)), "Failed to obtain home directory");
|
---|
814 | strFallbackOutFile.printf("%s/%s", szUserHome, strOutFile.c_str());
|
---|
815 | pszOutputFile = strFallbackOutFile.c_str();
|
---|
816 | }
|
---|
817 | else if (RT_SUCCESS(rc))
|
---|
818 | {
|
---|
819 | RTFileClose(tmp);
|
---|
820 | RTFileDelete(pszOutputFile);
|
---|
821 | }
|
---|
822 | }
|
---|
823 | BugReport *pReport;
|
---|
824 | if (fTextOutput)
|
---|
825 | pReport = new BugReportText(pszOutputFile);
|
---|
826 | else
|
---|
827 | pReport = new BugReportTarGzip(pszOutputFile);
|
---|
828 | createBugReport(pReport, homeDir, list);
|
---|
829 | pReport->process();
|
---|
830 | pReport->complete();
|
---|
831 | RTPrintf("Report was written to '%s'\n", pszOutputFile);
|
---|
832 | delete pReport;
|
---|
833 | }
|
---|
834 | catch (RTCError &e)
|
---|
835 | {
|
---|
836 | RTStrmPrintf(g_pStdErr, "ERROR: %s\n", e.what());
|
---|
837 | }
|
---|
838 |
|
---|
839 | com::Shutdown();
|
---|
840 |
|
---|
841 | if (g_pszVBoxManage)
|
---|
842 | RTStrFree(g_pszVBoxManage);
|
---|
843 |
|
---|
844 | return SUCCEEDED(hr) ? 0 : 1;
|
---|
845 | }
|
---|