1 | /* $Id: VBoxBugReport.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxBugReport - VirtualBox command-line diagnostics tool, main file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | handleRtError(RTPathTemp(m_szFileName, RTPATH_MAX),
|
---|
273 | "Failed to obtain path to temporary folder");
|
---|
274 | handleRtError(RTPathAppend(m_szFileName, RTPATH_MAX, "BugRepXXXXX.tmp"),
|
---|
275 | "Failed to append path");
|
---|
276 | handleRtError(RTFileCreateTemp(m_szFileName, 0600),
|
---|
277 | "Failed to create temporary file '%s'", m_szFileName);
|
---|
278 | handleRtError(RTStrmOpen(m_szFileName, "w", &m_Strm),
|
---|
279 | "Failed to open '%s'", m_szFileName);
|
---|
280 | }
|
---|
281 |
|
---|
282 | BugReportStream::~BugReportStream()
|
---|
283 | {
|
---|
284 | if (m_Strm)
|
---|
285 | RTStrmClose(m_Strm);
|
---|
286 | RTFileDelete(m_szFileName);
|
---|
287 | }
|
---|
288 |
|
---|
289 | int BugReportStream::printf(const char *pszFmt, ...)
|
---|
290 | {
|
---|
291 | va_list va;
|
---|
292 | va_start(va, pszFmt);
|
---|
293 | int cb = RTStrmPrintfV(m_Strm, pszFmt, va);
|
---|
294 | va_end(va);
|
---|
295 | return cb;
|
---|
296 | }
|
---|
297 |
|
---|
298 | int BugReportStream::putStr(const char *pszString)
|
---|
299 | {
|
---|
300 | return RTStrmPutStr(m_Strm, pszString);
|
---|
301 | }
|
---|
302 |
|
---|
303 | PRTSTREAM BugReportStream::getStream(void)
|
---|
304 | {
|
---|
305 | RTStrmClose(m_Strm);
|
---|
306 | handleRtError(RTStrmOpen(m_szFileName, "r", &m_Strm),
|
---|
307 | "Failed to open '%s'", m_szFileName);
|
---|
308 | return m_Strm;
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | /* Implementation - Generic */
|
---|
313 |
|
---|
314 | BugReportFile::BugReportFile(const char *pszPath, const char *pszShortName) : BugReportItem(pszShortName)
|
---|
315 | {
|
---|
316 | m_Strm = 0;
|
---|
317 | m_pszPath = RTStrDup(pszPath);
|
---|
318 | }
|
---|
319 |
|
---|
320 | BugReportFile::~BugReportFile()
|
---|
321 | {
|
---|
322 | if (m_Strm)
|
---|
323 | RTStrmClose(m_Strm);
|
---|
324 | if (m_pszPath)
|
---|
325 | RTStrFree(m_pszPath);
|
---|
326 | }
|
---|
327 |
|
---|
328 | PRTSTREAM BugReportFile::getStream(void)
|
---|
329 | {
|
---|
330 | handleRtError(RTStrmOpen(m_pszPath, "rb", &m_Strm),
|
---|
331 | "Failed to open '%s'", m_pszPath);
|
---|
332 | return m_Strm;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | BugReportCommand::BugReportCommand(const char *pszTitle, const char *pszExec, ...)
|
---|
337 | : BugReportItem(pszTitle), m_Strm(NULL)
|
---|
338 | {
|
---|
339 | unsigned cArgs = 0;
|
---|
340 | m_papszArgs[cArgs++] = RTStrDup(pszExec);
|
---|
341 |
|
---|
342 | const char *pszArg;
|
---|
343 | va_list va;
|
---|
344 | va_start(va, pszExec);
|
---|
345 | do
|
---|
346 | {
|
---|
347 | if (cArgs >= RT_ELEMENTS(m_papszArgs))
|
---|
348 | {
|
---|
349 | va_end(va);
|
---|
350 | throw RTCError(com::Utf8StrFmt("Too many arguments (%u > %u)\n", cArgs+1, RT_ELEMENTS(m_papszArgs)));
|
---|
351 | }
|
---|
352 | pszArg = va_arg(va, const char *);
|
---|
353 | m_papszArgs[cArgs++] = pszArg ? RTStrDup(pszArg) : NULL;
|
---|
354 | } while (pszArg);
|
---|
355 | va_end(va);
|
---|
356 | }
|
---|
357 |
|
---|
358 | BugReportCommand::~BugReportCommand()
|
---|
359 | {
|
---|
360 | if (m_Strm)
|
---|
361 | RTStrmClose(m_Strm);
|
---|
362 | RTFileDelete(m_szFileName);
|
---|
363 | for (size_t i = 0; i < RT_ELEMENTS(m_papszArgs) && m_papszArgs[i]; ++i)
|
---|
364 | RTStrFree(m_papszArgs[i]);
|
---|
365 | }
|
---|
366 |
|
---|
367 | PRTSTREAM BugReportCommand::getStream(void)
|
---|
368 | {
|
---|
369 | handleRtError(RTPathTemp(m_szFileName, RTPATH_MAX),
|
---|
370 | "Failed to obtain path to temporary folder");
|
---|
371 | handleRtError(RTPathAppend(m_szFileName, RTPATH_MAX, "BugRepXXXXX.tmp"),
|
---|
372 | "Failed to append path");
|
---|
373 | handleRtError(RTFileCreateTemp(m_szFileName, 0600),
|
---|
374 | "Failed to create temporary file '%s'", m_szFileName);
|
---|
375 |
|
---|
376 | RTHANDLE hStdOutErr;
|
---|
377 | hStdOutErr.enmType = RTHANDLETYPE_FILE;
|
---|
378 | handleRtError(RTFileOpen(&hStdOutErr.u.hFile, m_szFileName,
|
---|
379 | RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE),
|
---|
380 | "Failed to open temporary file '%s'", m_szFileName);
|
---|
381 |
|
---|
382 | RTPROCESS hProcess;
|
---|
383 | handleRtError(RTProcCreateEx(m_papszArgs[0], m_papszArgs, RTENV_DEFAULT, 0,
|
---|
384 | NULL, &hStdOutErr, &hStdOutErr,
|
---|
385 | NULL, NULL, NULL, &hProcess),
|
---|
386 | "Failed to create process '%s'", m_papszArgs[0]);
|
---|
387 | RTPROCSTATUS status;
|
---|
388 | handleRtError(RTProcWait(hProcess, RTPROCWAIT_FLAGS_BLOCK, &status),
|
---|
389 | "Process wait failed");
|
---|
390 | //if (status.enmReason == RTPROCEXITREASON_NORMAL) {}
|
---|
391 | RTFileClose(hStdOutErr.u.hFile);
|
---|
392 |
|
---|
393 | handleRtError(RTStrmOpen(m_szFileName, "r", &m_Strm),
|
---|
394 | "Failed to open '%s'", m_szFileName);
|
---|
395 | return m_Strm;
|
---|
396 | }
|
---|
397 |
|
---|
398 |
|
---|
399 | BugReportCommandTemp::BugReportCommandTemp(const char *pszTitle, const char *pszExec, ...)
|
---|
400 | : BugReportItem(pszTitle), m_Strm(NULL)
|
---|
401 | {
|
---|
402 | handleRtError(RTPathTemp(m_szFileName, RTPATH_MAX),
|
---|
403 | "Failed to obtain path to temporary folder");
|
---|
404 | handleRtError(RTPathAppend(m_szFileName, RTPATH_MAX, "BugRepXXXXX.tmp"),
|
---|
405 | "Failed to append path");
|
---|
406 | handleRtError(RTFileCreateTemp(m_szFileName, 0600),
|
---|
407 | "Failed to create temporary file '%s'", m_szFileName);
|
---|
408 |
|
---|
409 | unsigned cArgs = 0;
|
---|
410 | m_papszArgs[cArgs++] = RTStrDup(pszExec);
|
---|
411 |
|
---|
412 | const char *pszArg;
|
---|
413 | va_list va;
|
---|
414 | va_start(va, pszExec);
|
---|
415 | do
|
---|
416 | {
|
---|
417 | if (cArgs >= RT_ELEMENTS(m_papszArgs) - 1)
|
---|
418 | {
|
---|
419 | va_end(va);
|
---|
420 | throw RTCError(com::Utf8StrFmt("Too many arguments (%u > %u)\n", cArgs+1, RT_ELEMENTS(m_papszArgs)));
|
---|
421 | }
|
---|
422 | pszArg = va_arg(va, const char *);
|
---|
423 | m_papszArgs[cArgs++] = RTStrDup(pszArg ? pszArg : m_szFileName);
|
---|
424 | } while (pszArg);
|
---|
425 | va_end(va);
|
---|
426 |
|
---|
427 | m_papszArgs[cArgs++] = NULL;
|
---|
428 | }
|
---|
429 |
|
---|
430 | BugReportCommandTemp::~BugReportCommandTemp()
|
---|
431 | {
|
---|
432 | if (m_Strm)
|
---|
433 | RTStrmClose(m_Strm);
|
---|
434 | RTFileDelete(m_szErrFileName);
|
---|
435 | RTFileDelete(m_szFileName);
|
---|
436 | for (size_t i = 0; i < RT_ELEMENTS(m_papszArgs) && m_papszArgs[i]; ++i)
|
---|
437 | RTStrFree(m_papszArgs[i]);
|
---|
438 | }
|
---|
439 |
|
---|
440 | PRTSTREAM BugReportCommandTemp::getStream(void)
|
---|
441 | {
|
---|
442 | handleRtError(RTPathTemp(m_szErrFileName, RTPATH_MAX),
|
---|
443 | "Failed to obtain path to temporary folder");
|
---|
444 | handleRtError(RTPathAppend(m_szErrFileName, RTPATH_MAX, "BugRepErrXXXXX.tmp"),
|
---|
445 | "Failed to append path");
|
---|
446 | handleRtError(RTFileCreateTemp(m_szErrFileName, 0600),
|
---|
447 | "Failed to create temporary file '%s'", m_szErrFileName);
|
---|
448 |
|
---|
449 | RTHANDLE hStdOutErr;
|
---|
450 | hStdOutErr.enmType = RTHANDLETYPE_FILE;
|
---|
451 | handleRtError(RTFileOpen(&hStdOutErr.u.hFile, m_szErrFileName,
|
---|
452 | RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE),
|
---|
453 | "Failed to open temporary file '%s'", m_szErrFileName);
|
---|
454 |
|
---|
455 | /* Remove the output file to prevent errors or confirmation prompts */
|
---|
456 | handleRtError(RTFileDelete(m_szFileName),
|
---|
457 | "Failed to delete temporary file '%s'", m_szFileName);
|
---|
458 |
|
---|
459 | RTPROCESS hProcess;
|
---|
460 | handleRtError(RTProcCreateEx(m_papszArgs[0], m_papszArgs, RTENV_DEFAULT, 0,
|
---|
461 | NULL, &hStdOutErr, &hStdOutErr,
|
---|
462 | NULL, NULL, NULL, &hProcess),
|
---|
463 | "Failed to create process '%s'", m_papszArgs[0]);
|
---|
464 | RTPROCSTATUS status;
|
---|
465 | handleRtError(RTProcWait(hProcess, RTPROCWAIT_FLAGS_BLOCK, &status),
|
---|
466 | "Process wait failed");
|
---|
467 | RTFileClose(hStdOutErr.u.hFile);
|
---|
468 |
|
---|
469 | if (status.enmReason == RTPROCEXITREASON_NORMAL && status.iStatus == 0)
|
---|
470 | handleRtError(RTStrmOpen(m_szFileName, "r", &m_Strm), "Failed to open '%s'", m_szFileName);
|
---|
471 | else
|
---|
472 | handleRtError(RTStrmOpen(m_szErrFileName, "r", &m_Strm), "Failed to open '%s'", m_szErrFileName);
|
---|
473 | return m_Strm;
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | BugReportText::BugReportText(const char *pszFileName) : BugReport(pszFileName)
|
---|
478 | {
|
---|
479 | handleRtError(RTStrmOpen(pszFileName, "w", &m_StrmTxt),
|
---|
480 | "Failed to open '%s'", pszFileName);
|
---|
481 | }
|
---|
482 |
|
---|
483 | BugReportText::~BugReportText()
|
---|
484 | {
|
---|
485 | if (m_StrmTxt)
|
---|
486 | RTStrmClose(m_StrmTxt);
|
---|
487 | }
|
---|
488 |
|
---|
489 | void BugReportText::processItem(BugReportItem* item)
|
---|
490 | {
|
---|
491 | int cb = RTStrmPrintf(m_StrmTxt, "[ %s ] -------------------------------------------\n", item->getTitle());
|
---|
492 | if (!cb)
|
---|
493 | throw RTCError(com::Utf8StrFmt("Write failure (cb=%d)\n", cb));
|
---|
494 |
|
---|
495 | PRTSTREAM strmIn = NULL;
|
---|
496 | try
|
---|
497 | {
|
---|
498 | strmIn = item->getStream();
|
---|
499 | }
|
---|
500 | catch (RTCError &e)
|
---|
501 | {
|
---|
502 | strmIn = NULL;
|
---|
503 | RTStrmPutStr(m_StrmTxt, e.what());
|
---|
504 | }
|
---|
505 |
|
---|
506 | int rc = VINF_SUCCESS;
|
---|
507 |
|
---|
508 | if (strmIn)
|
---|
509 | {
|
---|
510 | char buf[64*1024];
|
---|
511 | size_t cbRead, cbWritten;
|
---|
512 | cbRead = cbWritten = 0;
|
---|
513 | while (RT_SUCCESS(rc = RTStrmReadEx(strmIn, buf, sizeof(buf), &cbRead)) && cbRead)
|
---|
514 | {
|
---|
515 | rc = RTStrmWriteEx(m_StrmTxt, applyFilters(item, buf, &cbRead), cbRead, &cbWritten);
|
---|
516 | if (RT_FAILURE(rc) || cbRead != cbWritten)
|
---|
517 | throw RTCError(com::Utf8StrFmt("Write failure (rc=%d, cbRead=%lu, cbWritten=%lu)\n",
|
---|
518 | rc, cbRead, cbWritten));
|
---|
519 | }
|
---|
520 | }
|
---|
521 |
|
---|
522 | handleRtError(RTStrmPutCh(m_StrmTxt, '\n'), "Write failure");
|
---|
523 | }
|
---|
524 |
|
---|
525 |
|
---|
526 | BugReportTarGzip::BugReportTarGzip(const char *pszFileName)
|
---|
527 | : BugReport(pszFileName), m_hTar(NIL_RTTAR), m_hTarFile(NIL_RTTARFILE)
|
---|
528 | {
|
---|
529 | VfsIoStreamHandle hVfsOut;
|
---|
530 | handleRtError(RTVfsIoStrmOpenNormal(pszFileName, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE,
|
---|
531 | hVfsOut.getPtr()),
|
---|
532 | "Failed to create output file '%s'", pszFileName);
|
---|
533 | handleRtError(RTZipGzipCompressIoStream(hVfsOut.get(), 0, 6, m_hVfsGzip.getPtr()),
|
---|
534 | "Failed to create compressed stream for '%s'", pszFileName);
|
---|
535 |
|
---|
536 | handleRtError(RTPathTemp(m_szTarName, RTPATH_MAX),
|
---|
537 | "Failed to obtain path to temporary folder");
|
---|
538 | handleRtError(RTPathAppend(m_szTarName, RTPATH_MAX, "BugRepXXXXX.tar"),
|
---|
539 | "Failed to append path");
|
---|
540 | handleRtError(RTFileCreateTemp(m_szTarName, 0600),
|
---|
541 | "Failed to create temporary file '%s'", m_szTarName);
|
---|
542 | handleRtError(RTFileDelete(m_szTarName),
|
---|
543 | "Failed to delete temporary file '%s'", m_szTarName);
|
---|
544 | handleRtError(RTTarOpen(&m_hTar, m_szTarName, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_ALL),
|
---|
545 | "Failed to create TAR file '%s'", m_szTarName);
|
---|
546 |
|
---|
547 | }
|
---|
548 |
|
---|
549 | BugReportTarGzip::~BugReportTarGzip()
|
---|
550 | {
|
---|
551 | if (m_hTarFile != NIL_RTTARFILE)
|
---|
552 | RTTarFileClose(m_hTarFile);
|
---|
553 | if (m_hTar != NIL_RTTAR)
|
---|
554 | RTTarClose(m_hTar);
|
---|
555 | }
|
---|
556 |
|
---|
557 | void BugReportTarGzip::processItem(BugReportItem* item)
|
---|
558 | {
|
---|
559 | /*
|
---|
560 | * @todo Our TAR implementation does not support names larger than 100 characters.
|
---|
561 | * We truncate the title to make sure it will fit into 100-character field of TAR header.
|
---|
562 | */
|
---|
563 | RTCString strTarFile = RTCString(item->getTitle()).substr(0, RTStrNLen(item->getTitle(), 99));
|
---|
564 | handleRtError(RTTarFileOpen(m_hTar, &m_hTarFile, strTarFile.c_str(),
|
---|
565 | RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE),
|
---|
566 | "Failed to open '%s' in TAR", strTarFile.c_str());
|
---|
567 |
|
---|
568 | PRTSTREAM strmIn = NULL;
|
---|
569 | try
|
---|
570 | {
|
---|
571 | strmIn = item->getStream();
|
---|
572 | }
|
---|
573 | catch (RTCError &e)
|
---|
574 | {
|
---|
575 | strmIn = NULL;
|
---|
576 | handleRtError(RTTarFileWriteAt(m_hTarFile, 0, e.what(), RTStrNLen(e.what(), 1024), NULL),
|
---|
577 | "Failed to write %u bytes to TAR", RTStrNLen(e.what(), 1024));
|
---|
578 | }
|
---|
579 |
|
---|
580 | int rc = VINF_SUCCESS;
|
---|
581 |
|
---|
582 | if (strmIn)
|
---|
583 | {
|
---|
584 | char buf[64*1024];
|
---|
585 | size_t cbRead = 0;
|
---|
586 | for (uint64_t offset = 0;
|
---|
587 | RT_SUCCESS(rc = RTStrmReadEx(strmIn, buf, sizeof(buf), &cbRead)) && cbRead;
|
---|
588 | offset += cbRead)
|
---|
589 | {
|
---|
590 | handleRtError(RTTarFileWriteAt(m_hTarFile, offset, applyFilters(item, buf, &cbRead), cbRead, NULL),
|
---|
591 | "Failed to write %u bytes to TAR", cbRead);
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | if (m_hTarFile)
|
---|
596 | {
|
---|
597 | handleRtError(RTTarFileClose(m_hTarFile), "Failed to close '%s' in TAR", strTarFile.c_str());
|
---|
598 | m_hTarFile = NIL_RTTARFILE;
|
---|
599 | }
|
---|
600 | }
|
---|
601 |
|
---|
602 | void BugReportTarGzip::complete(void)
|
---|
603 | {
|
---|
604 | if (m_hTarFile != NIL_RTTARFILE)
|
---|
605 | {
|
---|
606 | RTTarFileClose(m_hTarFile);
|
---|
607 | m_hTarFile = NIL_RTTARFILE;
|
---|
608 | }
|
---|
609 | if (m_hTar != NIL_RTTAR)
|
---|
610 | {
|
---|
611 | RTTarClose(m_hTar);
|
---|
612 | m_hTar = NIL_RTTAR;
|
---|
613 | }
|
---|
614 |
|
---|
615 | VfsIoStreamHandle hVfsIn;
|
---|
616 | handleRtError(RTVfsIoStrmOpenNormal(m_szTarName, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
|
---|
617 | hVfsIn.getPtr()),
|
---|
618 | "Failed to open TAR file '%s'", m_szTarName);
|
---|
619 |
|
---|
620 | int rc;
|
---|
621 | char buf[_64K];
|
---|
622 | size_t cbRead = 0;
|
---|
623 | while (RT_SUCCESS(rc = RTVfsIoStrmRead(hVfsIn.get(), buf, sizeof(buf), true, &cbRead)) && cbRead)
|
---|
624 | handleRtError(RTVfsIoStrmWrite(m_hVfsGzip.get(), buf, cbRead, true, NULL),
|
---|
625 | "Failed to write into compressed stream");
|
---|
626 | handleRtError(rc, "Failed to read from TAR stream");
|
---|
627 | handleRtError(RTVfsIoStrmFlush(m_hVfsGzip.get()), "Failed to flush output stream");
|
---|
628 | m_hVfsGzip.release();
|
---|
629 | }
|
---|
630 |
|
---|
631 |
|
---|
632 | /* Implementation - Main */
|
---|
633 |
|
---|
634 | void createBugReport(BugReport* report, const char *pszHome, MachineInfoList& machines)
|
---|
635 | {
|
---|
636 | /* Collect all log files from VBoxSVC */
|
---|
637 | VBRDir HomeDir(PathJoin(pszHome, "VBoxSVC.log*"));
|
---|
638 | const char *pcszSvcLogFile = HomeDir.next();
|
---|
639 | while (pcszSvcLogFile)
|
---|
640 | {
|
---|
641 | report->addItem(new BugReportFile(PathJoin(pszHome, pcszSvcLogFile), pcszSvcLogFile));
|
---|
642 | pcszSvcLogFile = HomeDir.next();
|
---|
643 | }
|
---|
644 |
|
---|
645 | report->addItem(new BugReportFile(PathJoin(pszHome, "VirtualBox.xml"), "VirtualBox.xml"));
|
---|
646 | report->addItem(new BugReportCommand("HostUsbDevices", g_pszVBoxManage, "list", "usbhost", NULL));
|
---|
647 | report->addItem(new BugReportCommand("HostUsbFilters", g_pszVBoxManage, "list", "usbfilters", NULL));
|
---|
648 | for (MachineInfoList::iterator it = machines.begin(); it != machines.end(); ++it)
|
---|
649 | {
|
---|
650 | VBRDir VmDir(PathJoin((*it)->getLogPath(), "VBox.log*"));
|
---|
651 | const char *pcszVmLogFile = VmDir.next();
|
---|
652 | while (pcszVmLogFile)
|
---|
653 | {
|
---|
654 | report->addItem(new BugReportFile(PathJoin((*it)->getLogPath(), pcszVmLogFile),
|
---|
655 | PathJoin((*it)->getName(), pcszVmLogFile)));
|
---|
656 | pcszVmLogFile = VmDir.next();
|
---|
657 | }
|
---|
658 | report->addItem(new BugReportFile((*it)->getSettingsFile(),
|
---|
659 | PathJoin((*it)->getName(), RTPathFilename((*it)->getSettingsFile()))));
|
---|
660 | report->addItem(new BugReportCommand(PathJoin((*it)->getName(), "GuestProperties"),
|
---|
661 | g_pszVBoxManage, "guestproperty", "enumerate",
|
---|
662 | (*it)->getName(), NULL));
|
---|
663 | }
|
---|
664 |
|
---|
665 | createBugReportOsSpecific(report, pszHome);
|
---|
666 | }
|
---|
667 |
|
---|
668 | void addMachine(MachineInfoList& list, ComPtr<IMachine> machine)
|
---|
669 | {
|
---|
670 | BOOL fAccessible = FALSE;
|
---|
671 | HRESULT hrc = machine->COMGETTER(Accessible)(&fAccessible);
|
---|
672 | if (SUCCEEDED(hrc) && !fAccessible)
|
---|
673 | return
|
---|
674 | handleComError(hrc, "Failed to get accessible status of VM");
|
---|
675 |
|
---|
676 | com::Bstr name, logFolder, settingsFile;
|
---|
677 | handleComError(machine->COMGETTER(Name)(name.asOutParam()),
|
---|
678 | "Failed to get VM name");
|
---|
679 | handleComError(machine->COMGETTER(LogFolder)(logFolder.asOutParam()),
|
---|
680 | "Failed to get VM log folder");
|
---|
681 | handleComError(machine->COMGETTER(SettingsFilePath)(settingsFile.asOutParam()),
|
---|
682 | "Failed to get VM settings file path");
|
---|
683 | list.push_back(new MachineInfo(com::Utf8Str(name).c_str(),
|
---|
684 | com::Utf8Str(logFolder).c_str(),
|
---|
685 | com::Utf8Str(settingsFile).c_str()));
|
---|
686 | }
|
---|
687 |
|
---|
688 |
|
---|
689 | static void printHeader(void)
|
---|
690 | {
|
---|
691 | RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " Bug Report Tool " VBOX_VERSION_STRING "\n"
|
---|
692 | "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n");
|
---|
693 | }
|
---|
694 |
|
---|
695 | int main(int argc, char *argv[])
|
---|
696 | {
|
---|
697 | /*
|
---|
698 | * Initialize the VBox runtime without loading
|
---|
699 | * the support driver.
|
---|
700 | */
|
---|
701 | RTR3InitExe(argc, &argv, 0);
|
---|
702 |
|
---|
703 | bool fAllMachines = false;
|
---|
704 | bool fTextOutput = false;
|
---|
705 | const char *pszOutputFile = NULL;
|
---|
706 | std::list<const char *> nameList;
|
---|
707 | RTGETOPTUNION ValueUnion;
|
---|
708 | RTGETOPTSTATE GetState;
|
---|
709 | int ret = RTGetOptInit(&GetState, argc, argv,
|
---|
710 | g_aOptions, RT_ELEMENTS(g_aOptions),
|
---|
711 | 1 /* First */, 0 /*fFlags*/);
|
---|
712 | if (RT_FAILURE(ret))
|
---|
713 | return ret;
|
---|
714 | int ch;
|
---|
715 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
716 | {
|
---|
717 | switch(ch)
|
---|
718 | {
|
---|
719 | case 'h':
|
---|
720 | printHeader();
|
---|
721 | RTStrmPrintf(g_pStdErr, g_szUsage, argv[0]);
|
---|
722 | return 0;
|
---|
723 | case 'A':
|
---|
724 | fAllMachines = true;
|
---|
725 | break;
|
---|
726 | case 'o':
|
---|
727 | pszOutputFile = ValueUnion.psz;
|
---|
728 | break;
|
---|
729 | case 't':
|
---|
730 | fTextOutput = true;
|
---|
731 | break;
|
---|
732 | case 'V':
|
---|
733 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
734 | return 0;
|
---|
735 | case VINF_GETOPT_NOT_OPTION:
|
---|
736 | nameList.push_back(ValueUnion.psz);
|
---|
737 | break;
|
---|
738 | default:
|
---|
739 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | printHeader();
|
---|
744 |
|
---|
745 | HRESULT hr = S_OK;
|
---|
746 | char homeDir[RTPATH_MAX];
|
---|
747 | com::GetVBoxUserHomeDirectory(homeDir, sizeof(homeDir));
|
---|
748 |
|
---|
749 | try
|
---|
750 | {
|
---|
751 | /* Figure out the full path to VBoxManage */
|
---|
752 | char szVBoxBin[RTPATH_MAX];
|
---|
753 | if (!RTProcGetExecutablePath(szVBoxBin, sizeof(szVBoxBin)))
|
---|
754 | throw RTCError("RTProcGetExecutablePath failed\n");
|
---|
755 | RTPathStripFilename(szVBoxBin);
|
---|
756 | g_pszVBoxManage = RTPathJoinA(szVBoxBin, VBOXMANAGE);
|
---|
757 | if (!g_pszVBoxManage)
|
---|
758 | throw RTCError("Out of memory\n");
|
---|
759 |
|
---|
760 | handleComError(com::Initialize(VBOX_COM_INIT_F_DEFAULT | VBOX_COM_INIT_F_NO_COM_PATCHING), "Failed to initialize COM");
|
---|
761 |
|
---|
762 | MachineInfoList list;
|
---|
763 |
|
---|
764 | do
|
---|
765 | {
|
---|
766 | ComPtr<IVirtualBoxClient> virtualBoxClient;
|
---|
767 | ComPtr<IVirtualBox> virtualBox;
|
---|
768 | ComPtr<ISession> session;
|
---|
769 |
|
---|
770 | hr = virtualBoxClient.createLocalObject(CLSID_VirtualBoxClient);
|
---|
771 | if (SUCCEEDED(hr))
|
---|
772 | hr = virtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
|
---|
773 | else
|
---|
774 | hr = virtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
775 | if (FAILED(hr))
|
---|
776 | RTStrmPrintf(g_pStdErr, "WARNING: Failed to create the VirtualBox object (hr=0x%x)\n", hr);
|
---|
777 | else
|
---|
778 | {
|
---|
779 | hr = session.createInprocObject(CLSID_Session);
|
---|
780 | if (FAILED(hr))
|
---|
781 | RTStrmPrintf(g_pStdErr, "WARNING: Failed to create a session object (hr=0x%x)\n", hr);
|
---|
782 | }
|
---|
783 |
|
---|
784 | if (SUCCEEDED(hr))
|
---|
785 | {
|
---|
786 | if (fAllMachines)
|
---|
787 | {
|
---|
788 | com::SafeIfaceArray<IMachine> machines;
|
---|
789 | hr = virtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(machines));
|
---|
790 | if (SUCCEEDED(hr))
|
---|
791 | {
|
---|
792 | for (size_t i = 0; i < machines.size(); ++i)
|
---|
793 | {
|
---|
794 | if (machines[i])
|
---|
795 | addMachine(list, machines[i]);
|
---|
796 | }
|
---|
797 | }
|
---|
798 | }
|
---|
799 | else
|
---|
800 | {
|
---|
801 | for ( std::list<const char *>::iterator it = nameList.begin(); it != nameList.end(); ++it)
|
---|
802 | {
|
---|
803 | ComPtr<IMachine> machine;
|
---|
804 | handleComError(virtualBox->FindMachine(com::Bstr(*it).raw(), machine.asOutParam()),
|
---|
805 | "No such machine '%s'", *it);
|
---|
806 | addMachine(list, machine);
|
---|
807 | }
|
---|
808 | }
|
---|
809 | }
|
---|
810 |
|
---|
811 | }
|
---|
812 | while(0);
|
---|
813 |
|
---|
814 | RTTIMESPEC TimeSpec;
|
---|
815 | RTTIME Time;
|
---|
816 | RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
|
---|
817 | RTCStringFmt strOutFile("%04d-%02d-%02d-%02d-%02d-%02d-bugreport.%s",
|
---|
818 | Time.i32Year, Time.u8Month, Time.u8MonthDay,
|
---|
819 | Time.u8Hour, Time.u8Minute, Time.u8Second,
|
---|
820 | fTextOutput ? "txt" : "tgz");
|
---|
821 | RTCString strFallbackOutFile;
|
---|
822 | if (!pszOutputFile)
|
---|
823 | {
|
---|
824 | RTFILE tmp;
|
---|
825 | pszOutputFile = strOutFile.c_str();
|
---|
826 | int rc = RTFileOpen(&tmp, pszOutputFile, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
|
---|
827 | if (rc == VERR_ACCESS_DENIED)
|
---|
828 | {
|
---|
829 | char szUserHome[RTPATH_MAX];
|
---|
830 | handleRtError(RTPathUserHome(szUserHome, sizeof(szUserHome)), "Failed to obtain home directory");
|
---|
831 | strFallbackOutFile.printf("%s/%s", szUserHome, strOutFile.c_str());
|
---|
832 | pszOutputFile = strFallbackOutFile.c_str();
|
---|
833 | }
|
---|
834 | else if (RT_SUCCESS(rc))
|
---|
835 | {
|
---|
836 | RTFileClose(tmp);
|
---|
837 | RTFileDelete(pszOutputFile);
|
---|
838 | }
|
---|
839 | }
|
---|
840 | BugReport *pReport;
|
---|
841 | if (fTextOutput)
|
---|
842 | pReport = new BugReportText(pszOutputFile);
|
---|
843 | else
|
---|
844 | pReport = new BugReportTarGzip(pszOutputFile);
|
---|
845 | createBugReport(pReport, homeDir, list);
|
---|
846 | pReport->process();
|
---|
847 | pReport->complete();
|
---|
848 | RTPrintf("Report was written to '%s'\n", pszOutputFile);
|
---|
849 | delete pReport;
|
---|
850 | }
|
---|
851 | catch (RTCError &e)
|
---|
852 | {
|
---|
853 | RTStrmPrintf(g_pStdErr, "ERROR: %s\n", e.what());
|
---|
854 | }
|
---|
855 |
|
---|
856 | com::Shutdown();
|
---|
857 |
|
---|
858 | if (g_pszVBoxManage)
|
---|
859 | RTStrFree(g_pszVBoxManage);
|
---|
860 |
|
---|
861 | return SUCCEEDED(hr) ? 0 : 1;
|
---|
862 | }
|
---|