]>
Commit | Line | Data |
---|---|---|
d7bed479 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
d7bed479 JH |
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
d7bed479 JH |
18 | */ |
19 | ||
269528f5 | 20 | #ifdef ENABLE_DECODE |
aaabd61b | 21 | #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
269528f5 | 22 | #endif |
d52d8455 | 23 | |
15a69e54 | 24 | #include <cstdint> |
c5d6200c | 25 | #include <fstream> |
f7951df4 JH |
26 | #include <getopt.h> |
27 | ||
c5d6200c SA |
28 | #include <libsigrokcxx/libsigrokcxx.hpp> |
29 | ||
30 | #include <QCheckBox> | |
664fca5c | 31 | #include <QDebug> |
c5d6200c SA |
32 | #include <QFile> |
33 | #include <QFileInfo> | |
34 | #include <QMessageBox> | |
0bd39c0c | 35 | #include <QSettings> |
c5d6200c | 36 | #include <QTextStream> |
075fb499 | 37 | |
140181a4 | 38 | #ifdef ENABLE_SIGNALS |
2acdb232 | 39 | #include "signalhandler.hpp" |
140181a4 JH |
40 | #endif |
41 | ||
c1a688de SA |
42 | #ifdef ENABLE_STACKTRACE |
43 | #include <signal.h> | |
44 | #include <boost/stacktrace.hpp> | |
45 | #include <QStandardPaths> | |
46 | #endif | |
47 | ||
2acdb232 JH |
48 | #include "pv/application.hpp" |
49 | #include "pv/devicemanager.hpp" | |
bcb4c327 SA |
50 | #include "pv/globalsettings.hpp" |
51 | #include "pv/logging.hpp" | |
2acdb232 | 52 | #include "pv/mainwindow.hpp" |
419ec4e1 | 53 | #include "pv/session.hpp" |
c1a688de | 54 | |
9137928c | 55 | #ifdef ANDROID |
aff51746 | 56 | #include <libsigrokandroidutils/libsigrokandroidutils.h> |
dddff2e7 | 57 | #include "android/assetreader.hpp" |
2acdb232 | 58 | #include "android/loghandler.hpp" |
9137928c | 59 | #endif |
d7bed479 | 60 | |
075fb499 AG |
61 | #include "config.h" |
62 | ||
11a04e2a | 63 | #ifdef _WIN32 |
11a04e2a | 64 | #include <QtPlugin> |
09f55d96 UH |
65 | Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) |
66 | Q_IMPORT_PLUGIN(QSvgPlugin) | |
11a04e2a UH |
67 | #endif |
68 | ||
6f925ba9 | 69 | using std::exception; |
c5d6200c SA |
70 | using std::ifstream; |
71 | using std::ofstream; | |
6f925ba9 UH |
72 | using std::shared_ptr; |
73 | using std::string; | |
74 | ||
c1a688de SA |
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 | } | |
c5d6200c SA |
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 | } | |
c1a688de SA |
138 | #endif |
139 | ||
d4384c6d JH |
140 | void usage() |
141 | { | |
9fd5bb6e | 142 | fprintf(stdout, |
d4384c6d | 143 | "Usage:\n" |
41e2ade4 | 144 | " %s [OPTIONS] [FILE]\n" |
d4384c6d JH |
145 | "\n" |
146 | "Help Options:\n" | |
d4384c6d | 147 | " -h, -?, --help Show help option\n" |
aa90e86b JH |
148 | "\n" |
149 | "Application Options:\n" | |
150 | " -V, --version Show release version\n" | |
151 | " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n" | |
58d8e4c6 | 152 | " -d, --driver Specify the device driver to use\n" |
1d6e35dc | 153 | " -D, --no-scan Don't auto-scan for devices, use -d spec only\n" |
e3c79b07 JH |
154 | " -i, --input-file Load input from file\n" |
155 | " -I, --input-format Input format\n" | |
156f06e9 | 156 | " -c, --clean Don't restore previous sessions on startup\n" |
c062efdc | 157 | " -s, --log-to-stdout Don't use logging, output to stdout instead\n" |
41e2ade4 | 158 | "\n", PV_BIN_NAME); |
d4384c6d JH |
159 | } |
160 | ||
d7bed479 JH |
161 | int main(int argc, char *argv[]) |
162 | { | |
be03620e | 163 | int ret = 0; |
6f925ba9 | 164 | shared_ptr<sigrok::Context> context; |
58d8e4c6 | 165 | string open_file, open_file_format, driver; |
156f06e9 | 166 | bool restore_sessions = true; |
1d6e35dc | 167 | bool do_scan = true; |
c062efdc | 168 | bool do_logging = true; |
f2242929 | 169 | |
dac1bb97 | 170 | Application a(argc, argv); |
640e5565 | 171 | |
9137928c | 172 | #ifdef ANDROID |
aff51746 | 173 | srau_init_environment(); |
9137928c | 174 | pv::AndroidLogHandler::install_callbacks(); |
dddff2e7 | 175 | pv::AndroidAssetReader asset_reader; |
9137928c MC |
176 | #endif |
177 | ||
f7951df4 | 178 | // Parse arguments |
15a69e54 | 179 | while (true) { |
f7951df4 | 180 | static const struct option long_options[] = { |
f74015db UH |
181 | {"help", no_argument, nullptr, 'h'}, |
182 | {"version", no_argument, nullptr, 'V'}, | |
183 | {"loglevel", required_argument, nullptr, 'l'}, | |
58d8e4c6 | 184 | {"driver", required_argument, nullptr, 'd'}, |
d0f22e70 | 185 | {"no-scan", no_argument, nullptr, 'D'}, |
f74015db UH |
186 | {"input-file", required_argument, nullptr, 'i'}, |
187 | {"input-format", required_argument, nullptr, 'I'}, | |
156f06e9 | 188 | {"clean", no_argument, nullptr, 'c'}, |
c062efdc | 189 | {"log-to-stdout", no_argument, nullptr, 's'}, |
f74015db | 190 | {nullptr, 0, nullptr, 0} |
f7951df4 JH |
191 | }; |
192 | ||
815b3f26 | 193 | const int c = getopt_long(argc, argv, |
c062efdc | 194 | "h?VDcsl:d:i:I:", long_options, nullptr); |
f7951df4 JH |
195 | if (c == -1) |
196 | break; | |
197 | ||
198 | switch (c) { | |
aa90e86b JH |
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 | ||
9b5099b6 JH |
209 | case 'l': |
210 | { | |
211 | const int loglevel = atoi(optarg); | |
ed6f8680 GS |
212 | if (loglevel < 0 || loglevel > 5) { |
213 | qDebug() << "ERROR: invalid log level spec."; | |
214 | break; | |
215 | } | |
e8d00928 | 216 | context->set_log_level(sigrok::LogLevel::get(loglevel)); |
269528f5 JH |
217 | |
218 | #ifdef ENABLE_DECODE | |
9b5099b6 | 219 | srd_log_loglevel_set(loglevel); |
269528f5 | 220 | #endif |
d52d8455 | 221 | |
0bd39c0c UH |
222 | if (loglevel >= 5) { |
223 | const QSettings settings; | |
224 | qDebug() << "Settings:" << settings.fileName() | |
225 | << "format" << settings.format(); | |
226 | } | |
9b5099b6 JH |
227 | break; |
228 | } | |
e3c79b07 | 229 | |
58d8e4c6 GS |
230 | case 'd': |
231 | driver = optarg; | |
232 | break; | |
233 | ||
1d6e35dc GS |
234 | case 'D': |
235 | do_scan = false; | |
236 | break; | |
237 | ||
e3c79b07 JH |
238 | case 'i': |
239 | open_file = optarg; | |
240 | break; | |
241 | ||
242 | case 'I': | |
243 | open_file_format = optarg; | |
244 | break; | |
156f06e9 SA |
245 | |
246 | case 'c': | |
247 | restore_sessions = false; | |
248 | break; | |
c062efdc SA |
249 | |
250 | case 's': | |
251 | do_logging = false; | |
252 | break; | |
f7951df4 JH |
253 | } |
254 | } | |
255 | ||
3e5bc268 | 256 | if (argc - optind > 1) { |
39ccf9c3 | 257 | fprintf(stderr, "Only one file can be opened.\n"); |
1d478458 | 258 | return 1; |
e3c79b07 | 259 | } |
1d478458 | 260 | |
c063290a UH |
261 | if (argc - optind == 1) |
262 | open_file = argv[argc - 1]; | |
263 | ||
bcb4c327 SA |
264 | // Prepare the global settings since logging needs them early on |
265 | pv::GlobalSettings settings; | |
266 | settings.set_defaults_where_needed(); | |
267 | ||
c062efdc SA |
268 | if (do_logging) |
269 | pv::logging.init(); | |
bcb4c327 | 270 | |
6a6772b7 | 271 | // Initialise libsigrok |
e8d00928 | 272 | context = sigrok::Context::create(); |
419ec4e1 SA |
273 | pv::Session::sr_context = context; |
274 | ||
c1a688de SA |
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); | |
c5d6200c SA |
283 | |
284 | if (QFileInfo::exists(stacktrace_filename)) | |
285 | process_stacktrace(temp_path); | |
c1a688de SA |
286 | #endif |
287 | ||
dddff2e7 DE |
288 | #ifdef ANDROID |
289 | context->set_resource_reader(&asset_reader); | |
290 | #endif | |
d52d8455 JH |
291 | do { |
292 | ||
269528f5 | 293 | #ifdef ENABLE_DECODE |
d52d8455 | 294 | // Initialise libsigrokdecode |
4c60462b | 295 | if (srd_init(nullptr) != SRD_OK) { |
d52d8455 JH |
296 | qDebug() << "ERROR: libsigrokdecode init failed."; |
297 | break; | |
298 | } | |
be03620e JH |
299 | |
300 | // Load the protocol decoders | |
301 | srd_decoder_load_all(); | |
269528f5 | 302 | #endif |
be03620e | 303 | |
c1a688de | 304 | #ifndef ENABLE_STACKTRACE |
107ca6d3 | 305 | try { |
c1a688de | 306 | #endif |
664fca5c | 307 | |
c1a688de SA |
308 | // Create the device manager, initialise the drivers |
309 | pv::DeviceManager device_manager(context, driver, do_scan); | |
664fca5c | 310 | |
c1a688de SA |
311 | // Initialise the main window |
312 | pv::MainWindow w(device_manager); | |
313 | w.show(); | |
3ed18835 | 314 | |
c1a688de SA |
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(); | |
3ed18835 | 322 | |
140181a4 | 323 | #ifdef ENABLE_SIGNALS |
c1a688de SA |
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."; | |
140181a4 | 332 | #endif |
7a255aa9 | 333 | |
c1a688de SA |
334 | // Run the application |
335 | ret = a.exec(); | |
107ca6d3 | 336 | |
c1a688de | 337 | #ifndef ENABLE_STACKTRACE |
30677c13 | 338 | } catch (exception& e) { |
c1a688de | 339 | qDebug() << "Exception:" << e.what(); |
6fb67b27 | 340 | } |
c1a688de | 341 | #endif |
6fb67b27 | 342 | |
269528f5 | 343 | #ifdef ENABLE_DECODE |
d52d8455 | 344 | // Destroy libsigrokdecode |
be03620e | 345 | srd_exit(); |
269528f5 | 346 | #endif |
664fca5c | 347 | |
15a69e54 | 348 | } while (false); |
664fca5c | 349 | |
664fca5c | 350 | return ret; |
d7bed479 | 351 | } |