]> sigrok.org Git - pulseview.git/blame - main.cpp
Add boost::stacktrace support
[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>
fe3a1c21 25#include <libsigrokcxx/libsigrokcxx.hpp>
664fca5c 26
f7951df4
JH
27#include <getopt.h>
28
664fca5c 29#include <QDebug>
0bd39c0c 30#include <QSettings>
075fb499 31
140181a4 32#ifdef ENABLE_SIGNALS
2acdb232 33#include "signalhandler.hpp"
140181a4
JH
34#endif
35
c1a688de
SA
36#ifdef ENABLE_STACKTRACE
37#include <signal.h>
38#include <boost/stacktrace.hpp>
39#include <QStandardPaths>
40#endif
41
2acdb232
JH
42#include "pv/application.hpp"
43#include "pv/devicemanager.hpp"
44#include "pv/mainwindow.hpp"
419ec4e1 45#include "pv/session.hpp"
c1a688de 46
9137928c 47#ifdef ANDROID
aff51746 48#include <libsigrokandroidutils/libsigrokandroidutils.h>
dddff2e7 49#include "android/assetreader.hpp"
2acdb232 50#include "android/loghandler.hpp"
9137928c 51#endif
d7bed479 52
075fb499
AG
53#include "config.h"
54
11a04e2a 55#ifdef _WIN32
11a04e2a 56#include <QtPlugin>
09f55d96
UH
57Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
58Q_IMPORT_PLUGIN(QSvgPlugin)
11a04e2a
UH
59#endif
60
6f925ba9
UH
61using std::exception;
62using std::shared_ptr;
63using std::string;
64
c1a688de
SA
65#if ENABLE_STACKTRACE
66QString stacktrace_filename;
67
68void signal_handler(int signum)
69{
70 ::signal(signum, SIG_DFL);
71 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
72 ::raise(SIGABRT);
73}
74#endif
75
d4384c6d
JH
76void usage()
77{
9fd5bb6e 78 fprintf(stdout,
d4384c6d 79 "Usage:\n"
41e2ade4 80 " %s [OPTIONS] [FILE]\n"
d4384c6d
JH
81 "\n"
82 "Help Options:\n"
d4384c6d 83 " -h, -?, --help Show help option\n"
aa90e86b
JH
84 "\n"
85 "Application Options:\n"
86 " -V, --version Show release version\n"
87 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
58d8e4c6 88 " -d, --driver Specify the device driver to use\n"
1d6e35dc 89 " -D, --no-scan Don't auto-scan for devices, use -d spec only\n"
e3c79b07
JH
90 " -i, --input-file Load input from file\n"
91 " -I, --input-format Input format\n"
156f06e9 92 " -c, --clean Don't restore previous sessions on startup\n"
41e2ade4 93 "\n", PV_BIN_NAME);
d4384c6d
JH
94}
95
d7bed479
JH
96int main(int argc, char *argv[])
97{
be03620e 98 int ret = 0;
6f925ba9 99 shared_ptr<sigrok::Context> context;
58d8e4c6 100 string open_file, open_file_format, driver;
156f06e9 101 bool restore_sessions = true;
1d6e35dc 102 bool do_scan = true;
f2242929 103
dac1bb97 104 Application a(argc, argv);
640e5565 105
9137928c 106#ifdef ANDROID
aff51746 107 srau_init_environment();
9137928c 108 pv::AndroidLogHandler::install_callbacks();
dddff2e7 109 pv::AndroidAssetReader asset_reader;
9137928c
MC
110#endif
111
f7951df4 112 // Parse arguments
15a69e54 113 while (true) {
f7951df4 114 static const struct option long_options[] = {
f74015db
UH
115 {"help", no_argument, nullptr, 'h'},
116 {"version", no_argument, nullptr, 'V'},
117 {"loglevel", required_argument, nullptr, 'l'},
58d8e4c6 118 {"driver", required_argument, nullptr, 'd'},
f74015db
UH
119 {"input-file", required_argument, nullptr, 'i'},
120 {"input-format", required_argument, nullptr, 'I'},
156f06e9 121 {"clean", no_argument, nullptr, 'c'},
f74015db 122 {nullptr, 0, nullptr, 0}
f7951df4
JH
123 };
124
815b3f26 125 const int c = getopt_long(argc, argv,
1d6e35dc 126 "l:Vhc?d:Di:I:", long_options, nullptr);
f7951df4
JH
127 if (c == -1)
128 break;
129
130 switch (c) {
aa90e86b
JH
131 case 'h':
132 case '?':
133 usage();
134 return 0;
135
136 case 'V':
137 // Print version info
138 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
139 return 0;
140
9b5099b6
JH
141 case 'l':
142 {
143 const int loglevel = atoi(optarg);
ed6f8680
GS
144 if (loglevel < 0 || loglevel > 5) {
145 qDebug() << "ERROR: invalid log level spec.";
146 break;
147 }
e8d00928 148 context->set_log_level(sigrok::LogLevel::get(loglevel));
269528f5
JH
149
150#ifdef ENABLE_DECODE
9b5099b6 151 srd_log_loglevel_set(loglevel);
269528f5 152#endif
d52d8455 153
0bd39c0c
UH
154 if (loglevel >= 5) {
155 const QSettings settings;
156 qDebug() << "Settings:" << settings.fileName()
157 << "format" << settings.format();
158 }
9b5099b6
JH
159 break;
160 }
e3c79b07 161
58d8e4c6
GS
162 case 'd':
163 driver = optarg;
164 break;
165
1d6e35dc
GS
166 case 'D':
167 do_scan = false;
168 break;
169
e3c79b07
JH
170 case 'i':
171 open_file = optarg;
172 break;
173
174 case 'I':
175 open_file_format = optarg;
176 break;
156f06e9
SA
177
178 case 'c':
179 restore_sessions = false;
180 break;
f7951df4
JH
181 }
182 }
183
3e5bc268 184 if (argc - optind > 1) {
39ccf9c3 185 fprintf(stderr, "Only one file can be opened.\n");
1d478458 186 return 1;
e3c79b07 187 }
1d478458 188
c063290a
UH
189 if (argc - optind == 1)
190 open_file = argv[argc - 1];
191
6a6772b7 192 // Initialise libsigrok
e8d00928 193 context = sigrok::Context::create();
419ec4e1
SA
194 pv::Session::sr_context = context;
195
c1a688de
SA
196#if ENABLE_STACKTRACE
197 QString temp_path = QStandardPaths::standardLocations(
198 QStandardPaths::TempLocation).at(0);
199 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
200 qDebug() << "Stack trace file is" << stacktrace_filename;
201
202 ::signal(SIGSEGV, &signal_handler);
203 ::signal(SIGABRT, &signal_handler);
204#endif
205
dddff2e7
DE
206#ifdef ANDROID
207 context->set_resource_reader(&asset_reader);
208#endif
d52d8455
JH
209 do {
210
269528f5 211#ifdef ENABLE_DECODE
d52d8455 212 // Initialise libsigrokdecode
4c60462b 213 if (srd_init(nullptr) != SRD_OK) {
d52d8455
JH
214 qDebug() << "ERROR: libsigrokdecode init failed.";
215 break;
216 }
be03620e
JH
217
218 // Load the protocol decoders
219 srd_decoder_load_all();
269528f5 220#endif
be03620e 221
c1a688de 222#ifndef ENABLE_STACKTRACE
107ca6d3 223 try {
c1a688de 224#endif
664fca5c 225
c1a688de
SA
226 // Create the device manager, initialise the drivers
227 pv::DeviceManager device_manager(context, driver, do_scan);
664fca5c 228
c1a688de
SA
229 // Initialise the main window
230 pv::MainWindow w(device_manager);
231 w.show();
3ed18835 232
c1a688de
SA
233 if (restore_sessions)
234 w.restore_sessions();
235
236 if (!open_file.empty())
237 w.add_session_with_file(open_file, open_file_format);
238 else
239 w.add_default_session();
3ed18835 240
140181a4 241#ifdef ENABLE_SIGNALS
c1a688de
SA
242 if (SignalHandler::prepare_signals()) {
243 SignalHandler *const handler = new SignalHandler(&w);
244 QObject::connect(handler, SIGNAL(int_received()),
245 &w, SLOT(close()));
246 QObject::connect(handler, SIGNAL(term_received()),
247 &w, SLOT(close()));
248 } else
249 qWarning() << "Could not prepare signal handler.";
140181a4 250#endif
7a255aa9 251
c1a688de
SA
252 // Run the application
253 ret = a.exec();
107ca6d3 254
c1a688de 255#ifndef ENABLE_STACKTRACE
30677c13 256 } catch (exception& e) {
c1a688de 257 qDebug() << "Exception:" << e.what();
6fb67b27 258 }
c1a688de 259#endif
6fb67b27 260
269528f5 261#ifdef ENABLE_DECODE
d52d8455 262 // Destroy libsigrokdecode
be03620e 263 srd_exit();
269528f5 264#endif
664fca5c 265
15a69e54 266 } while (false);
664fca5c 267
664fca5c 268 return ret;
d7bed479 269}