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 <libsigrokcxx/libsigrokcxx.hpp>
35 #include <QMessageBox>
37 #include <QTextStream>
42 #include "signalhandler.hpp"
45 #ifdef ENABLE_STACKTRACE
47 #include <boost/stacktrace.hpp>
48 #include <QStandardPaths>
51 #include "pv/application.hpp"
52 #include "pv/devicemanager.hpp"
53 #include "pv/globalsettings.hpp"
54 #include "pv/logging.hpp"
55 #include "pv/mainwindow.hpp"
56 #include "pv/session.hpp"
57 #include "pv/util.hpp"
60 #include <libsigrokandroidutils/libsigrokandroidutils.h>
61 #include "android/assetreader.hpp"
62 #include "android/loghandler.hpp"
67 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
68 Q_IMPORT_PLUGIN(QSvgPlugin)
74 using std::shared_ptr;
78 QString stacktrace_filename;
80 void signal_handler(int signum)
82 ::signal(signum, SIG_DFL);
83 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
87 void process_stacktrace(QString temp_path)
89 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
91 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
92 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
93 ofstream::out | ofstream::trunc);
95 boost::stacktrace::stacktrace st =
96 boost::stacktrace::stacktrace::from_dump(ifs);
102 QFile f(stacktrace_outfile);
103 f.open(QFile::ReadOnly | QFile::Text);
105 QString stacktrace = fs.readAll();
106 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
108 qDebug() << QObject::tr("Stack trace of previous crash:");
109 qDebug() << "---------------------------------------------------------";
110 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
111 qDebug() << stacktrace.toLocal8Bit().data();
112 qDebug() << "---------------------------------------------------------";
116 // Remove stack trace so we don't process it again the next time we run
117 QFile::remove(stacktrace_filename.toLocal8Bit().data());
119 // Show notification dialog if permitted
120 pv::GlobalSettings settings;
121 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
122 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
125 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
126 "A human-readable form has been saved to disk and was written to " \
127 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
128 msgbox.setIcon(QMessageBox::Icon::Information);
129 msgbox.addButton(QMessageBox::Ok);
130 msgbox.setCheckBox(cb);
132 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
133 pv::GlobalSettings settings;
134 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
146 " %s [OPTIONS] [FILE]\n"
149 " -h, -?, --help Show help option\n"
151 "Application Options:\n"
152 " -V, --version Show release version\n"
153 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
154 " -d, --driver Specify the device driver to use\n"
155 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
156 " -i, --input-file Load input from file\n"
157 " -I, --input-format Input format\n"
158 " -c, --clean Don't restore previous sessions on startup\n"
162 int main(int argc, char *argv[])
165 shared_ptr<sigrok::Context> context;
166 string open_file_format, driver;
167 vector<string> open_files;
168 bool restore_sessions = true;
170 bool show_version = false;
172 Application a(argc, argv);
175 srau_init_environment();
176 pv::AndroidLogHandler::install_callbacks();
177 pv::AndroidAssetReader asset_reader;
182 static const struct option long_options[] = {
183 {"help", no_argument, nullptr, 'h'},
184 {"version", no_argument, nullptr, 'V'},
185 {"loglevel", required_argument, nullptr, 'l'},
186 {"driver", required_argument, nullptr, 'd'},
187 {"dont-scan", no_argument, nullptr, 'D'},
188 {"input-file", required_argument, nullptr, 'i'},
189 {"input-format", required_argument, nullptr, 'I'},
190 {"clean", no_argument, nullptr, 'c'},
191 {"log-to-stdout", no_argument, nullptr, 's'},
192 {nullptr, 0, nullptr, 0}
195 const int c = getopt_long(argc, argv,
196 "h?VDcl:d:i:I:", long_options, nullptr);
212 const int loglevel = atoi(optarg);
213 if (loglevel < 0 || loglevel > 5) {
214 qDebug() << "ERROR: invalid log level spec.";
217 context->set_log_level(sigrok::LogLevel::get(loglevel));
220 srd_log_loglevel_set(loglevel);
224 const QSettings settings;
225 qDebug() << "Settings:" << settings.fileName()
226 << "format" << settings.format();
240 open_files.emplace_back(optarg);
244 open_file_format = optarg;
248 restore_sessions = false;
255 for (int i = 0; i < argc; i++)
256 open_files.emplace_back(argv[i]);
258 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
259 qRegisterMetaType<uint64_t>("uint64_t");
261 // Prepare the global settings since logging needs them early on
262 pv::GlobalSettings settings;
263 settings.save_internal_defaults();
264 settings.set_defaults_where_needed();
265 settings.apply_theme();
269 // Initialise libsigrok
270 context = sigrok::Context::create();
271 pv::Session::sr_context = context;
273 #if ENABLE_STACKTRACE
274 QString temp_path = QStandardPaths::standardLocations(
275 QStandardPaths::TempLocation).at(0);
276 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
277 qDebug() << "Stack trace file is" << stacktrace_filename;
279 ::signal(SIGSEGV, &signal_handler);
280 ::signal(SIGABRT, &signal_handler);
282 if (QFileInfo::exists(stacktrace_filename))
283 process_stacktrace(temp_path);
287 context->set_resource_reader(&asset_reader);
292 // Initialise libsigrokdecode
293 if (srd_init(nullptr) != SRD_OK) {
294 qDebug() << "ERROR: libsigrokdecode init failed.";
298 // Load the protocol decoders
299 srd_decoder_load_all();
302 #ifndef ENABLE_STACKTRACE
306 // Create the device manager, initialise the drivers
307 pv::DeviceManager device_manager(context, driver, do_scan);
309 a.collect_version_info(context);
311 a.print_version_info();
313 // Initialise the main window
314 pv::MainWindow w(device_manager);
317 if (restore_sessions)
318 w.restore_sessions();
320 if (open_files.empty())
321 w.add_default_session();
323 for (string& open_file : open_files)
324 w.add_session_with_file(open_file, open_file_format);
326 #ifdef ENABLE_SIGNALS
327 if (SignalHandler::prepare_signals()) {
328 SignalHandler *const handler = new SignalHandler(&w);
329 QObject::connect(handler, SIGNAL(int_received()),
331 QObject::connect(handler, SIGNAL(term_received()),
334 qWarning() << "Could not prepare signal handler.";
337 // Run the application
341 #ifndef ENABLE_STACKTRACE
342 } catch (exception& e) {
343 qDebug() << "Exception:" << e.what();
348 // Destroy libsigrokdecode