]> sigrok.org Git - pulseview.git/blame - main.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / main.cpp
CommitLineData
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 26#include <getopt.h>
caa2b3e9 27#include <vector>
f7951df4 28
696c7eac 29#ifdef ENABLE_FLOW
c9f57375 30#include <gstreamermm.h>
045d8116 31#include <libsigrokflow/libsigrokflow.hpp>
5252f438 32#endif
c9f57375 33
c5d6200c
SA
34#include <libsigrokcxx/libsigrokcxx.hpp>
35
36#include <QCheckBox>
664fca5c 37#include <QDebug>
c5d6200c
SA
38#include <QFile>
39#include <QFileInfo>
40#include <QMessageBox>
0bd39c0c 41#include <QSettings>
c5d6200c 42#include <QTextStream>
075fb499 43
49719858
SA
44#include "config.h"
45
140181a4 46#ifdef ENABLE_SIGNALS
2acdb232 47#include "signalhandler.hpp"
140181a4
JH
48#endif
49
c1a688de
SA
50#ifdef ENABLE_STACKTRACE
51#include <signal.h>
52#include <boost/stacktrace.hpp>
53#include <QStandardPaths>
54#endif
55
2acdb232
JH
56#include "pv/application.hpp"
57#include "pv/devicemanager.hpp"
bcb4c327
SA
58#include "pv/globalsettings.hpp"
59#include "pv/logging.hpp"
2acdb232 60#include "pv/mainwindow.hpp"
419ec4e1 61#include "pv/session.hpp"
21f58646 62#include "pv/util.hpp"
1f3033cb 63#include "pv/data/segment.hpp"
c1a688de 64
9137928c 65#ifdef ANDROID
aff51746 66#include <libsigrokandroidutils/libsigrokandroidutils.h>
dddff2e7 67#include "android/assetreader.hpp"
2acdb232 68#include "android/loghandler.hpp"
9137928c 69#endif
d7bed479 70
11a04e2a 71#ifdef _WIN32
11a04e2a 72#include <QtPlugin>
b987d46c 73#ifdef QT_STATIC
09f55d96
UH
74Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
75Q_IMPORT_PLUGIN(QSvgPlugin)
11a04e2a 76#endif
b987d46c 77#endif
11a04e2a 78
6f925ba9 79using std::exception;
c5d6200c
SA
80using std::ifstream;
81using std::ofstream;
6f925ba9
UH
82using std::shared_ptr;
83using std::string;
84
c1a688de
SA
85#if ENABLE_STACKTRACE
86QString stacktrace_filename;
87
88void signal_handler(int signum)
89{
90 ::signal(signum, SIG_DFL);
91 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
92 ::raise(SIGABRT);
93}
c5d6200c
SA
94
95void process_stacktrace(QString temp_path)
96{
97 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
98
99 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
100 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
101 ofstream::out | ofstream::trunc);
102
103 boost::stacktrace::stacktrace st =
104 boost::stacktrace::stacktrace::from_dump(ifs);
105 ofs << st;
106
107 ofs.close();
108 ifs.close();
109
110 QFile f(stacktrace_outfile);
111 f.open(QFile::ReadOnly | QFile::Text);
112 QTextStream fs(&f);
113 QString stacktrace = fs.readAll();
114 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
115
116 qDebug() << QObject::tr("Stack trace of previous crash:");
117 qDebug() << "---------------------------------------------------------";
118 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
119 qDebug() << stacktrace.toLocal8Bit().data();
120 qDebug() << "---------------------------------------------------------";
121
122 f.close();
123
124 // Remove stack trace so we don't process it again the next time we run
125 QFile::remove(stacktrace_filename.toLocal8Bit().data());
126
127 // Show notification dialog if permitted
128 pv::GlobalSettings settings;
129 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
130 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
131
132 QMessageBox msgbox;
133 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
134 "A human-readable form has been saved to disk and was written to " \
135 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
136 msgbox.setIcon(QMessageBox::Icon::Information);
137 msgbox.addButton(QMessageBox::Ok);
138 msgbox.setCheckBox(cb);
139
140 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
141 pv::GlobalSettings settings;
142 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
143 !state); });
144
145 msgbox.exec();
146 }
147}
c1a688de
SA
148#endif
149
d4384c6d
JH
150void usage()
151{
9fd5bb6e 152 fprintf(stdout,
d4384c6d 153 "Usage:\n"
41e2ade4 154 " %s [OPTIONS] [FILE]\n"
d4384c6d
JH
155 "\n"
156 "Help Options:\n"
d4384c6d 157 " -h, -?, --help Show help option\n"
aa90e86b
JH
158 "\n"
159 "Application Options:\n"
160 " -V, --version Show release version\n"
161 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
58d8e4c6 162 " -d, --driver Specify the device driver to use\n"
0e96068d 163 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
e3c79b07 164 " -i, --input-file Load input from file\n"
611c8625 165 " -s, --settings Load PulseView session setup from file\n"
e3c79b07 166 " -I, --input-format Input format\n"
156f06e9 167 " -c, --clean Don't restore previous sessions on startup\n"
41e2ade4 168 "\n", PV_BIN_NAME);
d4384c6d
JH
169}
170
d7bed479
JH
171int main(int argc, char *argv[])
172{
be03620e 173 int ret = 0;
6f925ba9 174 shared_ptr<sigrok::Context> context;
611c8625 175 string open_file_format, open_setup_file, driver;
caa2b3e9 176 vector<string> open_files;
156f06e9 177 bool restore_sessions = true;
1d6e35dc 178 bool do_scan = true;
49719858 179 bool show_version = false;
f2242929 180
696c7eac 181#ifdef ENABLE_FLOW
c9f57375
UH
182 // Initialise gstreamermm. Must be called before any other GLib stuff.
183 Gst::init();
024db17b
UH
184
185 // Initialize libsigrokflow. Must be called after Gst::init().
186 Srf::init();
5252f438 187#endif
c9f57375 188
dac1bb97 189 Application a(argc, argv);
640e5565 190
9137928c 191#ifdef ANDROID
aff51746 192 srau_init_environment();
9137928c 193 pv::AndroidLogHandler::install_callbacks();
dddff2e7 194 pv::AndroidAssetReader asset_reader;
9137928c
MC
195#endif
196
f7951df4 197 // Parse arguments
15a69e54 198 while (true) {
f7951df4 199 static const struct option long_options[] = {
f74015db
UH
200 {"help", no_argument, nullptr, 'h'},
201 {"version", no_argument, nullptr, 'V'},
202 {"loglevel", required_argument, nullptr, 'l'},
58d8e4c6 203 {"driver", required_argument, nullptr, 'd'},
0e96068d 204 {"dont-scan", no_argument, nullptr, 'D'},
f74015db 205 {"input-file", required_argument, nullptr, 'i'},
611c8625 206 {"settings", required_argument, nullptr, 's'},
f74015db 207 {"input-format", required_argument, nullptr, 'I'},
156f06e9 208 {"clean", no_argument, nullptr, 'c'},
c062efdc 209 {"log-to-stdout", no_argument, nullptr, 's'},
f74015db 210 {nullptr, 0, nullptr, 0}
f7951df4
JH
211 };
212
815b3f26 213 const int c = getopt_long(argc, argv,
611c8625 214 "h?VDcl:d:i:s:I:", long_options, nullptr);
f7951df4
JH
215 if (c == -1)
216 break;
217
218 switch (c) {
aa90e86b
JH
219 case 'h':
220 case '?':
221 usage();
222 return 0;
223
224 case 'V':
49719858
SA
225 show_version = true;
226 break;
aa90e86b 227
9b5099b6
JH
228 case 'l':
229 {
230 const int loglevel = atoi(optarg);
ed6f8680
GS
231 if (loglevel < 0 || loglevel > 5) {
232 qDebug() << "ERROR: invalid log level spec.";
233 break;
234 }
e8d00928 235 context->set_log_level(sigrok::LogLevel::get(loglevel));
269528f5
JH
236
237#ifdef ENABLE_DECODE
9b5099b6 238 srd_log_loglevel_set(loglevel);
269528f5 239#endif
d52d8455 240
0bd39c0c
UH
241 if (loglevel >= 5) {
242 const QSettings settings;
243 qDebug() << "Settings:" << settings.fileName()
244 << "format" << settings.format();
245 }
9b5099b6
JH
246 break;
247 }
e3c79b07 248
58d8e4c6
GS
249 case 'd':
250 driver = optarg;
251 break;
252
1d6e35dc
GS
253 case 'D':
254 do_scan = false;
255 break;
256
e3c79b07 257 case 'i':
1f2082e2 258 open_files.emplace_back(optarg);
e3c79b07
JH
259 break;
260
611c8625
DL
261 case 's':
262 open_setup_file = optarg;
263 break;
264
e3c79b07
JH
265 case 'I':
266 open_file_format = optarg;
267 break;
156f06e9
SA
268
269 case 'c':
270 restore_sessions = false;
271 break;
f7951df4
JH
272 }
273 }
6a96e1b2
GS
274 argc -= optind;
275 argv += optind;
f7951df4 276
6a96e1b2 277 for (int i = 0; i < argc; i++)
1f2082e2 278 open_files.emplace_back(argv[i]);
1d478458 279
21f58646 280 qRegisterMetaType<uint64_t>("uint64_t");
1f3033cb 281 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
720f4762 282 qRegisterMetaType<SharedPtrToSegment>("SharedPtrToSegment");
cf1541a1 283 qRegisterMetaType<shared_ptr<pv::data::SignalBase>>("shared_ptr<SignalBase>");
21f58646 284
bcb4c327
SA
285 // Prepare the global settings since logging needs them early on
286 pv::GlobalSettings settings;
0466001b 287 settings.add_change_handler(&a); // Only the application object can't register itself
374c697f 288 settings.save_internal_defaults();
bcb4c327 289 settings.set_defaults_where_needed();
0466001b 290 settings.apply_language();
37b0bd35 291 settings.apply_theme();
bcb4c327 292
4b923408 293 pv::logging.init();
bcb4c327 294
6a6772b7 295 // Initialise libsigrok
e8d00928 296 context = sigrok::Context::create();
419ec4e1
SA
297 pv::Session::sr_context = context;
298
c1a688de
SA
299#if ENABLE_STACKTRACE
300 QString temp_path = QStandardPaths::standardLocations(
301 QStandardPaths::TempLocation).at(0);
302 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
303 qDebug() << "Stack trace file is" << stacktrace_filename;
304
305 ::signal(SIGSEGV, &signal_handler);
306 ::signal(SIGABRT, &signal_handler);
c5d6200c
SA
307
308 if (QFileInfo::exists(stacktrace_filename))
309 process_stacktrace(temp_path);
c1a688de
SA
310#endif
311
dddff2e7
DE
312#ifdef ANDROID
313 context->set_resource_reader(&asset_reader);
314#endif
d52d8455
JH
315 do {
316
269528f5 317#ifdef ENABLE_DECODE
d52d8455 318 // Initialise libsigrokdecode
4c60462b 319 if (srd_init(nullptr) != SRD_OK) {
d52d8455
JH
320 qDebug() << "ERROR: libsigrokdecode init failed.";
321 break;
322 }
be03620e
JH
323
324 // Load the protocol decoders
325 srd_decoder_load_all();
269528f5 326#endif
be03620e 327
c1a688de 328#ifndef ENABLE_STACKTRACE
107ca6d3 329 try {
c1a688de 330#endif
664fca5c 331
c1a688de
SA
332 // Create the device manager, initialise the drivers
333 pv::DeviceManager device_manager(context, driver, do_scan);
664fca5c 334
22dc4040 335 a.collect_version_info(device_manager);
49719858
SA
336 if (show_version) {
337 a.print_version_info();
338 } else {
339 // Initialise the main window
340 pv::MainWindow w(device_manager);
341 w.show();
3ed18835 342
49719858
SA
343 if (restore_sessions)
344 w.restore_sessions();
c1a688de 345
49719858
SA
346 if (open_files.empty())
347 w.add_default_session();
348 else
f4ab4b5c 349 for (string& open_file : open_files)
96dbf014 350 w.add_session_with_file(open_file, open_file_format, open_setup_file);
3ed18835 351
140181a4 352#ifdef ENABLE_SIGNALS
49719858
SA
353 if (SignalHandler::prepare_signals()) {
354 SignalHandler *const handler = new SignalHandler(&w);
f1e9295a
MB
355 QObject::connect(handler, SIGNAL(int_received()), &w, SLOT(close()));
356 QObject::connect(handler, SIGNAL(term_received()), &w, SLOT(close()));
357 QObject::connect(handler, SIGNAL(usr1_received()), &w, SLOT(on_run_stop_clicked()));
49719858
SA
358 } else
359 qWarning() << "Could not prepare signal handler.";
140181a4 360#endif
7a255aa9 361
49719858
SA
362 // Run the application
363 ret = a.exec();
364 }
107ca6d3 365
c1a688de 366#ifndef ENABLE_STACKTRACE
30677c13 367 } catch (exception& e) {
c1a688de 368 qDebug() << "Exception:" << e.what();
6fb67b27 369 }
c1a688de 370#endif
6fb67b27 371
269528f5 372#ifdef ENABLE_DECODE
d52d8455 373 // Destroy libsigrokdecode
be03620e 374 srd_exit();
269528f5 375#endif
664fca5c 376
15a69e54 377 } while (false);
664fca5c 378
664fca5c 379 return ret;
d7bed479 380}