]> sigrok.org Git - pulseview.git/blob - main.cpp
DecodeSignal: Support unitsize > 1 for logic output
[pulseview.git] / main.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #ifdef ENABLE_DECODE
21 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #endif
23
24 #include <cstdint>
25 #include <fstream>
26 #include <getopt.h>
27 #include <vector>
28
29 #ifdef ENABLE_FLOW
30 #include <gstreamermm.h>
31 #include <libsigrokflow/libsigrokflow.hpp>
32 #endif
33
34 #include <libsigrokcxx/libsigrokcxx.hpp>
35
36 #include <QCheckBox>
37 #include <QDebug>
38 #include <QFile>
39 #include <QFileInfo>
40 #include <QMessageBox>
41 #include <QSettings>
42 #include <QTextStream>
43
44 #include "config.h"
45
46 #ifdef ENABLE_SIGNALS
47 #include "signalhandler.hpp"
48 #endif
49
50 #ifdef ENABLE_STACKTRACE
51 #include <signal.h>
52 #include <boost/stacktrace.hpp>
53 #include <QStandardPaths>
54 #endif
55
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"
64
65 #ifdef ANDROID
66 #include <libsigrokandroidutils/libsigrokandroidutils.h>
67 #include "android/assetreader.hpp"
68 #include "android/loghandler.hpp"
69 #endif
70
71 #ifdef _WIN32
72 #include <QtPlugin>
73 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
74 Q_IMPORT_PLUGIN(QSvgPlugin)
75 #endif
76
77 using std::exception;
78 using std::ifstream;
79 using std::ofstream;
80 using std::shared_ptr;
81 using std::string;
82
83 #if ENABLE_STACKTRACE
84 QString stacktrace_filename;
85
86 void signal_handler(int signum)
87 {
88         ::signal(signum, SIG_DFL);
89         boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
90         ::raise(SIGABRT);
91 }
92
93 void process_stacktrace(QString temp_path)
94 {
95         const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
96
97         ifstream ifs(stacktrace_filename.toLocal8Bit().data());
98         ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
99                 ofstream::out | ofstream::trunc);
100
101         boost::stacktrace::stacktrace st =
102                 boost::stacktrace::stacktrace::from_dump(ifs);
103         ofs << st;
104
105         ofs.close();
106         ifs.close();
107
108         QFile f(stacktrace_outfile);
109         f.open(QFile::ReadOnly | QFile::Text);
110         QTextStream fs(&f);
111         QString stacktrace = fs.readAll();
112         stacktrace = stacktrace.trimmed().replace('\n', "<br />");
113
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() << "---------------------------------------------------------";
119
120         f.close();
121
122         // Remove stack trace so we don't process it again the next time we run
123         QFile::remove(stacktrace_filename.toLocal8Bit().data());
124
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"));
129
130                 QMessageBox msgbox;
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);
137
138                 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
139                         pv::GlobalSettings settings;
140                         settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
141                                 !state); });
142
143                 msgbox.exec();
144         }
145 }
146 #endif
147
148 void usage()
149 {
150         fprintf(stdout,
151                 "Usage:\n"
152                 "  %s [OPTIONS] [FILE]\n"
153                 "\n"
154                 "Help Options:\n"
155                 "  -h, -?, --help                  Show help option\n"
156                 "\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"
166                 "\n", PV_BIN_NAME);
167 }
168
169 int main(int argc, char *argv[])
170 {
171         int ret = 0;
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;
176         bool do_scan = true;
177         bool show_version = false;
178
179 #ifdef ENABLE_FLOW
180         // Initialise gstreamermm. Must be called before any other GLib stuff.
181         Gst::init();
182
183         // Initialize libsigrokflow. Must be called after Gst::init().
184         Srf::init();
185 #endif
186
187         Application a(argc, argv);
188
189 #ifdef ANDROID
190         srau_init_environment();
191         pv::AndroidLogHandler::install_callbacks();
192         pv::AndroidAssetReader asset_reader;
193 #endif
194
195         // Parse arguments
196         while (true) {
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}
209                 };
210
211                 const int c = getopt_long(argc, argv,
212                         "h?VDcl:d:i:s:I:", long_options, nullptr);
213                 if (c == -1)
214                         break;
215
216                 switch (c) {
217                 case 'h':
218                 case '?':
219                         usage();
220                         return 0;
221
222                 case 'V':
223                         show_version = true;
224                         break;
225
226                 case 'l':
227                 {
228                         const int loglevel = atoi(optarg);
229                         if (loglevel < 0 || loglevel > 5) {
230                                 qDebug() << "ERROR: invalid log level spec.";
231                                 break;
232                         }
233                         context->set_log_level(sigrok::LogLevel::get(loglevel));
234
235 #ifdef ENABLE_DECODE
236                         srd_log_loglevel_set(loglevel);
237 #endif
238
239                         if (loglevel >= 5) {
240                                 const QSettings settings;
241                                 qDebug() << "Settings:" << settings.fileName()
242                                         << "format" << settings.format();
243                         }
244                         break;
245                 }
246
247                 case 'd':
248                         driver = optarg;
249                         break;
250
251                 case 'D':
252                         do_scan = false;
253                         break;
254
255                 case 'i':
256                         open_files.emplace_back(optarg);
257                         break;
258
259                 case 's':
260                         open_setup_file = optarg;
261                         break;
262
263                 case 'I':
264                         open_file_format = optarg;
265                         break;
266
267                 case 'c':
268                         restore_sessions = false;
269                         break;
270                 }
271         }
272         argc -= optind;
273         argv += optind;
274
275         for (int i = 0; i < argc; i++)
276                 open_files.emplace_back(argv[i]);
277
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>");
282
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();
290
291         pv::logging.init();
292
293         // Initialise libsigrok
294         context = sigrok::Context::create();
295         pv::Session::sr_context = context;
296
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;
302
303         ::signal(SIGSEGV, &signal_handler);
304         ::signal(SIGABRT, &signal_handler);
305
306         if (QFileInfo::exists(stacktrace_filename))
307                 process_stacktrace(temp_path);
308 #endif
309
310 #ifdef ANDROID
311         context->set_resource_reader(&asset_reader);
312 #endif
313         do {
314
315 #ifdef ENABLE_DECODE
316                 // Initialise libsigrokdecode
317                 if (srd_init(nullptr) != SRD_OK) {
318                         qDebug() << "ERROR: libsigrokdecode init failed.";
319                         break;
320                 }
321
322                 // Load the protocol decoders
323                 srd_decoder_load_all();
324 #endif
325
326 #ifndef ENABLE_STACKTRACE
327                 try {
328 #endif
329
330                 // Create the device manager, initialise the drivers
331                 pv::DeviceManager device_manager(context, driver, do_scan);
332
333                 a.collect_version_info(context);
334                 if (show_version) {
335                         a.print_version_info();
336                 } else {
337                         // Initialise the main window
338                         pv::MainWindow w(device_manager);
339                         w.show();
340
341                         if (restore_sessions)
342                                 w.restore_sessions();
343
344                         if (open_files.empty())
345                                 w.add_default_session();
346                         else
347                                 for (string& open_file : open_files)
348                                         w.add_session_with_file(open_file, open_file_format, open_setup_file);
349
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()));
356                         } else
357                                 qWarning() << "Could not prepare signal handler.";
358 #endif
359
360                         // Run the application
361                         ret = a.exec();
362                 }
363
364 #ifndef ENABLE_STACKTRACE
365                 } catch (exception& e) {
366                         qDebug() << "Exception:" << e.what();
367                 }
368 #endif
369
370 #ifdef ENABLE_DECODE
371                 // Destroy libsigrokdecode
372                 srd_exit();
373 #endif
374
375         } while (false);
376
377         return ret;
378 }