]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #ifdef ENABLE_DECODE | |
21 | #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */ | |
22 | #endif | |
23 | ||
24 | #include <cstdint> | |
25 | #include <fstream> | |
26 | #include <getopt.h> | |
27 | ||
28 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
29 | ||
30 | #include <QCheckBox> | |
31 | #include <QDebug> | |
32 | #include <QFile> | |
33 | #include <QFileInfo> | |
34 | #include <QMessageBox> | |
35 | #include <QSettings> | |
36 | #include <QTextStream> | |
37 | ||
38 | #ifdef ENABLE_SIGNALS | |
39 | #include "signalhandler.hpp" | |
40 | #endif | |
41 | ||
42 | #ifdef ENABLE_STACKTRACE | |
43 | #include <signal.h> | |
44 | #include <boost/stacktrace.hpp> | |
45 | #include <QStandardPaths> | |
46 | #endif | |
47 | ||
48 | #include "pv/application.hpp" | |
49 | #include "pv/devicemanager.hpp" | |
50 | #include "pv/globalsettings.hpp" | |
51 | #include "pv/logging.hpp" | |
52 | #include "pv/mainwindow.hpp" | |
53 | #include "pv/session.hpp" | |
54 | ||
55 | #ifdef ANDROID | |
56 | #include <libsigrokandroidutils/libsigrokandroidutils.h> | |
57 | #include "android/assetreader.hpp" | |
58 | #include "android/loghandler.hpp" | |
59 | #endif | |
60 | ||
61 | #include "config.h" | |
62 | ||
63 | #ifdef _WIN32 | |
64 | #include <QtPlugin> | |
65 | Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) | |
66 | Q_IMPORT_PLUGIN(QSvgPlugin) | |
67 | #endif | |
68 | ||
69 | using std::exception; | |
70 | using std::ifstream; | |
71 | using std::ofstream; | |
72 | using std::shared_ptr; | |
73 | using std::string; | |
74 | ||
75 | #if ENABLE_STACKTRACE | |
76 | QString stacktrace_filename; | |
77 | ||
78 | void signal_handler(int signum) | |
79 | { | |
80 | ::signal(signum, SIG_DFL); | |
81 | boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data()); | |
82 | ::raise(SIGABRT); | |
83 | } | |
84 | ||
85 | void process_stacktrace(QString temp_path) | |
86 | { | |
87 | const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt"; | |
88 | ||
89 | ifstream ifs(stacktrace_filename.toLocal8Bit().data()); | |
90 | ofstream ofs(stacktrace_outfile.toLocal8Bit().data(), | |
91 | ofstream::out | ofstream::trunc); | |
92 | ||
93 | boost::stacktrace::stacktrace st = | |
94 | boost::stacktrace::stacktrace::from_dump(ifs); | |
95 | ofs << st; | |
96 | ||
97 | ofs.close(); | |
98 | ifs.close(); | |
99 | ||
100 | QFile f(stacktrace_outfile); | |
101 | f.open(QFile::ReadOnly | QFile::Text); | |
102 | QTextStream fs(&f); | |
103 | QString stacktrace = fs.readAll(); | |
104 | stacktrace = stacktrace.trimmed().replace('\n', "<br />"); | |
105 | ||
106 | qDebug() << QObject::tr("Stack trace of previous crash:"); | |
107 | qDebug() << "---------------------------------------------------------"; | |
108 | // Note: qDebug() prints quotation marks for QString output, so we feed it char* | |
109 | qDebug() << stacktrace.toLocal8Bit().data(); | |
110 | qDebug() << "---------------------------------------------------------"; | |
111 | ||
112 | f.close(); | |
113 | ||
114 | // Remove stack trace so we don't process it again the next time we run | |
115 | QFile::remove(stacktrace_filename.toLocal8Bit().data()); | |
116 | ||
117 | // Show notification dialog if permitted | |
118 | pv::GlobalSettings settings; | |
119 | if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) { | |
120 | QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again")); | |
121 | ||
122 | QMessageBox msgbox; | |
123 | msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \ | |
124 | "A human-readable form has been saved to disk and was written to " \ | |
125 | "the log. You may access it from the settings dialog.").arg(PV_TITLE)); | |
126 | msgbox.setIcon(QMessageBox::Icon::Information); | |
127 | msgbox.addButton(QMessageBox::Ok); | |
128 | msgbox.setCheckBox(cb); | |
129 | ||
130 | QObject::connect(cb, &QCheckBox::stateChanged, [](int state){ | |
131 | pv::GlobalSettings settings; | |
132 | settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace, | |
133 | !state); }); | |
134 | ||
135 | msgbox.exec(); | |
136 | } | |
137 | } | |
138 | #endif | |
139 | ||
140 | void usage() | |
141 | { | |
142 | fprintf(stdout, | |
143 | "Usage:\n" | |
144 | " %s [OPTIONS] [FILE]\n" | |
145 | "\n" | |
146 | "Help Options:\n" | |
147 | " -h, -?, --help Show help option\n" | |
148 | "\n" | |
149 | "Application Options:\n" | |
150 | " -V, --version Show release version\n" | |
151 | " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n" | |
152 | " -d, --driver Specify the device driver to use\n" | |
153 | " -D, --no-scan Don't auto-scan for devices, use -d spec only\n" | |
154 | " -i, --input-file Load input from file\n" | |
155 | " -I, --input-format Input format\n" | |
156 | " -c, --clean Don't restore previous sessions on startup\n" | |
157 | " -s, --log-to-stdout Don't use logging, output to stdout instead\n" | |
158 | "\n", PV_BIN_NAME); | |
159 | } | |
160 | ||
161 | int main(int argc, char *argv[]) | |
162 | { | |
163 | int ret = 0; | |
164 | shared_ptr<sigrok::Context> context; | |
165 | string open_file, open_file_format, driver; | |
166 | bool restore_sessions = true; | |
167 | bool do_scan = true; | |
168 | bool do_logging = true; | |
169 | ||
170 | Application a(argc, argv); | |
171 | ||
172 | #ifdef ANDROID | |
173 | srau_init_environment(); | |
174 | pv::AndroidLogHandler::install_callbacks(); | |
175 | pv::AndroidAssetReader asset_reader; | |
176 | #endif | |
177 | ||
178 | // Parse arguments | |
179 | while (true) { | |
180 | static const struct option long_options[] = { | |
181 | {"help", no_argument, nullptr, 'h'}, | |
182 | {"version", no_argument, nullptr, 'V'}, | |
183 | {"loglevel", required_argument, nullptr, 'l'}, | |
184 | {"driver", required_argument, nullptr, 'd'}, | |
185 | {"no-scan", no_argument, nullptr, 'D'}, | |
186 | {"input-file", required_argument, nullptr, 'i'}, | |
187 | {"input-format", required_argument, nullptr, 'I'}, | |
188 | {"clean", no_argument, nullptr, 'c'}, | |
189 | {"log-to-stdout", no_argument, nullptr, 's'}, | |
190 | {nullptr, 0, nullptr, 0} | |
191 | }; | |
192 | ||
193 | const int c = getopt_long(argc, argv, | |
194 | "h?VDcsl:d:i:I:", long_options, nullptr); | |
195 | if (c == -1) | |
196 | break; | |
197 | ||
198 | switch (c) { | |
199 | case 'h': | |
200 | case '?': | |
201 | usage(); | |
202 | return 0; | |
203 | ||
204 | case 'V': | |
205 | // Print version info | |
206 | fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING); | |
207 | return 0; | |
208 | ||
209 | case 'l': | |
210 | { | |
211 | const int loglevel = atoi(optarg); | |
212 | if (loglevel < 0 || loglevel > 5) { | |
213 | qDebug() << "ERROR: invalid log level spec."; | |
214 | break; | |
215 | } | |
216 | context->set_log_level(sigrok::LogLevel::get(loglevel)); | |
217 | ||
218 | #ifdef ENABLE_DECODE | |
219 | srd_log_loglevel_set(loglevel); | |
220 | #endif | |
221 | ||
222 | if (loglevel >= 5) { | |
223 | const QSettings settings; | |
224 | qDebug() << "Settings:" << settings.fileName() | |
225 | << "format" << settings.format(); | |
226 | } | |
227 | break; | |
228 | } | |
229 | ||
230 | case 'd': | |
231 | driver = optarg; | |
232 | break; | |
233 | ||
234 | case 'D': | |
235 | do_scan = false; | |
236 | break; | |
237 | ||
238 | case 'i': | |
239 | open_file = optarg; | |
240 | break; | |
241 | ||
242 | case 'I': | |
243 | open_file_format = optarg; | |
244 | break; | |
245 | ||
246 | case 'c': | |
247 | restore_sessions = false; | |
248 | break; | |
249 | ||
250 | case 's': | |
251 | do_logging = false; | |
252 | break; | |
253 | } | |
254 | } | |
255 | ||
256 | if (argc - optind > 1) { | |
257 | fprintf(stderr, "Only one file can be opened.\n"); | |
258 | return 1; | |
259 | } | |
260 | ||
261 | if (argc - optind == 1) | |
262 | open_file = argv[argc - 1]; | |
263 | ||
264 | // Prepare the global settings since logging needs them early on | |
265 | pv::GlobalSettings settings; | |
266 | settings.set_defaults_where_needed(); | |
267 | ||
268 | if (do_logging) | |
269 | pv::logging.init(); | |
270 | ||
271 | // Initialise libsigrok | |
272 | context = sigrok::Context::create(); | |
273 | pv::Session::sr_context = context; | |
274 | ||
275 | #if ENABLE_STACKTRACE | |
276 | QString temp_path = QStandardPaths::standardLocations( | |
277 | QStandardPaths::TempLocation).at(0); | |
278 | stacktrace_filename = temp_path + "/pv_stacktrace.dmp"; | |
279 | qDebug() << "Stack trace file is" << stacktrace_filename; | |
280 | ||
281 | ::signal(SIGSEGV, &signal_handler); | |
282 | ::signal(SIGABRT, &signal_handler); | |
283 | ||
284 | if (QFileInfo::exists(stacktrace_filename)) | |
285 | process_stacktrace(temp_path); | |
286 | #endif | |
287 | ||
288 | #ifdef ANDROID | |
289 | context->set_resource_reader(&asset_reader); | |
290 | #endif | |
291 | do { | |
292 | ||
293 | #ifdef ENABLE_DECODE | |
294 | // Initialise libsigrokdecode | |
295 | if (srd_init(nullptr) != SRD_OK) { | |
296 | qDebug() << "ERROR: libsigrokdecode init failed."; | |
297 | break; | |
298 | } | |
299 | ||
300 | // Load the protocol decoders | |
301 | srd_decoder_load_all(); | |
302 | #endif | |
303 | ||
304 | #ifndef ENABLE_STACKTRACE | |
305 | try { | |
306 | #endif | |
307 | ||
308 | // Create the device manager, initialise the drivers | |
309 | pv::DeviceManager device_manager(context, driver, do_scan); | |
310 | ||
311 | // Initialise the main window | |
312 | pv::MainWindow w(device_manager); | |
313 | w.show(); | |
314 | ||
315 | if (restore_sessions) | |
316 | w.restore_sessions(); | |
317 | ||
318 | if (!open_file.empty()) | |
319 | w.add_session_with_file(open_file, open_file_format); | |
320 | else | |
321 | w.add_default_session(); | |
322 | ||
323 | #ifdef ENABLE_SIGNALS | |
324 | if (SignalHandler::prepare_signals()) { | |
325 | SignalHandler *const handler = new SignalHandler(&w); | |
326 | QObject::connect(handler, SIGNAL(int_received()), | |
327 | &w, SLOT(close())); | |
328 | QObject::connect(handler, SIGNAL(term_received()), | |
329 | &w, SLOT(close())); | |
330 | } else | |
331 | qWarning() << "Could not prepare signal handler."; | |
332 | #endif | |
333 | ||
334 | // Run the application | |
335 | ret = a.exec(); | |
336 | ||
337 | #ifndef ENABLE_STACKTRACE | |
338 | } catch (exception& e) { | |
339 | qDebug() << "Exception:" << e.what(); | |
340 | } | |
341 | #endif | |
342 | ||
343 | #ifdef ENABLE_DECODE | |
344 | // Destroy libsigrokdecode | |
345 | srd_exit(); | |
346 | #endif | |
347 | ||
348 | } while (false); | |
349 | ||
350 | return ret; | |
351 | } |