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"
73 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin
)
74 Q_IMPORT_PLUGIN(QSvgPlugin
)
80 using std::shared_ptr
;
84 QString stacktrace_filename
;
86 void signal_handler(int signum
)
88 ::signal(signum
, SIG_DFL
);
89 boost::stacktrace::safe_dump_to(stacktrace_filename
.toLocal8Bit().data());
93 void process_stacktrace(QString temp_path
)
95 const QString stacktrace_outfile
= temp_path
+ "/pv_stacktrace.txt";
97 ifstream
ifs(stacktrace_filename
.toLocal8Bit().data());
98 ofstream
ofs(stacktrace_outfile
.toLocal8Bit().data(),
99 ofstream::out
| ofstream::trunc
);
101 boost::stacktrace::stacktrace st
=
102 boost::stacktrace::stacktrace::from_dump(ifs
);
108 QFile
f(stacktrace_outfile
);
109 f
.open(QFile::ReadOnly
| QFile::Text
);
111 QString stacktrace
= fs
.readAll();
112 stacktrace
= stacktrace
.trimmed().replace('\n', "<br />");
114 qDebug() << QObject::tr("Stack trace of previous crash:");
115 qDebug() << "---------------------------------------------------------";
116 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
117 qDebug() << stacktrace
.toLocal8Bit().data();
118 qDebug() << "---------------------------------------------------------";
122 // Remove stack trace so we don't process it again the next time we run
123 QFile::remove(stacktrace_filename
.toLocal8Bit().data());
125 // Show notification dialog if permitted
126 pv::GlobalSettings settings
;
127 if (settings
.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace
).toBool()) {
128 QCheckBox
*cb
= new QCheckBox(QObject::tr("Don't show this message again"));
131 msgbox
.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
132 "A human-readable form has been saved to disk and was written to " \
133 "the log. You may access it from the settings dialog.").arg(PV_TITLE
));
134 msgbox
.setIcon(QMessageBox::Icon::Information
);
135 msgbox
.addButton(QMessageBox::Ok
);
136 msgbox
.setCheckBox(cb
);
138 QObject::connect(cb
, &QCheckBox::stateChanged
, [](int state
){
139 pv::GlobalSettings settings
;
140 settings
.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace
,
152 " %s [OPTIONS] [FILE]\n"
155 " -h, -?, --help Show help option\n"
157 "Application Options:\n"
158 " -V, --version Show release version\n"
159 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
160 " -d, --driver Specify the device driver to use\n"
161 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
162 " -i, --input-file Load input from file\n"
163 " -s, --settings Load PulseView session setup from file\n"
164 " -I, --input-format Input format\n"
165 " -c, --clean Don't restore previous sessions on startup\n"
169 int main(int argc
, char *argv
[])
172 shared_ptr
<sigrok::Context
> context
;
173 string open_file_format
, open_setup_file
, driver
;
174 vector
<string
> open_files
;
175 bool restore_sessions
= true;
177 bool show_version
= false;
180 // Initialise gstreamermm. Must be called before any other GLib stuff.
183 // Initialize libsigrokflow. Must be called after Gst::init().
187 Application
a(argc
, argv
);
190 srau_init_environment();
191 pv::AndroidLogHandler::install_callbacks();
192 pv::AndroidAssetReader asset_reader
;
197 static const struct option long_options
[] = {
198 {"help", no_argument
, nullptr, 'h'},
199 {"version", no_argument
, nullptr, 'V'},
200 {"loglevel", required_argument
, nullptr, 'l'},
201 {"driver", required_argument
, nullptr, 'd'},
202 {"dont-scan", no_argument
, nullptr, 'D'},
203 {"input-file", required_argument
, nullptr, 'i'},
204 {"settings", required_argument
, nullptr, 's'},
205 {"input-format", required_argument
, nullptr, 'I'},
206 {"clean", no_argument
, nullptr, 'c'},
207 {"log-to-stdout", no_argument
, nullptr, 's'},
208 {nullptr, 0, nullptr, 0}
211 const int c
= getopt_long(argc
, argv
,
212 "h?VDcl:d:i:s:I:", long_options
, nullptr);
228 const int loglevel
= atoi(optarg
);
229 if (loglevel
< 0 || loglevel
> 5) {
230 qDebug() << "ERROR: invalid log level spec.";
233 context
->set_log_level(sigrok::LogLevel::get(loglevel
));
236 srd_log_loglevel_set(loglevel
);
240 const QSettings settings
;
241 qDebug() << "Settings:" << settings
.fileName()
242 << "format" << settings
.format();
256 open_files
.emplace_back(optarg
);
260 open_setup_file
= optarg
;
264 open_file_format
= optarg
;
268 restore_sessions
= false;
275 for (int i
= 0; i
< argc
; i
++)
276 open_files
.emplace_back(argv
[i
]);
278 qRegisterMetaType
<uint64_t>("uint64_t");
279 qRegisterMetaType
<pv::util::Timestamp
>("util::Timestamp");
280 qRegisterMetaType
<SharedPtrToSegment
>("SharedPtrToSegment");
281 qRegisterMetaType
<shared_ptr
<pv::data::SignalBase
>>("shared_ptr<SignalBase>");
283 // Prepare the global settings since logging needs them early on
284 pv::GlobalSettings settings
;
285 settings
.add_change_handler(&a
); // Only the application object can't register itself
286 settings
.save_internal_defaults();
287 settings
.set_defaults_where_needed();
288 settings
.apply_language();
289 settings
.apply_theme();
293 // Initialise libsigrok
294 context
= sigrok::Context::create();
295 pv::Session::sr_context
= context
;
297 #if ENABLE_STACKTRACE
298 QString temp_path
= QStandardPaths::standardLocations(
299 QStandardPaths::TempLocation
).at(0);
300 stacktrace_filename
= temp_path
+ "/pv_stacktrace.dmp";
301 qDebug() << "Stack trace file is" << stacktrace_filename
;
303 ::signal(SIGSEGV
, &signal_handler
);
304 ::signal(SIGABRT
, &signal_handler
);
306 if (QFileInfo::exists(stacktrace_filename
))
307 process_stacktrace(temp_path
);
311 context
->set_resource_reader(&asset_reader
);
316 // Initialise libsigrokdecode
317 if (srd_init(nullptr) != SRD_OK
) {
318 qDebug() << "ERROR: libsigrokdecode init failed.";
322 // Load the protocol decoders
323 srd_decoder_load_all();
326 #ifndef ENABLE_STACKTRACE
330 // Create the device manager, initialise the drivers
331 pv::DeviceManager
device_manager(context
, driver
, do_scan
);
333 a
.collect_version_info(context
);
335 a
.print_version_info();
337 // Initialise the main window
338 pv::MainWindow
w(device_manager
);
341 if (restore_sessions
)
342 w
.restore_sessions();
344 if (open_files
.empty())
345 w
.add_default_session();
347 for (string
& open_file
: open_files
)
348 w
.add_session_with_file(open_file
, open_file_format
, open_setup_file
);
350 #ifdef ENABLE_SIGNALS
351 if (SignalHandler::prepare_signals()) {
352 SignalHandler
*const handler
= new SignalHandler(&w
);
353 QObject::connect(handler
, SIGNAL(int_received()), &w
, SLOT(close()));
354 QObject::connect(handler
, SIGNAL(term_received()), &w
, SLOT(close()));
355 QObject::connect(handler
, SIGNAL(usr1_received()), &w
, SLOT(on_run_stop_clicked()));
357 qWarning() << "Could not prepare signal handler.";
360 // Run the application
364 #ifndef ENABLE_STACKTRACE
365 } catch (exception
& e
) {
366 qDebug() << "Exception:" << e
.what();
371 // Destroy libsigrokdecode