2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
21 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
28 #include <libsigrokcxx/libsigrokcxx.hpp>
34 #include <QMessageBox>
36 #include <QTextStream>
39 #include "signalhandler.hpp"
42 #ifdef ENABLE_STACKTRACE
44 #include <boost/stacktrace.hpp>
45 #include <QStandardPaths>
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"
56 #include <libsigrokandroidutils/libsigrokandroidutils.h>
57 #include "android/assetreader.hpp"
58 #include "android/loghandler.hpp"
65 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
66 Q_IMPORT_PLUGIN(QSvgPlugin)
72 using std::shared_ptr;
76 QString stacktrace_filename;
78 void signal_handler(int signum)
80 ::signal(signum, SIG_DFL);
81 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
85 void process_stacktrace(QString temp_path)
87 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
89 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
90 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
91 ofstream::out | ofstream::trunc);
93 boost::stacktrace::stacktrace st =
94 boost::stacktrace::stacktrace::from_dump(ifs);
100 QFile f(stacktrace_outfile);
101 f.open(QFile::ReadOnly | QFile::Text);
103 QString stacktrace = fs.readAll();
104 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
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() << "---------------------------------------------------------";
114 // Remove stack trace so we don't process it again the next time we run
115 QFile::remove(stacktrace_filename.toLocal8Bit().data());
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"));
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);
130 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
131 pv::GlobalSettings settings;
132 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
144 " %s [OPTIONS] [FILE]\n"
147 " -h, -?, --help Show help option\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"
161 int main(int argc, char *argv[])
164 shared_ptr<sigrok::Context> context;
165 string open_file, open_file_format, driver;
166 bool restore_sessions = true;
168 bool do_logging = true;
170 Application a(argc, argv);
173 srau_init_environment();
174 pv::AndroidLogHandler::install_callbacks();
175 pv::AndroidAssetReader asset_reader;
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}
193 const int c = getopt_long(argc, argv,
194 "h?VDcsl:d:i:I:", long_options, nullptr);
205 // Print version info
206 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
211 const int loglevel = atoi(optarg);
212 if (loglevel < 0 || loglevel > 5) {
213 qDebug() << "ERROR: invalid log level spec.";
216 context->set_log_level(sigrok::LogLevel::get(loglevel));
219 srd_log_loglevel_set(loglevel);
223 const QSettings settings;
224 qDebug() << "Settings:" << settings.fileName()
225 << "format" << settings.format();
243 open_file_format = optarg;
247 restore_sessions = false;
256 if (argc - optind > 1) {
257 fprintf(stderr, "Only one file can be opened.\n");
261 if (argc - optind == 1)
262 open_file = argv[argc - 1];
264 // Prepare the global settings since logging needs them early on
265 pv::GlobalSettings settings;
266 settings.set_defaults_where_needed();
271 // Initialise libsigrok
272 context = sigrok::Context::create();
273 pv::Session::sr_context = context;
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;
281 ::signal(SIGSEGV, &signal_handler);
282 ::signal(SIGABRT, &signal_handler);
284 if (QFileInfo::exists(stacktrace_filename))
285 process_stacktrace(temp_path);
289 context->set_resource_reader(&asset_reader);
294 // Initialise libsigrokdecode
295 if (srd_init(nullptr) != SRD_OK) {
296 qDebug() << "ERROR: libsigrokdecode init failed.";
300 // Load the protocol decoders
301 srd_decoder_load_all();
304 #ifndef ENABLE_STACKTRACE
308 // Create the device manager, initialise the drivers
309 pv::DeviceManager device_manager(context, driver, do_scan);
311 // Initialise the main window
312 pv::MainWindow w(device_manager);
315 if (restore_sessions)
316 w.restore_sessions();
318 if (!open_file.empty())
319 w.add_session_with_file(open_file, open_file_format);
321 w.add_default_session();
323 #ifdef ENABLE_SIGNALS
324 if (SignalHandler::prepare_signals()) {
325 SignalHandler *const handler = new SignalHandler(&w);
326 QObject::connect(handler, SIGNAL(int_received()),
328 QObject::connect(handler, SIGNAL(term_received()),
331 qWarning() << "Could not prepare signal handler.";
334 // Run the application
337 #ifndef ENABLE_STACKTRACE
338 } catch (exception& e) {
339 qDebug() << "Exception:" << e.what();
344 // Destroy libsigrokdecode