1 | /* $Id: VBoxBugReport.h 76582 2019-01-01 06:25:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxBugReport - VirtualBox command-line diagnostics tool, internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 | #ifndef VBOX_INCLUDED_SRC_VBoxBugReport_VBoxBugReport_h
|
---|
19 | #define VBOX_INCLUDED_SRC_VBoxBugReport_VBoxBugReport_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * Introduction.
|
---|
26 | *
|
---|
27 | * In the most general sense a bug report is a collection of data obtained from
|
---|
28 | * the user's host system. It may include files common for all VMs, like the
|
---|
29 | * VBoxSVC.log file, as well as files related to particular machines. It may
|
---|
30 | * also contain the output of commands executed on the host, as well as data
|
---|
31 | * collected via OS APIs.
|
---|
32 | */
|
---|
33 |
|
---|
34 | /* @todo not sure if using a separate namespace would be beneficial */
|
---|
35 |
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/stream.h>
|
---|
38 | #include <iprt/tar.h>
|
---|
39 | #include <iprt/vfs.h>
|
---|
40 | #include <iprt/cpp/list.h>
|
---|
41 |
|
---|
42 | #ifdef RT_OS_WINDOWS
|
---|
43 | #define VBOXMANAGE "VBoxManage.exe"
|
---|
44 | #else /* !RT_OS_WINDOWS */
|
---|
45 | #define VBOXMANAGE "VBoxManage"
|
---|
46 | #endif /* !RT_OS_WINDOWS */
|
---|
47 |
|
---|
48 | /* Base */
|
---|
49 |
|
---|
50 | inline void handleRtError(int rc, const char *pszMsgFmt, ...)
|
---|
51 | {
|
---|
52 | if (RT_FAILURE(rc))
|
---|
53 | {
|
---|
54 | va_list va;
|
---|
55 | va_start(va, pszMsgFmt);
|
---|
56 | RTCString msgArgs(pszMsgFmt, va);
|
---|
57 | va_end(va);
|
---|
58 | RTCStringFmt msg("%s. %s (%d)\n", msgArgs.c_str(), RTErrGetFull(rc), rc);
|
---|
59 | throw RTCError(msg.c_str());
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | inline void handleComError(HRESULT hr, const char *pszMsgFmt, ...)
|
---|
64 | {
|
---|
65 | if (FAILED(hr))
|
---|
66 | {
|
---|
67 | va_list va;
|
---|
68 | va_start(va, pszMsgFmt);
|
---|
69 | RTCString msgArgs(pszMsgFmt, va);
|
---|
70 | va_end(va);
|
---|
71 | RTCStringFmt msg("%s (hr=0x%x)\n", msgArgs.c_str(), hr);
|
---|
72 | throw RTCError(msg.c_str());
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * An auxiliary class to facilitate in-place path joins.
|
---|
78 | */
|
---|
79 | class PathJoin
|
---|
80 | {
|
---|
81 | public:
|
---|
82 | PathJoin(const char *folder, const char *file) { m_path = RTPathJoinA(folder, file); }
|
---|
83 | ~PathJoin() { RTStrFree(m_path); };
|
---|
84 | operator char*() const { return m_path; };
|
---|
85 | private:
|
---|
86 | char *m_path;
|
---|
87 | };
|
---|
88 |
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * An abstract class serving as the root of the bug report filter tree.
|
---|
92 | * A child provides an implementation of the 'apply' method. A child
|
---|
93 | * should modify the input buffer (provided via pvSource) in place, or
|
---|
94 | * allocate a new buffer via 'allocateBuffer'. Allocated buffers are
|
---|
95 | * released automatically when another buffer is allocated, which means
|
---|
96 | * that NEXT CALL TO 'APPLY' INVALIDATES BUFFERS RETURNED IN PREVIOUS
|
---|
97 | * CALLS!
|
---|
98 | */
|
---|
99 | class BugReportFilter
|
---|
100 | {
|
---|
101 | public:
|
---|
102 | BugReportFilter();
|
---|
103 | virtual ~BugReportFilter();
|
---|
104 | virtual void *apply(void *pvSource, size_t *pcbInOut) = 0;
|
---|
105 | protected:
|
---|
106 | void *allocateBuffer(size_t cbNeeded);
|
---|
107 | private:
|
---|
108 | void *m_pvBuffer;
|
---|
109 | size_t m_cbBuffer;
|
---|
110 | };
|
---|
111 |
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * An abstract class serving as the root of the bug report item tree.
|
---|
115 | */
|
---|
116 | class BugReportItem
|
---|
117 | {
|
---|
118 | public:
|
---|
119 | BugReportItem(const char *pszTitle);
|
---|
120 | virtual ~BugReportItem();
|
---|
121 | virtual const char *getTitle(void);
|
---|
122 | virtual PRTSTREAM getStream(void) = 0;
|
---|
123 | void addFilter(BugReportFilter *filter);
|
---|
124 | void *applyFilter(void *pvSource, size_t *pcbInOut);
|
---|
125 | private:
|
---|
126 | char *m_pszTitle;
|
---|
127 | BugReportFilter *m_filter;
|
---|
128 | };
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * An abstract class to serve as a base class for all report types.
|
---|
132 | */
|
---|
133 | class BugReport
|
---|
134 | {
|
---|
135 | public:
|
---|
136 | BugReport(const char *pszFileName);
|
---|
137 | virtual ~BugReport();
|
---|
138 |
|
---|
139 | void addItem(BugReportItem* item, BugReportFilter *filter = 0);
|
---|
140 | int getItemCount(void);
|
---|
141 | void process();
|
---|
142 | void *applyFilters(BugReportItem* item, void *pvSource, size_t *pcbInOut);
|
---|
143 |
|
---|
144 | virtual void processItem(BugReportItem* item) = 0;
|
---|
145 | virtual void complete(void) = 0;
|
---|
146 |
|
---|
147 | protected:
|
---|
148 | char *m_pszFileName;
|
---|
149 | RTCList<BugReportItem*> m_Items;
|
---|
150 | };
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * An auxiliary class providing formatted output into a temporary file for item
|
---|
154 | * classes that obtain data via host OS APIs.
|
---|
155 | */
|
---|
156 | class BugReportStream : public BugReportItem
|
---|
157 | {
|
---|
158 | public:
|
---|
159 | BugReportStream(const char *pszTitle);
|
---|
160 | virtual ~BugReportStream();
|
---|
161 | virtual PRTSTREAM getStream(void);
|
---|
162 | protected:
|
---|
163 | int printf(const char *pszFmt, ...);
|
---|
164 | int putStr(const char *pszString);
|
---|
165 | private:
|
---|
166 | PRTSTREAM m_Strm;
|
---|
167 | char m_szFileName[RTPATH_MAX];
|
---|
168 | };
|
---|
169 |
|
---|
170 |
|
---|
171 | /* Generic */
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * This class reports everything into a single text file.
|
---|
175 | */
|
---|
176 | class BugReportText : public BugReport
|
---|
177 | {
|
---|
178 | public:
|
---|
179 | BugReportText(const char *pszFileName);
|
---|
180 | virtual ~BugReportText();
|
---|
181 | virtual void processItem(BugReportItem* item);
|
---|
182 | virtual void complete(void) {};
|
---|
183 | private:
|
---|
184 | PRTSTREAM m_StrmTxt;
|
---|
185 | };
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * This class reports items as individual files archived into a single compressed TAR file.
|
---|
189 | */
|
---|
190 | class BugReportTarGzip : public BugReport
|
---|
191 | {
|
---|
192 | public:
|
---|
193 | BugReportTarGzip(const char *pszFileName);
|
---|
194 | virtual ~BugReportTarGzip();
|
---|
195 | virtual void processItem(BugReportItem* item);
|
---|
196 | virtual void complete(void);
|
---|
197 | private:
|
---|
198 | /*
|
---|
199 | * Helper class to release handles going out of scope.
|
---|
200 | */
|
---|
201 | class VfsIoStreamHandle
|
---|
202 | {
|
---|
203 | public:
|
---|
204 | VfsIoStreamHandle() : m_hVfsStream(NIL_RTVFSIOSTREAM) {};
|
---|
205 | ~VfsIoStreamHandle() { release(); }
|
---|
206 | PRTVFSIOSTREAM getPtr(void) { return &m_hVfsStream; };
|
---|
207 | RTVFSIOSTREAM get(void) { return m_hVfsStream; };
|
---|
208 | void release(void)
|
---|
209 | {
|
---|
210 | if (m_hVfsStream != NIL_RTVFSIOSTREAM)
|
---|
211 | RTVfsIoStrmRelease(m_hVfsStream);
|
---|
212 | m_hVfsStream = NIL_RTVFSIOSTREAM;
|
---|
213 | };
|
---|
214 | private:
|
---|
215 | RTVFSIOSTREAM m_hVfsStream;
|
---|
216 | };
|
---|
217 |
|
---|
218 | VfsIoStreamHandle m_hVfsGzip;
|
---|
219 |
|
---|
220 | RTTAR m_hTar;
|
---|
221 | RTTARFILE m_hTarFile;
|
---|
222 | char m_szTarName[RTPATH_MAX];
|
---|
223 | };
|
---|
224 |
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * BugReportFile adds a file as an item to a report.
|
---|
228 | */
|
---|
229 | class BugReportFile : public BugReportItem
|
---|
230 | {
|
---|
231 | public:
|
---|
232 | BugReportFile(const char *pszPath, const char *pcszName);
|
---|
233 | virtual ~BugReportFile();
|
---|
234 | virtual PRTSTREAM getStream(void);
|
---|
235 |
|
---|
236 | private:
|
---|
237 | char *m_pszPath;
|
---|
238 | PRTSTREAM m_Strm;
|
---|
239 | };
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * A base class for item classes that collect CLI output.
|
---|
243 | */
|
---|
244 | class BugReportCommand : public BugReportItem
|
---|
245 | {
|
---|
246 | public:
|
---|
247 | BugReportCommand(const char *pszTitle, const char *pszExec, ...);
|
---|
248 | virtual ~BugReportCommand();
|
---|
249 | virtual PRTSTREAM getStream(void);
|
---|
250 | private:
|
---|
251 | PRTSTREAM m_Strm;
|
---|
252 | char m_szFileName[RTPATH_MAX];
|
---|
253 | char *m_papszArgs[32];
|
---|
254 | };
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * A base class for item classes that provide temp output file to a command.
|
---|
258 | */
|
---|
259 | class BugReportCommandTemp : public BugReportItem
|
---|
260 | {
|
---|
261 | public:
|
---|
262 | BugReportCommandTemp(const char *pszTitle, const char *pszExec, ...);
|
---|
263 | virtual ~BugReportCommandTemp();
|
---|
264 | virtual PRTSTREAM getStream(void);
|
---|
265 | private:
|
---|
266 | PRTSTREAM m_Strm;
|
---|
267 | char m_szFileName[RTPATH_MAX];
|
---|
268 | char m_szErrFileName[RTPATH_MAX];
|
---|
269 | char *m_papszArgs[32];
|
---|
270 | };
|
---|
271 |
|
---|
272 | /* Platform-specific */
|
---|
273 |
|
---|
274 | void createBugReportOsSpecific(BugReport* report, const char *pszHome);
|
---|
275 |
|
---|
276 | #endif /* !VBOX_INCLUDED_SRC_VBoxBugReport_VBoxBugReport_h */
|
---|