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 #include <gstreamermm.h>
31 #include <libsigrokcxx/libsigrokcxx.hpp>
37 #include <QMessageBox>
39 #include <QTextStream>
44 #include "signalhandler.hpp"
47 #ifdef ENABLE_STACKTRACE
49 #include <boost/stacktrace.hpp>
50 #include <QStandardPaths>
53 #include "pv/application.hpp"
54 #include "pv/devicemanager.hpp"
55 #include "pv/globalsettings.hpp"
56 #include "pv/logging.hpp"
57 #include "pv/mainwindow.hpp"
58 #include "pv/session.hpp"
59 #include "pv/util.hpp"
62 #include <libsigrokandroidutils/libsigrokandroidutils.h>
63 #include "android/assetreader.hpp"
64 #include "android/loghandler.hpp"
69 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
70 Q_IMPORT_PLUGIN(QSvgPlugin)
76 using std::shared_ptr;
80 QString stacktrace_filename;
82 void signal_handler(int signum)
84 ::signal(signum, SIG_DFL);
85 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
89 void process_stacktrace(QString temp_path)
91 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
93 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
94 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
95 ofstream::out | ofstream::trunc);
97 boost::stacktrace::stacktrace st =
98 boost::stacktrace::stacktrace::from_dump(ifs);
104 QFile f(stacktrace_outfile);
105 f.open(QFile::ReadOnly | QFile::Text);
107 QString stacktrace = fs.readAll();
108 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
110 qDebug() << QObject::tr("Stack trace of previous crash:");
111 qDebug() << "---------------------------------------------------------";
112 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
113 qDebug() << stacktrace.toLocal8Bit().data();
114 qDebug() << "---------------------------------------------------------";
118 // Remove stack trace so we don't process it again the next time we run
119 QFile::remove(stacktrace_filename.toLocal8Bit().data());
121 // Show notification dialog if permitted
122 pv::GlobalSettings settings;
123 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
124 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
127 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
128 "A human-readable form has been saved to disk and was written to " \
129 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
130 msgbox.setIcon(QMessageBox::Icon::Information);
131 msgbox.addButton(QMessageBox::Ok);
132 msgbox.setCheckBox(cb);
134 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
135 pv::GlobalSettings settings;
136 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
148 " %s [OPTIONS] [FILE]\n"
151 " -h, -?, --help Show help option\n"
153 "Application Options:\n"
154 " -V, --version Show release version\n"
155 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
156 " -d, --driver Specify the device driver to use\n"
157 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
158 " -i, --input-file Load input from file\n"
159 " -I, --input-format Input format\n"
160 " -c, --clean Don't restore previous sessions on startup\n"
164 int main(int argc, char *argv[])
167 shared_ptr<sigrok::Context> context;
168 string open_file_format, driver;
169 vector<string> open_files;
170 bool restore_sessions = true;
172 bool show_version = false;
174 // Initialise gstreamermm. Must be called before any other GLib stuff.
177 Application a(argc, argv);
180 srau_init_environment();
181 pv::AndroidLogHandler::install_callbacks();
182 pv::AndroidAssetReader asset_reader;
187 static const struct option long_options[] = {
188 {"help", no_argument, nullptr, 'h'},
189 {"version", no_argument, nullptr, 'V'},
190 {"loglevel", required_argument, nullptr, 'l'},
191 {"driver", required_argument, nullptr, 'd'},
192 {"dont-scan", no_argument, nullptr, 'D'},
193 {"input-file", required_argument, nullptr, 'i'},
194 {"input-format", required_argument, nullptr, 'I'},
195 {"clean", no_argument, nullptr, 'c'},
196 {"log-to-stdout", no_argument, nullptr, 's'},
197 {nullptr, 0, nullptr, 0}
200 const int c = getopt_long(argc, argv,
201 "h?VDcl:d:i:I:", long_options, nullptr);
217 const int loglevel = atoi(optarg);
218 if (loglevel < 0 || loglevel > 5) {
219 qDebug() << "ERROR: invalid log level spec.";
222 context->set_log_level(sigrok::LogLevel::get(loglevel));
225 srd_log_loglevel_set(loglevel);
229 const QSettings settings;
230 qDebug() << "Settings:" << settings.fileName()
231 << "format" << settings.format();
245 open_files.emplace_back(optarg);
249 open_file_format = optarg;
253 restore_sessions = false;
260 for (int i = 0; i < argc; i++)
261 open_files.emplace_back(argv[i]);
263 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
264 qRegisterMetaType<uint64_t>("uint64_t");
266 // Prepare the global settings since logging needs them early on
267 pv::GlobalSettings settings;
268 settings.save_internal_defaults();
269 settings.set_defaults_where_needed();
270 settings.apply_theme();
274 // Initialise libsigrok
275 context = sigrok::Context::create();
276 pv::Session::sr_context = context;
278 #if ENABLE_STACKTRACE
279 QString temp_path = QStandardPaths::standardLocations(
280 QStandardPaths::TempLocation).at(0);
281 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
282 qDebug() << "Stack trace file is" << stacktrace_filename;
284 ::signal(SIGSEGV, &signal_handler);
285 ::signal(SIGABRT, &signal_handler);
287 if (QFileInfo::exists(stacktrace_filename))
288 process_stacktrace(temp_path);
292 context->set_resource_reader(&asset_reader);
297 // Initialise libsigrokdecode
298 if (srd_init(nullptr) != SRD_OK) {
299 qDebug() << "ERROR: libsigrokdecode init failed.";
303 // Load the protocol decoders
304 srd_decoder_load_all();
307 #ifndef ENABLE_STACKTRACE
311 // Create the device manager, initialise the drivers
312 pv::DeviceManager device_manager(context, driver, do_scan);
314 a.collect_version_info(context);
316 a.print_version_info();
318 // Initialise the main window
319 pv::MainWindow w(device_manager);
322 if (restore_sessions)
323 w.restore_sessions();
325 if (open_files.empty())
326 w.add_default_session();
328 for (string& open_file : open_files)
329 w.add_session_with_file(open_file, open_file_format);
331 #ifdef ENABLE_SIGNALS
332 if (SignalHandler::prepare_signals()) {
333 SignalHandler *const handler = new SignalHandler(&w);
334 QObject::connect(handler, SIGNAL(int_received()),
336 QObject::connect(handler, SIGNAL(term_received()),
339 qWarning() << "Could not prepare signal handler.";
342 // Run the application
346 #ifndef ENABLE_STACKTRACE
347 } catch (exception& e) {
348 qDebug() << "Exception:" << e.what();
353 // Destroy libsigrokdecode