Imported Upstream version 1.0.1
[gammaray-debian.git] / main.cpp
blob26e3d40e487a07ec7bcc73ff180c9b2e97f1d052
1 /*
2 main.cpp
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
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>
31 #include <QDebug>
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;
43 out << "" << 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;
48 out << "" << endl;
49 out << "Options:" << endl;
50 out << " -i, --injector <injector>\tset injection type, possible values:" << endl;
51 out << " \t" << InjectorFactory::availableInjectors().join(", ")
52 << endl;
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;
56 out << 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)."
61 << endl;
64 int main(int argc, char **argv)
66 QApplication app(argc, argv);
67 QStringList args = app.arguments();
68 args.takeFirst(); // that's us
70 QString injectorType;
71 int pid = -1;
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();
76 continue;
78 if ((arg == QLatin1String("-p") || arg == QLatin1String("--pid")) && !args.isEmpty()) {
79 pid = args.takeFirst().toInt();
80 continue;
82 if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
83 usage(argv[0]);
84 return 0;
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;
90 return 0;
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) {
104 AttachDialog dialog;
105 if (dialog.exec() == QDialog::Accepted) {
106 bool ok;
107 pid = dialog.pid().toInt(&ok);
108 if (!ok) {
109 return 0;
111 } else {
112 return 0;
116 const QString probeDll = ProbeFinder::findProbe(QLatin1String("gammaray_probe"));
117 AbstractInjector::Ptr injector;
118 if (injectorType.isEmpty()) {
119 if (pid > 0) {
120 injector = InjectorFactory::defaultInjectorForAttach();
121 } else {
122 injector = InjectorFactory::defaultInjectorForLaunch();
124 } else {
125 injector = InjectorFactory::createInjector(injectorType);
128 if (injector) {
129 if (pid > 0) {
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;
136 return 1;
137 } else {
138 return 0;
140 } else {
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;
147 return 1;
149 return injector->exitCode();
151 return 1;
154 if (injectorType.isEmpty()) {
155 if (pid > 0) {
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;
159 #else
160 err << "Uh-oh, there is no default attach injector" << endl;
161 #endif
162 } else {
163 err << "Uh-oh, there is no default launch injector" << endl;
165 } else {
166 err << "Injector " << injectorType << " not found." << endl;
168 return 1;