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. */
30 #include <gstreamermm.h>
31 #include <libsigrokflow/libsigrokflow.hpp>
34 #include <libsigrokcxx/libsigrokcxx.hpp>
40 #include <QMessageBox>
42 #include <QTextStream>
47 #include "signalhandler.hpp"
50 #ifdef ENABLE_STACKTRACE
52 #include <boost/stacktrace.hpp>
53 #include <QStandardPaths>
56 #include "pv/application.hpp"
57 #include "pv/devicemanager.hpp"
58 #include "pv/globalsettings.hpp"
59 #include "pv/logging.hpp"
60 #include "pv/mainwindow.hpp"
61 #include "pv/session.hpp"
62 #include "pv/util.hpp"
65 #include <libsigrokandroidutils/libsigrokandroidutils.h>
66 #include "android/assetreader.hpp"
67 #include "android/loghandler.hpp"
72 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
73 Q_IMPORT_PLUGIN(QSvgPlugin)
79 using std::shared_ptr;
83 QString stacktrace_filename;
85 void signal_handler(int signum)
87 ::signal(signum, SIG_DFL);
88 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
92 void process_stacktrace(QString temp_path)
94 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
96 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
97 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
98 ofstream::out | ofstream::trunc);
100 boost::stacktrace::stacktrace st =
101 boost::stacktrace::stacktrace::from_dump(ifs);
107 QFile f(stacktrace_outfile);
108 f.open(QFile::ReadOnly | QFile::Text);
110 QString stacktrace = fs.readAll();
111 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
113 qDebug() << QObject::tr("Stack trace of previous crash:");
114 qDebug() << "---------------------------------------------------------";
115 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
116 qDebug() << stacktrace.toLocal8Bit().data();
117 qDebug() << "---------------------------------------------------------";
121 // Remove stack trace so we don't process it again the next time we run
122 QFile::remove(stacktrace_filename.toLocal8Bit().data());
124 // Show notification dialog if permitted
125 pv::GlobalSettings settings;
126 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
127 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
130 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
131 "A human-readable form has been saved to disk and was written to " \
132 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
133 msgbox.setIcon(QMessageBox::Icon::Information);
134 msgbox.addButton(QMessageBox::Ok);
135 msgbox.setCheckBox(cb);
137 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
138 pv::GlobalSettings settings;
139 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
151 " %s [OPTIONS] [FILE]\n"
154 " -h, -?, --help Show help option\n"
156 "Application Options:\n"
157 " -V, --version Show release version\n"
158 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
159 " -d, --driver Specify the device driver to use\n"
160 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
161 " -i, --input-file Load input from file\n"
162 " -I, --input-format Input format\n"
163 " -c, --clean Don't restore previous sessions on startup\n"
167 int main(int argc, char *argv[])
170 shared_ptr<sigrok::Context> context;
171 string open_file_format, driver;
172 vector<string> open_files;
173 bool restore_sessions = true;
175 bool show_version = false;
178 // Initialise gstreamermm. Must be called before any other GLib stuff.
181 // Initialize libsigrokflow. Must be called after Gst::init().
185 Application a(argc, argv);
188 srau_init_environment();
189 pv::AndroidLogHandler::install_callbacks();
190 pv::AndroidAssetReader asset_reader;
195 static const struct option long_options[] = {
196 {"help", no_argument, nullptr, 'h'},
197 {"version", no_argument, nullptr, 'V'},
198 {"loglevel", required_argument, nullptr, 'l'},
199 {"driver", required_argument, nullptr, 'd'},
200 {"dont-scan", no_argument, nullptr, 'D'},
201 {"input-file", required_argument, nullptr, 'i'},
202 {"input-format", required_argument, nullptr, 'I'},
203 {"clean", no_argument, nullptr, 'c'},
204 {"log-to-stdout", no_argument, nullptr, 's'},
205 {nullptr, 0, nullptr, 0}
208 const int c = getopt_long(argc, argv,
209 "h?VDcl:d:i:I:", long_options, nullptr);
225 const int loglevel = atoi(optarg);
226 if (loglevel < 0 || loglevel > 5) {
227 qDebug() << "ERROR: invalid log level spec.";
230 context->set_log_level(sigrok::LogLevel::get(loglevel));
233 srd_log_loglevel_set(loglevel);
237 const QSettings settings;
238 qDebug() << "Settings:" << settings.fileName()
239 << "format" << settings.format();
253 open_files.emplace_back(optarg);
257 open_file_format = optarg;
261 restore_sessions = false;
268 for (int i = 0; i < argc; i++)
269 open_files.emplace_back(argv[i]);
271 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
272 qRegisterMetaType<uint64_t>("uint64_t");
274 // Prepare the global settings since logging needs them early on
275 pv::GlobalSettings settings;
276 settings.save_internal_defaults();
277 settings.set_defaults_where_needed();
278 settings.apply_theme();
282 // Initialise libsigrok
283 context = sigrok::Context::create();
284 pv::Session::sr_context = context;
286 #if ENABLE_STACKTRACE
287 QString temp_path = QStandardPaths::standardLocations(
288 QStandardPaths::TempLocation).at(0);
289 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
290 qDebug() << "Stack trace file is" << stacktrace_filename;
292 ::signal(SIGSEGV, &signal_handler);
293 ::signal(SIGABRT, &signal_handler);
295 if (QFileInfo::exists(stacktrace_filename))
296 process_stacktrace(temp_path);
300 context->set_resource_reader(&asset_reader);
305 // Initialise libsigrokdecode
306 if (srd_init(nullptr) != SRD_OK) {
307 qDebug() << "ERROR: libsigrokdecode init failed.";
311 // Load the protocol decoders
312 srd_decoder_load_all();
315 #ifndef ENABLE_STACKTRACE
319 // Create the device manager, initialise the drivers
320 pv::DeviceManager device_manager(context, driver, do_scan);
322 a.collect_version_info(context);
324 a.print_version_info();
326 // Initialise the main window
327 pv::MainWindow w(device_manager);
330 if (restore_sessions)
331 w.restore_sessions();
333 if (open_files.empty())
334 w.add_default_session();
336 for (string& open_file : open_files)
337 w.add_session_with_file(open_file, open_file_format);
339 #ifdef ENABLE_SIGNALS
340 if (SignalHandler::prepare_signals()) {
341 SignalHandler *const handler = new SignalHandler(&w);
342 QObject::connect(handler, SIGNAL(int_received()),
344 QObject::connect(handler, SIGNAL(term_received()),
347 qWarning() << "Could not prepare signal handler.";
350 // Run the application
354 #ifndef ENABLE_STACKTRACE
355 } catch (exception& e) {
356 qDebug() << "Exception:" << e.what();
361 // Destroy libsigrokdecode