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"
63 #include "pv/data/segment.hpp"
66 #include <libsigrokandroidutils/libsigrokandroidutils.h>
67 #include "android/assetreader.hpp"
68 #include "android/loghandler.hpp"
74 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
75 Q_IMPORT_PLUGIN(QSvgPlugin)
82 using std::shared_ptr;
86 QString stacktrace_filename;
88 void signal_handler(int signum)
90 ::signal(signum, SIG_DFL);
91 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
95 void process_stacktrace(QString temp_path)
97 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
99 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
100 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
101 ofstream::out | ofstream::trunc);
103 boost::stacktrace::stacktrace st =
104 boost::stacktrace::stacktrace::from_dump(ifs);
110 QFile f(stacktrace_outfile);
111 f.open(QFile::ReadOnly | QFile::Text);
113 QString stacktrace = fs.readAll();
114 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
116 qDebug() << QObject::tr("Stack trace of previous crash:");
117 qDebug() << "---------------------------------------------------------";
118 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
119 qDebug() << stacktrace.toLocal8Bit().data();
120 qDebug() << "---------------------------------------------------------";
124 // Remove stack trace so we don't process it again the next time we run
125 QFile::remove(stacktrace_filename.toLocal8Bit().data());
127 // Show notification dialog if permitted
128 pv::GlobalSettings settings;
129 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
130 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
133 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
134 "A human-readable form has been saved to disk and was written to " \
135 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
136 msgbox.setIcon(QMessageBox::Icon::Information);
137 msgbox.addButton(QMessageBox::Ok);
138 msgbox.setCheckBox(cb);
140 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
141 pv::GlobalSettings settings;
142 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
154 " %s [OPTIONS] [FILE]\n"
157 " -h, -?, --help Show help option\n"
159 "Application Options:\n"
160 " -V, --version Show release version\n"
161 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
162 " -d, --driver Specify the device driver to use\n"
163 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
164 " -i, --input-file Load input from file\n"
165 " -s, --settings Load PulseView session setup from file\n"
166 " -I, --input-format Input format\n"
167 " -c, --clean Don't restore previous sessions on startup\n"
171 int main(int argc, char *argv[])
174 shared_ptr<sigrok::Context> context;
175 string open_file_format, open_setup_file, driver;
176 vector<string> open_files;
177 bool restore_sessions = true;
179 bool show_version = false;
182 // Initialise gstreamermm. Must be called before any other GLib stuff.
185 // Initialize libsigrokflow. Must be called after Gst::init().
189 Application a(argc, argv);
192 srau_init_environment();
193 pv::AndroidLogHandler::install_callbacks();
194 pv::AndroidAssetReader asset_reader;
199 static const struct option long_options[] = {
200 {"help", no_argument, nullptr, 'h'},
201 {"version", no_argument, nullptr, 'V'},
202 {"loglevel", required_argument, nullptr, 'l'},
203 {"driver", required_argument, nullptr, 'd'},
204 {"dont-scan", no_argument, nullptr, 'D'},
205 {"input-file", required_argument, nullptr, 'i'},
206 {"settings", required_argument, nullptr, 's'},
207 {"input-format", required_argument, nullptr, 'I'},
208 {"clean", no_argument, nullptr, 'c'},
209 {"log-to-stdout", no_argument, nullptr, 's'},
210 {nullptr, 0, nullptr, 0}
213 const int c = getopt_long(argc, argv,
214 "h?VDcl:d:i:s:I:", long_options, nullptr);
230 const int loglevel = atoi(optarg);
231 if (loglevel < 0 || loglevel > 5) {
232 qDebug() << "ERROR: invalid log level spec.";
235 context->set_log_level(sigrok::LogLevel::get(loglevel));
238 srd_log_loglevel_set(loglevel);
242 const QSettings settings;
243 qDebug() << "Settings:" << settings.fileName()
244 << "format" << settings.format();
258 open_files.emplace_back(optarg);
262 open_setup_file = optarg;
266 open_file_format = optarg;
270 restore_sessions = false;
277 for (int i = 0; i < argc; i++)
278 open_files.emplace_back(argv[i]);
280 qRegisterMetaType<uint64_t>("uint64_t");
281 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
282 qRegisterMetaType<SharedPtrToSegment>("SharedPtrToSegment");
283 qRegisterMetaType<shared_ptr<pv::data::SignalBase>>("shared_ptr<SignalBase>");
285 // Prepare the global settings since logging needs them early on
286 pv::GlobalSettings settings;
287 settings.add_change_handler(&a); // Only the application object can't register itself
288 settings.save_internal_defaults();
289 settings.set_defaults_where_needed();
290 settings.apply_language();
291 settings.apply_theme();
295 // Initialise libsigrok
296 context = sigrok::Context::create();
297 pv::Session::sr_context = context;
299 #if ENABLE_STACKTRACE
300 QString temp_path = QStandardPaths::standardLocations(
301 QStandardPaths::TempLocation).at(0);
302 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
303 qDebug() << "Stack trace file is" << stacktrace_filename;
305 ::signal(SIGSEGV, &signal_handler);
306 ::signal(SIGABRT, &signal_handler);
308 if (QFileInfo::exists(stacktrace_filename))
309 process_stacktrace(temp_path);
313 context->set_resource_reader(&asset_reader);
318 // Initialise libsigrokdecode
319 if (srd_init(nullptr) != SRD_OK) {
320 qDebug() << "ERROR: libsigrokdecode init failed.";
324 // Load the protocol decoders
325 srd_decoder_load_all();
328 #ifndef ENABLE_STACKTRACE
332 // Create the device manager, initialise the drivers
333 pv::DeviceManager device_manager(context, driver, do_scan);
335 a.collect_version_info(device_manager);
337 a.print_version_info();
339 // Initialise the main window
340 pv::MainWindow w(device_manager);
343 if (restore_sessions)
344 w.restore_sessions();
346 if (open_files.empty())
347 w.add_default_session();
349 for (string& open_file : open_files)
350 w.add_session_with_file(open_file, open_file_format, open_setup_file);
352 #ifdef ENABLE_SIGNALS
353 if (SignalHandler::prepare_signals()) {
354 SignalHandler *const handler = new SignalHandler(&w);
355 QObject::connect(handler, SIGNAL(int_received()), &w, SLOT(close()));
356 QObject::connect(handler, SIGNAL(term_received()), &w, SLOT(close()));
357 QObject::connect(handler, SIGNAL(usr1_received()), &w, SLOT(on_run_stop_clicked()));
359 qWarning() << "Could not prepare signal handler.";
362 // Run the application
366 #ifndef ENABLE_STACKTRACE
367 } catch (exception& e) {
368 qDebug() << "Exception:" << e.what();
373 // Destroy libsigrokdecode