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>
40 #include "signalhandler.hpp"
43 #ifdef ENABLE_STACKTRACE
45 #include <boost/stacktrace.hpp>
46 #include <QStandardPaths>
49 #include "pv/application.hpp"
50 #include "pv/devicemanager.hpp"
51 #include "pv/globalsettings.hpp"
52 #include "pv/logging.hpp"
53 #include "pv/mainwindow.hpp"
54 #include "pv/session.hpp"
57 #include <libsigrokandroidutils/libsigrokandroidutils.h>
58 #include "android/assetreader.hpp"
59 #include "android/loghandler.hpp"
66 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
67 Q_IMPORT_PLUGIN(QSvgPlugin)
73 using std::shared_ptr;
77 QString stacktrace_filename;
79 void signal_handler(int signum)
81 ::signal(signum, SIG_DFL);
82 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
86 void process_stacktrace(QString temp_path)
88 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
90 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
91 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
92 ofstream::out | ofstream::trunc);
94 boost::stacktrace::stacktrace st =
95 boost::stacktrace::stacktrace::from_dump(ifs);
101 QFile f(stacktrace_outfile);
102 f.open(QFile::ReadOnly | QFile::Text);
104 QString stacktrace = fs.readAll();
105 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
107 qDebug() << QObject::tr("Stack trace of previous crash:");
108 qDebug() << "---------------------------------------------------------";
109 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
110 qDebug() << stacktrace.toLocal8Bit().data();
111 qDebug() << "---------------------------------------------------------";
115 // Remove stack trace so we don't process it again the next time we run
116 QFile::remove(stacktrace_filename.toLocal8Bit().data());
118 // Show notification dialog if permitted
119 pv::GlobalSettings settings;
120 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
121 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
124 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
125 "A human-readable form has been saved to disk and was written to " \
126 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
127 msgbox.setIcon(QMessageBox::Icon::Information);
128 msgbox.addButton(QMessageBox::Ok);
129 msgbox.setCheckBox(cb);
131 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
132 pv::GlobalSettings settings;
133 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
145 " %s [OPTIONS] [FILE]\n"
148 " -h, -?, --help Show help option\n"
150 "Application Options:\n"
151 " -V, --version Show release version\n"
152 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
153 " -d, --driver Specify the device driver to use\n"
154 " -D, --no-scan Don't auto-scan for devices, use -d spec only\n"
155 " -i, --input-file Load input from file\n"
156 " -I, --input-format Input format\n"
157 " -c, --clean Don't restore previous sessions on startup\n"
158 " -s, --log-to-stdout Don't use logging, output to stdout instead\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 do_logging = true;
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 {"no-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?VDcsl:d:i:I:", long_options, nullptr);
207 // Print version info
208 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
213 const int loglevel = atoi(optarg);
214 if (loglevel < 0 || loglevel > 5) {
215 qDebug() << "ERROR: invalid log level spec.";
218 context->set_log_level(sigrok::LogLevel::get(loglevel));
221 srd_log_loglevel_set(loglevel);
225 const QSettings settings;
226 qDebug() << "Settings:" << settings.fileName()
227 << "format" << settings.format();
241 open_files.push_back(optarg);
245 open_file_format = optarg;
249 restore_sessions = false;
260 for (int i = 0; i < argc; i++)
261 open_files.push_back(argv[i]);
263 // Prepare the global settings since logging needs them early on
264 pv::GlobalSettings settings;
265 settings.set_defaults_where_needed();
270 // Initialise libsigrok
271 context = sigrok::Context::create();
272 pv::Session::sr_context = context;
274 #if ENABLE_STACKTRACE
275 QString temp_path = QStandardPaths::standardLocations(
276 QStandardPaths::TempLocation).at(0);
277 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
278 qDebug() << "Stack trace file is" << stacktrace_filename;
280 ::signal(SIGSEGV, &signal_handler);
281 ::signal(SIGABRT, &signal_handler);
283 if (QFileInfo::exists(stacktrace_filename))
284 process_stacktrace(temp_path);
288 context->set_resource_reader(&asset_reader);
293 // Initialise libsigrokdecode
294 if (srd_init(nullptr) != SRD_OK) {
295 qDebug() << "ERROR: libsigrokdecode init failed.";
299 // Load the protocol decoders
300 srd_decoder_load_all();
303 #ifndef ENABLE_STACKTRACE
307 // Create the device manager, initialise the drivers
308 pv::DeviceManager device_manager(context, driver, do_scan);
310 // Initialise the main window
311 pv::MainWindow w(device_manager);
314 if (restore_sessions)
315 w.restore_sessions();
317 if (open_files.empty())
318 w.add_default_session();
320 for (string open_file : open_files)
321 w.add_session_with_file(open_file, open_file_format);
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