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. */
29 #ifdef ENABLE_GSTREAMERMM
30 #include <gstreamermm.h>
33 #include <libsigrokcxx/libsigrokcxx.hpp>
39 #include <QMessageBox>
41 #include <QTextStream>
46 #include "signalhandler.hpp"
49 #ifdef ENABLE_STACKTRACE
51 #include <boost/stacktrace.hpp>
52 #include <QStandardPaths>
55 #include "pv/application.hpp"
56 #include "pv/devicemanager.hpp"
57 #include "pv/globalsettings.hpp"
58 #include "pv/logging.hpp"
59 #include "pv/mainwindow.hpp"
60 #include "pv/session.hpp"
61 #include "pv/util.hpp"
64 #include <libsigrokandroidutils/libsigrokandroidutils.h>
65 #include "android/assetreader.hpp"
66 #include "android/loghandler.hpp"
71 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
72 Q_IMPORT_PLUGIN(QSvgPlugin)
78 using std::shared_ptr;
82 QString stacktrace_filename;
84 void signal_handler(int signum)
86 ::signal(signum, SIG_DFL);
87 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
91 void process_stacktrace(QString temp_path)
93 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
95 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
96 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
97 ofstream::out | ofstream::trunc);
99 boost::stacktrace::stacktrace st =
100 boost::stacktrace::stacktrace::from_dump(ifs);
106 QFile f(stacktrace_outfile);
107 f.open(QFile::ReadOnly | QFile::Text);
109 QString stacktrace = fs.readAll();
110 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
112 qDebug() << QObject::tr("Stack trace of previous crash:");
113 qDebug() << "---------------------------------------------------------";
114 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
115 qDebug() << stacktrace.toLocal8Bit().data();
116 qDebug() << "---------------------------------------------------------";
120 // Remove stack trace so we don't process it again the next time we run
121 QFile::remove(stacktrace_filename.toLocal8Bit().data());
123 // Show notification dialog if permitted
124 pv::GlobalSettings settings;
125 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
126 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
129 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
130 "A human-readable form has been saved to disk and was written to " \
131 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
132 msgbox.setIcon(QMessageBox::Icon::Information);
133 msgbox.addButton(QMessageBox::Ok);
134 msgbox.setCheckBox(cb);
136 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
137 pv::GlobalSettings settings;
138 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
150 " %s [OPTIONS] [FILE]\n"
153 " -h, -?, --help Show help option\n"
155 "Application Options:\n"
156 " -V, --version Show release version\n"
157 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
158 " -d, --driver Specify the device driver to use\n"
159 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
160 " -i, --input-file Load input from file\n"
161 " -I, --input-format Input format\n"
162 " -c, --clean Don't restore previous sessions on startup\n"
166 int main(int argc, char *argv[])
169 shared_ptr<sigrok::Context> context;
170 string open_file_format, driver;
171 vector<string> open_files;
172 bool restore_sessions = true;
174 bool show_version = false;
176 #ifdef ENABLE_GSTREAMERMM
177 // Initialise gstreamermm. Must be called before any other GLib stuff.
181 Application a(argc, argv);
184 srau_init_environment();
185 pv::AndroidLogHandler::install_callbacks();
186 pv::AndroidAssetReader asset_reader;
191 static const struct option long_options[] = {
192 {"help", no_argument, nullptr, 'h'},
193 {"version", no_argument, nullptr, 'V'},
194 {"loglevel", required_argument, nullptr, 'l'},
195 {"driver", required_argument, nullptr, 'd'},
196 {"dont-scan", no_argument, nullptr, 'D'},
197 {"input-file", required_argument, nullptr, 'i'},
198 {"input-format", required_argument, nullptr, 'I'},
199 {"clean", no_argument, nullptr, 'c'},
200 {"log-to-stdout", no_argument, nullptr, 's'},
201 {nullptr, 0, nullptr, 0}
204 const int c = getopt_long(argc, argv,
205 "h?VDcl:d:i:I:", long_options, nullptr);
221 const int loglevel = atoi(optarg);
222 if (loglevel < 0 || loglevel > 5) {
223 qDebug() << "ERROR: invalid log level spec.";
226 context->set_log_level(sigrok::LogLevel::get(loglevel));
229 srd_log_loglevel_set(loglevel);
233 const QSettings settings;
234 qDebug() << "Settings:" << settings.fileName()
235 << "format" << settings.format();
249 open_files.emplace_back(optarg);
253 open_file_format = optarg;
257 restore_sessions = false;
264 for (int i = 0; i < argc; i++)
265 open_files.emplace_back(argv[i]);
267 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
268 qRegisterMetaType<uint64_t>("uint64_t");
270 // Prepare the global settings since logging needs them early on
271 pv::GlobalSettings settings;
272 settings.save_internal_defaults();
273 settings.set_defaults_where_needed();
274 settings.apply_theme();
278 // Initialise libsigrok
279 context = sigrok::Context::create();
280 pv::Session::sr_context = context;
282 #if ENABLE_STACKTRACE
283 QString temp_path = QStandardPaths::standardLocations(
284 QStandardPaths::TempLocation).at(0);
285 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
286 qDebug() << "Stack trace file is" << stacktrace_filename;
288 ::signal(SIGSEGV, &signal_handler);
289 ::signal(SIGABRT, &signal_handler);
291 if (QFileInfo::exists(stacktrace_filename))
292 process_stacktrace(temp_path);
296 context->set_resource_reader(&asset_reader);
301 // Initialise libsigrokdecode
302 if (srd_init(nullptr) != SRD_OK) {
303 qDebug() << "ERROR: libsigrokdecode init failed.";
307 // Load the protocol decoders
308 srd_decoder_load_all();
311 #ifndef ENABLE_STACKTRACE
315 // Create the device manager, initialise the drivers
316 pv::DeviceManager device_manager(context, driver, do_scan);
318 a.collect_version_info(context);
320 a.print_version_info();
322 // Initialise the main window
323 pv::MainWindow w(device_manager);
326 if (restore_sessions)
327 w.restore_sessions();
329 if (open_files.empty())
330 w.add_default_session();
332 for (string& open_file : open_files)
333 w.add_session_with_file(open_file, open_file_format);
335 #ifdef ENABLE_SIGNALS
336 if (SignalHandler::prepare_signals()) {
337 SignalHandler *const handler = new SignalHandler(&w);
338 QObject::connect(handler, SIGNAL(int_received()),
340 QObject::connect(handler, SIGNAL(term_received()),
343 qWarning() << "Could not prepare signal handler.";
346 // Run the application
350 #ifndef ENABLE_STACKTRACE
351 } catch (exception& e) {
352 qDebug() << "Exception:" << e.what();
357 // Destroy libsigrokdecode