4 This file is part of GammaRay, the Qt application inspection and
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "config-gammaray.h"
25 #include "config-gammaray-version.h"
26 #include "probefinder.h"
27 #include "injector/injectorfactory.h"
28 #include "attach/attachdialog.h"
30 #include <QApplication>
32 #include <QStringList>
34 using namespace GammaRay
;
36 QTextStream
out(stdout
);
37 QTextStream
err(stderr
);
39 static void usage(const char *argv0
)
41 out
<< "Usage: " << argv0
42 << " [--injector <injector>] [--pid <pid> | <application> <args>]" << endl
;
44 out
<< "Inspect runtime internals of a Qt-application, such as:" << endl
;
45 out
<< " QObject tree, properties, signal/slots, widgets, models," << endl
;
46 out
<< " graphics views, javascript debugger, resources," << endl
;
47 out
<< " state machines, meta types, fonts, codecs, text documents" << endl
;
49 out
<< "Options:" << endl
;
50 out
<< " -i, --injector <injector>\tset injection type, possible values:" << endl
;
51 out
<< " \t" << InjectorFactory::availableInjectors().join(", ")
53 out
<< " -p, --pid <pid> \tattach to running Qt application" << endl
;
54 out
<< " -h, --help \tprint program help and exit" << endl
;
55 out
<< " -v, --version \tprint program version and exit" << endl
;
57 << "When run without any options, " << argv0
<< " will present a list of running\n"
58 << "Qt-applications from which you can attach the selected injector. Else,\n"
59 << "you can attach to a running process by specifying its pid, or you can\n"
60 << "start a new Qt-application by specifying its name (and optional arguments)."
64 int main(int argc
, char **argv
)
66 QApplication
app(argc
, argv
);
67 QStringList args
= app
.arguments();
68 args
.takeFirst(); // that's us
72 while (!args
.isEmpty() && args
.first().startsWith('-')) {
73 const QString arg
= args
.takeFirst();
74 if ((arg
== QLatin1String("-i") || arg
== QLatin1String("--injector")) && !args
.isEmpty()) {
75 injectorType
= args
.takeFirst();
78 if ((arg
== QLatin1String("-p") || arg
== QLatin1String("--pid")) && !args
.isEmpty()) {
79 pid
= args
.takeFirst().toInt();
82 if (arg
== QLatin1String("-h") || arg
== QLatin1String("--help")) {
86 if (arg
== QLatin1String("-v") || arg
== QLatin1String("--version")) {
87 out
<< PROGRAM_NAME
<< " version " << GAMMARAY_VERSION_STRING
<< endl
;
88 out
<< "Copyright (C) 2010-2011 Klaralvdalens Datakonsult AB, "
89 << "a KDAB Group company, info@kdab.com" << endl
;
92 if (arg
== QLatin1String("-filtertest")) {
93 qputenv("GAMMARAY_TEST_FILTER", "1");
95 if (arg
== QLatin1String("-unittest")) {
96 qputenv("GAMMARAY_UNITTEST", "1");
98 if (arg
== QLatin1String("-modeltest")) {
99 qputenv("GAMMARAY_MODELTEST", "1");
103 if (args
.isEmpty() && pid
<= 0) {
105 if (dialog
.exec() == QDialog::Accepted
) {
107 pid
= dialog
.pid().toInt(&ok
);
116 const QString probeDll
= ProbeFinder::findProbe(QLatin1String("gammaray_probe"));
117 AbstractInjector::Ptr injector
;
118 if (injectorType
.isEmpty()) {
120 injector
= InjectorFactory::defaultInjectorForAttach();
122 injector
= InjectorFactory::defaultInjectorForLaunch();
125 injector
= InjectorFactory::createInjector(injectorType
);
130 if (!injector
->attach(pid
, probeDll
, QLatin1String("gammaray_probe_inject"))) {
131 err
<< "Unable to attach injector " << injector
->name() << endl
;
132 err
<< "Exit code: " << injector
->exitCode() << endl
;
133 if (!injector
->errorString().isEmpty()) {
134 err
<< "Error: " << injector
->errorString() << endl
;
141 if (!injector
->launch(args
, probeDll
, QLatin1String("gammaray_probe_inject"))) {
142 err
<< "Failed to launch injector " << injector
->name() << endl
;
143 err
<< "Exit code: " << injector
->exitCode() << endl
;
144 if (!injector
->errorString().isEmpty()) {
145 err
<< "Error: " << injector
->errorString() << endl
;
149 return injector
->exitCode();
154 if (injectorType
.isEmpty()) {
156 #if defined(Q_OS_WIN)
157 err
<< "Sorry, but at this time there is no attach injector on the Windows platform" << endl
;
158 err
<< "Only the launch injector windll is available on Windows" << endl
;
160 err
<< "Uh-oh, there is no default attach injector" << endl
;
163 err
<< "Uh-oh, there is no default launch injector" << endl
;
166 err
<< "Injector " << injectorType
<< " not found." << endl
;