VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxBugReport/VBoxBugReport.cpp@ 64761

Last change on this file since 64761 was 63735, checked in by vboxsync, 8 years ago

BugReport(bugref:7809): Do not throw exceptions in destructors.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.4 KB
Line 
1/* $Id: VBoxBugReport.cpp 63735 2016-09-06 08:31:10Z vboxsync $ */
2/** @file
3 * VBoxBugReport - VirtualBox command-line diagnostics tool, main file.
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#include <VBox/com/com.h>
20#include <VBox/com/string.h>
21#include <VBox/com/array.h>
22//#include <VBox/com/Guid.h>
23#include <VBox/com/ErrorInfo.h>
24#include <VBox/com/errorprint.h>
25#include <VBox/com/VirtualBox.h>
26
27#include <VBox/version.h>
28
29#include <iprt/buildconfig.h>
30#include <iprt/env.h>
31#include <iprt/file.h>
32#include <iprt/getopt.h>
33#include <iprt/initterm.h>
34#include <iprt/path.h>
35#include <iprt/process.h>
36#include <iprt/zip.h>
37#include <iprt/cpp/exception.h>
38
39#include <list>
40
41#include "VBoxBugReport.h"
42
43/* Implementation - Base */
44
45#ifndef RT_OS_WINDOWS
46/** @todo Replace with platform-specific implementations. */
47void createBugReportOsSpecific(BugReport *pReport, const char *pszHome)
48{
49 RT_NOREF(pReport, pszHome);
50}
51#endif /* !RT_OS_WINDOWS */
52
53
54/* Globals */
55
56static char *g_pszVBoxManage = NULL;
57
58static const RTGETOPTDEF g_aOptions[] =
59{
60 { "-all", 'A', RTGETOPT_REQ_NOTHING },
61 { "--all", 'A', RTGETOPT_REQ_NOTHING },
62 { "-output", 'o', RTGETOPT_REQ_STRING },
63 { "--output", 'o', RTGETOPT_REQ_STRING },
64 { "-text", 't', RTGETOPT_REQ_NOTHING },
65 { "--text", 't', RTGETOPT_REQ_NOTHING }
66};
67
68static const char g_szUsage[] =
69 "Usage: %s [-h|-?|--help] [-A|--all|<vmname>...] [-o <file>|--output=<file>]\n"
70 " Several VM names can be specified at once to be included into single report.\n"
71 " If none is given then no machines will be included. Specifying -A overrides\n"
72 " any VM names provided and included all registered machines.\n"
73 "Options:\n"
74 " -h, -help, --help Print usage information\n"
75 " -A, -all, --all Include all registered machines\n"
76 " -o, -output, --output Specifies the name of the output file\n"
77 " -t, -text, --text Produce a single text file instead of compressed TAR\n"
78 " -V, -version, --version Print version number and exit\n"
79 "\n";
80
81
82/*
83 * This class stores machine-specific file paths that are obtained via
84 * VirtualBox API. In case API is not functioning properly these paths
85 * will be deduced on the best effort basis.
86 */
87class MachineInfo
88{
89public:
90 MachineInfo(const char *name, const char *logFolder, const char *settingsFile);
91 ~MachineInfo();
92 const char *getName() const { return m_name; };
93 const char *getLogPath() const { return m_logpath; };
94 const char *getSettingsFile() const { return m_settings; };
95private:
96 char *m_name;
97 char *m_logpath;
98 char *m_settings;
99};
100
101MachineInfo::MachineInfo(const char *name, const char *logFolder, const char *settingsFile)
102{
103 m_name = RTStrDup(name);
104 m_logpath = RTStrDup(logFolder);
105 m_settings = RTStrDup(settingsFile);
106}
107
108MachineInfo::~MachineInfo()
109{
110 RTStrFree(m_logpath);
111 RTStrFree(m_name);
112 RTStrFree(m_settings);
113 m_logpath = m_name = m_settings = 0;
114}
115
116typedef std::list<MachineInfo*> MachineInfoList;
117
118
119class VBRDir
120{
121public:
122 VBRDir(const char *pcszPath)
123 {
124 int rc = RTDirOpenFiltered(&m_pDir, pcszPath, RTDIRFILTER_WINNT, 0);
125 if (RT_FAILURE(rc))
126 throw RTCError(com::Utf8StrFmt("Failed to open directory '%s'\n", pcszPath));
127 };
128 ~VBRDir()
129 {
130 int rc = RTDirClose(m_pDir);
131 AssertRC(rc);
132 };
133 const char *next(void)
134 {
135 int rc = RTDirRead(m_pDir, &m_DirEntry, NULL);
136 if (RT_SUCCESS(rc))
137 return m_DirEntry.szName;
138 else if (rc == VERR_NO_MORE_FILES)
139 return NULL;
140 throw RTCError("Failed to read directory element\n");
141 };
142
143private:
144 PRTDIR m_pDir;
145 RTDIRENTRY m_DirEntry;
146};
147
148/*
149 * An abstract class serving as the root of the bug report item tree.
150 */
151BugReportItem::BugReportItem(const char *pszTitle)
152{
153 m_pszTitle = RTStrDup(pszTitle);
154}
155
156BugReportItem::~BugReportItem()
157{
158 RTStrFree(m_pszTitle);
159}
160
161const char * BugReportItem::getTitle(void)
162{
163 return m_pszTitle;
164}
165
166
167BugReport::BugReport(const char *pszFileName)
168{
169 m_pszFileName = RTStrDup(pszFileName);
170}
171
172BugReport::~BugReport()
173{
174 for (unsigned i = 0; i < m_Items.size(); ++i)
175 {
176 delete m_Items[i];
177 }
178 RTStrFree(m_pszFileName);
179}
180
181int BugReport::getItemCount(void)
182{
183 return (int)m_Items.size();
184}
185
186void BugReport::addItem(BugReportItem* item)
187{
188 if (item)
189 m_Items.append(item);
190}
191
192void BugReport::process(void)
193{
194 for (unsigned i = 0; i < m_Items.size(); ++i)
195 {
196 BugReportItem *pItem = m_Items[i];
197 RTPrintf("%3u%% - collecting %s...\n", i * 100 / m_Items.size(), pItem->getTitle());
198 processItem(pItem);
199 }
200 RTPrintf("100%% - compressing...\n\n");
201}
202
203
204BugReportStream::BugReportStream(const char *pszTitle) : BugReportItem(pszTitle)
205{
206 handleRtError(RTPathTemp(m_szFileName, RTPATH_MAX),
207 "Failed to obtain path to temporary folder");
208 handleRtError(RTPathAppend(m_szFileName, RTPATH_MAX, "BugRepXXXXX.tmp"),
209 "Failed to append path");
210 handleRtError(RTFileCreateTemp(m_szFileName, 0600),
211 "Failed to create temporary file '%s'", m_szFileName);
212 handleRtError(RTStrmOpen(m_szFileName, "w", &m_Strm),
213 "Failed to open '%s'", m_szFileName);
214}
215
216BugReportStream::~BugReportStream()
217{
218 if (m_Strm)
219 RTStrmClose(m_Strm);
220 RTFileDelete(m_szFileName);
221}
222
223int BugReportStream::printf(const char *pszFmt, ...)
224{
225 va_list va;
226 va_start(va, pszFmt);
227 int cb = RTStrmPrintfV(m_Strm, pszFmt, va);
228 va_end(va);
229 return cb;
230}
231
232int BugReportStream::putStr(const char *pszString)
233{
234 return RTStrmPutStr(m_Strm, pszString);
235}
236
237PRTSTREAM BugReportStream::getStream(void)
238{
239 RTStrmClose(m_Strm);
240 handleRtError(RTStrmOpen(m_szFileName, "r", &m_Strm),
241 "Failed to open '%s'", m_szFileName);
242 return m_Strm;
243}
244
245
246/* Implementation - Generic */
247
248BugReportFile::BugReportFile(const char *pszPath, const char *pszShortName) : BugReportItem(pszShortName)
249{
250 m_Strm = 0;
251 m_pszPath = RTStrDup(pszPath);
252}
253
254BugReportFile::~BugReportFile()
255{
256 if (m_Strm)
257 RTStrmClose(m_Strm);
258 if (m_pszPath)
259 RTStrFree(m_pszPath);
260}
261
262PRTSTREAM BugReportFile::getStream(void)
263{
264 handleRtError(RTStrmOpen(m_pszPath, "rb", &m_Strm),
265 "Failed to open '%s'", m_pszPath);
266 return m_Strm;
267}
268
269
270BugReportCommand::BugReportCommand(const char *pszTitle, const char *pszExec, ...)
271 : BugReportItem(pszTitle), m_Strm(NULL)
272{
273 unsigned cArgs = 0;
274 m_papszArgs[cArgs++] = RTStrDup(pszExec);
275
276 const char *pszArg;
277 va_list va;
278 va_start(va, pszExec);
279 do
280 {
281 if (cArgs >= RT_ELEMENTS(m_papszArgs))
282 {
283 va_end(va);
284 throw RTCError(com::Utf8StrFmt("Too many arguments (%u > %u)\n", cArgs+1, RT_ELEMENTS(m_papszArgs)));
285 }
286 pszArg = va_arg(va, const char *);
287 m_papszArgs[cArgs++] = pszArg ? RTStrDup(pszArg) : NULL;
288 } while (pszArg);
289 va_end(va);
290}
291
292BugReportCommand::~BugReportCommand()
293{
294 if (m_Strm)
295 RTStrmClose(m_Strm);
296 RTFileDelete(m_szFileName);
297 for (size_t i = 0; i < RT_ELEMENTS(m_papszArgs) && m_papszArgs[i]; ++i)
298 RTStrFree(m_papszArgs[i]);
299}
300
301PRTSTREAM BugReportCommand::getStream(void)
302{
303 handleRtError(RTPathTemp(m_szFileName, RTPATH_MAX),
304 "Failed to obtain path to temporary folder");
305 handleRtError(RTPathAppend(m_szFileName, RTPATH_MAX, "BugRepXXXXX.tmp"),
306 "Failed to append path");
307 handleRtError(RTFileCreateTemp(m_szFileName, 0600),
308 "Failed to create temporary file '%s'", m_szFileName);
309
310 RTHANDLE hStdOutErr;
311 hStdOutErr.enmType = RTHANDLETYPE_FILE;
312 handleRtError(RTFileOpen(&hStdOutErr.u.hFile, m_szFileName,
313 RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE),
314 "Failed to open temporary file '%s'", m_szFileName);
315
316 RTPROCESS hProcess;
317 handleRtError(RTProcCreateEx(m_papszArgs[0], m_papszArgs, RTENV_DEFAULT, 0,
318 NULL, &hStdOutErr, &hStdOutErr,
319 NULL, NULL, &hProcess),
320 "Failed to create process '%s'", m_papszArgs[0]);
321 RTPROCSTATUS status;
322 handleRtError(RTProcWait(hProcess, RTPROCWAIT_FLAGS_BLOCK, &status),
323 "Process wait failed");
324 //if (status.enmReason == RTPROCEXITREASON_NORMAL) {}
325 RTFileClose(hStdOutErr.u.hFile);
326
327 handleRtError(RTStrmOpen(m_szFileName, "r", &m_Strm),
328 "Failed to open '%s'", m_szFileName);
329 return m_Strm;
330}
331
332
333BugReportText::BugReportText(const char *pszFileName) : BugReport(pszFileName)
334{
335 handleRtError(RTStrmOpen(pszFileName, "w", &m_StrmTxt),
336 "Failed to open '%s'", pszFileName);
337}
338
339BugReportText::~BugReportText()
340{
341 if (m_StrmTxt)
342 RTStrmClose(m_StrmTxt);
343}
344
345void BugReportText::processItem(BugReportItem* item)
346{
347 int cb = RTStrmPrintf(m_StrmTxt, "[ %s ] -------------------------------------------\n", item->getTitle());
348 if (!cb)
349 throw RTCError(com::Utf8StrFmt("Write failure (cb=%d)\n", cb));
350
351 PRTSTREAM strmIn = NULL;
352 try
353 {
354 strmIn = item->getStream();
355 }
356 catch (RTCError &e)
357 {
358 strmIn = NULL;
359 RTStrmPutStr(m_StrmTxt, e.what());
360 }
361
362 int rc = VINF_SUCCESS;
363
364 if (strmIn)
365 {
366 char buf[64*1024];
367 size_t cbRead, cbWritten;
368 cbRead = cbWritten = 0;
369 while (RT_SUCCESS(rc = RTStrmReadEx(strmIn, buf, sizeof(buf), &cbRead)) && cbRead)
370 {
371 rc = RTStrmWriteEx(m_StrmTxt, buf, cbRead, &cbWritten);
372 if (RT_FAILURE(rc) || cbRead != cbWritten)
373 throw RTCError(com::Utf8StrFmt("Write failure (rc=%d, cbRead=%lu, cbWritten=%lu)\n",
374 rc, cbRead, cbWritten));
375 }
376 }
377
378 handleRtError(RTStrmPutCh(m_StrmTxt, '\n'), "Write failure");
379}
380
381
382BugReportTarGzip::BugReportTarGzip(const char *pszFileName)
383 : BugReport(pszFileName), m_hTar(NIL_RTTAR), m_hTarFile(NIL_RTTARFILE)
384{
385 VfsIoStreamHandle hVfsOut;
386 handleRtError(RTVfsIoStrmOpenNormal(pszFileName, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE,
387 hVfsOut.getPtr()),
388 "Failed to create output file '%s'", pszFileName);
389 handleRtError(RTZipGzipCompressIoStream(hVfsOut.get(), 0, 6, m_hVfsGzip.getPtr()),
390 "Failed to create compressed stream for '%s'", pszFileName);
391
392 handleRtError(RTPathTemp(m_szTarName, RTPATH_MAX),
393 "Failed to obtain path to temporary folder");
394 handleRtError(RTPathAppend(m_szTarName, RTPATH_MAX, "BugRepXXXXX.tar"),
395 "Failed to append path");
396 handleRtError(RTFileCreateTemp(m_szTarName, 0600),
397 "Failed to create temporary file '%s'", m_szTarName);
398 handleRtError(RTFileDelete(m_szTarName),
399 "Failed to delete temporary file '%s'", m_szTarName);
400 handleRtError(RTTarOpen(&m_hTar, m_szTarName, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_ALL),
401 "Failed to create TAR file '%s'", m_szTarName);
402
403}
404
405BugReportTarGzip::~BugReportTarGzip()
406{
407 if (m_hTarFile != NIL_RTTARFILE)
408 RTTarFileClose(m_hTarFile);
409 if (m_hTar != NIL_RTTAR)
410 RTTarClose(m_hTar);
411}
412
413void BugReportTarGzip::processItem(BugReportItem* item)
414{
415 /*
416 * @todo Our TAR implementation does not support names larger than 100 characters.
417 * We truncate the title to make sure it will fit into 100-character field of TAR header.
418 */
419 RTCString strTarFile = RTCString(item->getTitle()).substr(0, RTStrNLen(item->getTitle(), 99));
420 handleRtError(RTTarFileOpen(m_hTar, &m_hTarFile, strTarFile.c_str(),
421 RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE),
422 "Failed to open '%s' in TAR", strTarFile.c_str());
423
424 PRTSTREAM strmIn = NULL;
425 try
426 {
427 strmIn = item->getStream();
428 }
429 catch (RTCError &e)
430 {
431 strmIn = NULL;
432 handleRtError(RTTarFileWriteAt(m_hTarFile, 0, e.what(), RTStrNLen(e.what(), 1024), NULL),
433 "Failed to write %u bytes to TAR", RTStrNLen(e.what(), 1024));
434 }
435
436 int rc = VINF_SUCCESS;
437
438 if (strmIn)
439 {
440 char buf[64*1024];
441 size_t cbRead = 0;
442 for (uint64_t offset = 0;
443 RT_SUCCESS(rc = RTStrmReadEx(strmIn, buf, sizeof(buf), &cbRead)) && cbRead;
444 offset += cbRead)
445 {
446 handleRtError(RTTarFileWriteAt(m_hTarFile, offset, buf, cbRead, NULL),
447 "Failed to write %u bytes to TAR", cbRead);
448 }
449 }
450
451 if (m_hTarFile)
452 {
453 handleRtError(RTTarFileClose(m_hTarFile), "Failed to close '%s' in TAR", strTarFile.c_str());
454 m_hTarFile = NIL_RTTARFILE;
455 }
456}
457
458void BugReportTarGzip::complete(void)
459{
460 if (m_hTarFile != NIL_RTTARFILE)
461 {
462 RTTarFileClose(m_hTarFile);
463 m_hTarFile = NIL_RTTARFILE;
464 }
465 if (m_hTar != NIL_RTTAR)
466 {
467 RTTarClose(m_hTar);
468 m_hTar = NIL_RTTAR;
469 }
470
471 VfsIoStreamHandle hVfsIn;
472 handleRtError(RTVfsIoStrmOpenNormal(m_szTarName, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
473 hVfsIn.getPtr()),
474 "Failed to open TAR file '%s'", m_szTarName);
475
476 int rc;
477 char buf[_64K];
478 size_t cbRead = 0;
479 while (RT_SUCCESS(rc = RTVfsIoStrmRead(hVfsIn.get(), buf, sizeof(buf), true, &cbRead)) && cbRead)
480 handleRtError(RTVfsIoStrmWrite(m_hVfsGzip.get(), buf, cbRead, true, NULL),
481 "Failed to write into compressed stream");
482 handleRtError(rc, "Failed to read from TAR stream");
483 handleRtError(RTVfsIoStrmFlush(m_hVfsGzip.get()), "Failed to flush output stream");
484 m_hVfsGzip.release();
485}
486
487
488/* Implementation - Main */
489
490void createBugReport(BugReport* report, const char *pszHome, MachineInfoList& machines)
491{
492 /* Collect all log files from VBoxSVC */
493 VBRDir HomeDir(PathJoin(pszHome, "VBoxSVC.log*"));
494 const char *pcszSvcLogFile = HomeDir.next();
495 while (pcszSvcLogFile)
496 {
497 report->addItem(new BugReportFile(PathJoin(pszHome, pcszSvcLogFile), pcszSvcLogFile));
498 pcszSvcLogFile = HomeDir.next();
499 }
500
501 report->addItem(new BugReportFile(PathJoin(pszHome, "VirtualBox.xml"), "VirtualBox.xml"));
502 report->addItem(new BugReportCommand("HostUsbDevices", g_pszVBoxManage, "list", "usbhost", NULL));
503 report->addItem(new BugReportCommand("HostUsbFilters", g_pszVBoxManage, "list", "usbfilters", NULL));
504 for (MachineInfoList::iterator it = machines.begin(); it != machines.end(); ++it)
505 {
506 VBRDir VmDir(PathJoin((*it)->getLogPath(), "VBox.log*"));
507 const char *pcszVmLogFile = HomeDir.next();
508 while (pcszVmLogFile)
509 {
510 report->addItem(new BugReportFile(PathJoin((*it)->getLogPath(), pcszVmLogFile),
511 PathJoin((*it)->getName(), pcszVmLogFile)));
512 pcszVmLogFile = HomeDir.next();
513 }
514 report->addItem(new BugReportFile((*it)->getSettingsFile(),
515 PathJoin((*it)->getName(), RTPathFilename((*it)->getSettingsFile()))));
516 report->addItem(new BugReportCommand(PathJoin((*it)->getName(), "GuestProperties"),
517 g_pszVBoxManage, "guestproperty", "enumerate",
518 (*it)->getName(), NULL));
519 }
520
521 createBugReportOsSpecific(report, pszHome);
522}
523
524void addMachine(MachineInfoList& list, ComPtr<IMachine> machine)
525{
526 com::Bstr name, logFolder, settingsFile;
527 handleComError(machine->COMGETTER(Name)(name.asOutParam()),
528 "Failed to get VM name");
529 handleComError(machine->COMGETTER(LogFolder)(logFolder.asOutParam()),
530 "Failed to get VM log folder");
531 handleComError(machine->COMGETTER(SettingsFilePath)(settingsFile.asOutParam()),
532 "Failed to get VM settings file path");
533 list.push_back(new MachineInfo(com::Utf8Str(name).c_str(),
534 com::Utf8Str(logFolder).c_str(),
535 com::Utf8Str(settingsFile).c_str()));
536}
537
538
539static void printHeader(void)
540{
541 RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " Bug Report Tool " VBOX_VERSION_STRING "\n"
542 "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n"
543 "All rights reserved.\n\n");
544}
545
546int main(int argc, char *argv[])
547{
548 /*
549 * Initialize the VBox runtime without loading
550 * the support driver.
551 */
552 RTR3InitExe(argc, &argv, 0);
553
554 bool fAllMachines = false;
555 bool fTextOutput = false;
556 const char *pszOutputFile = NULL;
557 std::list<const char *> nameList;
558 RTGETOPTUNION ValueUnion;
559 RTGETOPTSTATE GetState;
560 int ret = RTGetOptInit(&GetState, argc, argv,
561 g_aOptions, RT_ELEMENTS(g_aOptions),
562 1 /* First */, 0 /*fFlags*/);
563 if (RT_FAILURE(ret))
564 return ret;
565 int ch;
566 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
567 {
568 switch(ch)
569 {
570 case 'h':
571 printHeader();
572 RTStrmPrintf(g_pStdErr, g_szUsage, argv[0]);
573 return 0;
574 case 'A':
575 fAllMachines = true;
576 break;
577 case 'o':
578 pszOutputFile = ValueUnion.psz;
579 break;
580 case 't':
581 fTextOutput = true;
582 break;
583 case 'V':
584 RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
585 return 0;
586 case VINF_GETOPT_NOT_OPTION:
587 nameList.push_back(ValueUnion.psz);
588 break;
589 default:
590 return RTGetOptPrintError(ch, &ValueUnion);
591 }
592 }
593
594 printHeader();
595
596 HRESULT hr = S_OK;
597 char homeDir[RTPATH_MAX];
598 com::GetVBoxUserHomeDirectory(homeDir, sizeof(homeDir));
599
600 try
601 {
602 /* Figure out the full path to VBoxManage */
603 char szVBoxBin[RTPATH_MAX];
604 if (!RTProcGetExecutablePath(szVBoxBin, sizeof(szVBoxBin)))
605 throw RTCError("RTProcGetExecutablePath failed\n");
606 RTPathStripFilename(szVBoxBin);
607 g_pszVBoxManage = RTPathJoinA(szVBoxBin, VBOXMANAGE);
608 if (!g_pszVBoxManage)
609 throw RTCError("Out of memory\n");
610
611 handleComError(com::Initialize(), "Failed to initialize COM");
612
613 MachineInfoList list;
614
615 do
616 {
617 ComPtr<IVirtualBoxClient> virtualBoxClient;
618 ComPtr<IVirtualBox> virtualBox;
619 ComPtr<ISession> session;
620
621 hr = virtualBoxClient.createLocalObject(CLSID_VirtualBoxClient);
622 if (SUCCEEDED(hr))
623 hr = virtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
624 else
625 hr = virtualBox.createLocalObject(CLSID_VirtualBox);
626 if (FAILED(hr))
627 RTStrmPrintf(g_pStdErr, "WARNING: Failed to create the VirtualBox object (hr=0x%x)\n", hr);
628 else
629 {
630 hr = session.createInprocObject(CLSID_Session);
631 if (FAILED(hr))
632 RTStrmPrintf(g_pStdErr, "WARNING: Failed to create a session object (hr=0x%x)\n", hr);
633 }
634
635 if (SUCCEEDED(hr))
636 {
637 if (fAllMachines)
638 {
639 com::SafeIfaceArray<IMachine> machines;
640 hr = virtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(machines));
641 if (SUCCEEDED(hr))
642 {
643 for (size_t i = 0; i < machines.size(); ++i)
644 {
645 if (machines[i])
646 addMachine(list, machines[i]);
647 }
648 }
649 }
650 else
651 {
652 for ( std::list<const char *>::iterator it = nameList.begin(); it != nameList.end(); ++it)
653 {
654 ComPtr<IMachine> machine;
655 handleComError(virtualBox->FindMachine(com::Bstr(*it).raw(), machine.asOutParam()),
656 "No such machine '%s'", *it);
657 addMachine(list, machine);
658 }
659 }
660 }
661
662 }
663 while(0);
664
665 RTTIMESPEC TimeSpec;
666 RTTIME Time;
667 RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
668 RTCStringFmt strOutFile("%04d-%02d-%02d-%02d-%02d-%02d-bugreport.%s",
669 Time.i32Year, Time.u8Month, Time.u8MonthDay,
670 Time.u8Hour, Time.u8Minute, Time.u8Second,
671 fTextOutput ? "txt" : "tgz");
672 RTCString strFallbackOutFile;
673 if (!pszOutputFile)
674 {
675 RTFILE tmp;
676 pszOutputFile = strOutFile.c_str();
677 int rc = RTFileOpen(&tmp, pszOutputFile, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
678 if (rc == VERR_ACCESS_DENIED)
679 {
680 char szUserHome[RTPATH_MAX];
681 handleRtError(RTPathUserHome(szUserHome, sizeof(szUserHome)), "Failed to obtain home directory");
682 strFallbackOutFile.printf("%s/%s", szUserHome, strOutFile.c_str());
683 pszOutputFile = strFallbackOutFile.c_str();
684 }
685 else if (RT_SUCCESS(rc))
686 {
687 RTFileClose(tmp);
688 RTFileDelete(pszOutputFile);
689 }
690 }
691 BugReport *pReport;
692 if (fTextOutput)
693 pReport = new BugReportText(pszOutputFile);
694 else
695 pReport = new BugReportTarGzip(pszOutputFile);
696 createBugReport(pReport, homeDir, list);
697 pReport->process();
698 pReport->complete();
699 RTPrintf("Report was written to '%s'\n", pszOutputFile);
700 delete pReport;
701 }
702 catch (RTCError &e)
703 {
704 RTStrmPrintf(g_pStdErr, "ERROR: %s\n", e.what());
705 }
706
707 com::Shutdown();
708
709 if (g_pszVBoxManage)
710 RTStrFree(g_pszVBoxManage);
711
712 return SUCCEEDED(hr) ? 0 : 1;
713}
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