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