forked from KDAB/GammaRay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprobe.cpp
More file actions
778 lines (662 loc) · 21.6 KB
/
Copy pathprobe.cpp
File metadata and controls
778 lines (662 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
probe.cpp
This file is part of GammaRay, the Qt application inspection and
manipulation tool.
Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Volker Krause <volker.krause@kdab.com>
Author: Stephen Kelly <stephen.kelly@kdab.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//krazy:excludeall=null,captruefalse,staticobjects
#include "probe.h"
#include "mainwindow.h"
#include "objectlistmodel.h"
#include "objecttreemodel.h"
#include "connectionmodel.h"
#include "toolmodel.h"
#include "readorwritelocker.h"
#include "tools/modelinspector/modeltest.h"
#include "hooking/functionoverwriterfactory.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QThread>
#include <QMouseEvent>
#include <QDialog>
#include <QtCore/QTimer>
#include <QApplication>
#include <iostream>
#include <stdio.h>
#ifndef Q_OS_WIN
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#include <assert.h>
#ifdef Q_OS_MAC
#include <dlfcn.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/errno.h>
#endif
#define IF_DEBUG(x)
using namespace GammaRay;
using namespace std;
Probe *Probe::s_instance = 0;
bool functionsOverwritten = false;
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
namespace GammaRay
{
static bool probeConnectCallback(void ** args)
{
QObject *sender = reinterpret_cast<QObject*>(args[0]);
const char *signal = reinterpret_cast<const char*>(args[1]);
QObject *receiver = reinterpret_cast<QObject*>(args[2]);
const char *method = reinterpret_cast<const char*>(args[3]);
const Qt::ConnectionType *type = reinterpret_cast<Qt::ConnectionType*>(args[4]);
Probe::connectionAdded(sender, signal, receiver, method, *type);
return false;
}
static bool probeDisconnectCallback(void ** args)
{
QObject *sender = reinterpret_cast<QObject*>(args[0]);
const char *signal = reinterpret_cast<const char*>(args[1]);
QObject *receiver = reinterpret_cast<QObject*>(args[2]);
const char *method = reinterpret_cast<const char*>(args[3]);
Probe::connectionRemoved(sender, signal, receiver, method);
return false;
}
}
#endif // QT_VERSION
// useful for debugging, dumps the object and all it's parents
// also useable from GDB!
void dumpObject(QObject *obj)
{
if (!obj) {
cout << "QObject(0x0)" << endl;
return;
}
do {
cout << obj->metaObject()->className() << "(" << hex << obj << ")";
obj = obj->parent();
if (obj) {
cout << " <- ";
}
} while(obj);
cout << endl;
}
struct Listener
{
Listener()
: filterThread(0),
trackDestroyed(true)
{}
QThread *filterThread;
bool trackDestroyed;
};
Q_GLOBAL_STATIC(Listener, s_listener)
Q_GLOBAL_STATIC(QVector<QObject*>, s_addedBeforeProbeInsertion)
// ensures proper information is returned by isValidObject by
// locking it in objectAdded/Removed
class ObjectLock : public QReadWriteLock
{
public:
ObjectLock()
: QReadWriteLock(QReadWriteLock::Recursive)
{}
};
Q_GLOBAL_STATIC(ObjectLock, s_lock)
ProbeCreator::ProbeCreator(Type type)
: m_type(type)
{
//push object into the main thread, as windows creates a
//different thread where this runs in
moveToThread(QApplication::instance()->thread());
// delay to foreground thread
QMetaObject::invokeMethod(this, "createProbe", Qt::QueuedConnection);
}
void ProbeCreator::createProbe()
{
QWriteLocker lock(s_lock());
// make sure we are in the ui thread
Q_ASSERT(QThread::currentThread() == qApp->thread());
if (!qApp || Probe::isInitialized()) {
// never create it twice
return;
}
// Exit early instead of asserting in QWidgetPrivate::init()
const QApplication * const qGuiApplication = qApp; // qobject_cast<const QApplication *>(qApp);
if (!qGuiApplication || qGuiApplication->type() == QApplication::Tty) {
cerr << "Unable to attach to a non-GUI application.\n"
<< "Your application needs to use QApplication, "
<< "otherwise GammaRay can not work." << endl;
return;
}
IF_DEBUG(cout << "setting up new probe instance" << endl;)
s_listener()->filterThread = QThread::currentThread();
Q_ASSERT(!Probe::s_instance);
Probe::s_instance = new Probe;
s_listener()->filterThread = 0;
IF_DEBUG(cout << "done setting up new probe instance" << endl;)
Q_ASSERT(Probe::instance());
QMetaObject::invokeMethod(Probe::instance(), "delayedInit", Qt::QueuedConnection);
foreach (QObject *obj, *(s_addedBeforeProbeInsertion())) {
Probe::objectAdded(obj);
}
s_addedBeforeProbeInsertion()->clear();
if (m_type == CreateAndFindExisting) {
Probe::findExistingObjects();
}
deleteLater();
}
Probe::Probe(QObject *parent):
QObject(parent),
m_objectListModel(new ObjectListModel(this)),
m_objectTreeModel(new ObjectTreeModel(this)),
m_connectionModel(new ConnectionModel(this)),
m_toolModel(new ToolModel(this)),
m_window(0),
m_queueTimer(new QTimer(this))
{
Q_ASSERT(thread() == qApp->thread());
IF_DEBUG(cout << "attaching GammaRay probe" << endl;)
if (qgetenv("GAMMARAY_MODELTEST") == "1") {
new ModelTest(m_objectListModel, m_objectListModel);
new ModelTest(m_objectTreeModel, m_objectTreeModel);
new ModelTest(m_connectionModel, m_connectionModel);
new ModelTest(m_toolModel, m_toolModel);
}
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
QInternal::registerCallback(QInternal::ConnectCallback, &GammaRay::probeConnectCallback);
QInternal::registerCallback(QInternal::DisconnectCallback, &GammaRay::probeDisconnectCallback);
#endif
m_queueTimer->setSingleShot(true);
m_queueTimer->setInterval(0);
connect(m_queueTimer, SIGNAL(timeout()),
this, SLOT(queuedObjectsFullyConstructed()));
}
Probe::~Probe()
{
IF_DEBUG(cerr << "detaching GammaRay probe" << endl;)
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
QInternal::unregisterCallback(QInternal::ConnectCallback, &GammaRay::probeConnectCallback);
QInternal::unregisterCallback(QInternal::DisconnectCallback, &GammaRay::probeDisconnectCallback);
#endif
s_instance = 0;
}
void Probe::setWindow(GammaRay::MainWindow *window)
{
m_window = window;
}
GammaRay::MainWindow *Probe::window() const
{
return m_window;
}
Probe *GammaRay::Probe::instance()
{
if (!qApp) {
return NULL;
}
return s_instance;
}
bool Probe::isInitialized()
{
return s_instance && qApp;
}
void Probe::delayedInit()
{
if (qgetenv("GAMMARAY_UNSET_PRELOAD") == "1") {
qputenv("LD_PRELOAD", "");
}
if (qgetenv("GAMMARAY_UNSET_DYLD") == "1") {
qputenv("DYLD_INSERT_LIBRARIES", "");
qputenv("DYLD_FORCE_FLAT_NAMESPACE", "");
}
QCoreApplication::instance()->installEventFilter(s_instance);
IF_DEBUG(cout << "creating GammaRay::MainWindow" << endl;)
s_listener()->filterThread = QThread::currentThread();
GammaRay::MainWindow *window = new GammaRay::MainWindow;
s_listener()->filterThread = 0;
IF_DEBUG(cout << "creation done" << endl;)
window->setAttribute(Qt::WA_DeleteOnClose);
instance()->setWindow(window);
instance()->setParent(window);
window->show();
}
bool Probe::filterObject(QObject *obj)
{
Probe *p = Probe::instance();
Q_ASSERT(p);
if (obj->thread() != p->thread()) {
// shortcut, never filter objects from a different thread
return false;
}
return obj == p || obj == p->window() ||
Util::descendantOf(p, obj) ||
Util::descendantOf(p->window(), obj);
}
QAbstractItemModel *Probe::objectListModel() const
{
return m_objectListModel;
}
QAbstractItemModel *Probe::objectTreeModel() const
{
return m_objectTreeModel;
}
QAbstractItemModel *Probe::connectionModel() const
{
return m_connectionModel;
}
ToolModel *Probe::toolModel() const
{
return m_toolModel;
}
QObject *Probe::probe() const
{
return const_cast<GammaRay::Probe*>(this);
}
bool Probe::isValidObject(QObject *obj) const
{
///TODO: can we somehow assert(s_lock().isLocked()) ?!
return m_validObjects.contains(obj);
}
QReadWriteLock *Probe::objectLock() const
{
return s_lock();
}
void Probe::objectAdded(QObject *obj, bool fromCtor)
{
QWriteLocker lock(s_lock());
if (s_listener()->filterThread == obj->thread()) {
// Ignore
IF_DEBUG(cout
<< "objectAdded Ignore: "
<< hex << obj
<< (fromCtor ? " (from ctor)" : "") << endl;)
return;
} else if (isInitialized()) {
if (filterObject(obj)) {
IF_DEBUG(cout
<< "objectAdded Filter: "
<< hex << obj
<< (fromCtor ? " (from ctor)" : "") << endl;)
return;
} else if (instance()->m_validObjects.contains(obj)) {
// this happens when we get a child event before the objectAdded call from the ctor
// or when we add an item from s_addedBeforeProbeInsertion who got added already
// due to the add-parent-before-child logic
IF_DEBUG(cout
<< "objectAdded Known: "
<< hex << obj
<< (fromCtor ? " (from ctor)" : "") << endl;)
Q_ASSERT(fromCtor || s_addedBeforeProbeInsertion()->contains(obj));
return;
}
// make sure we already know the parent
if (obj->parent() && !instance()->m_validObjects.contains(obj->parent())) {
objectAdded(obj->parent(), fromCtor);
}
Q_ASSERT(!obj->parent() || instance()->m_validObjects.contains(obj->parent()));
instance()->m_validObjects << obj;
if (s_listener()->trackDestroyed) {
// when we did not use a preload variant that
// overwrites qt_removeObject we must track object
// deletion manually
connect(obj, SIGNAL(destroyed(QObject*)),
instance(), SLOT(handleObjectDestroyed(QObject*)),
Qt::DirectConnection);
}
if (!fromCtor && obj->parent() && instance()->m_queuedObjects.contains(obj->parent())) {
// when a child event triggers a call to objectAdded while inside the ctor
// the parent is already tracked but it's call to objectFullyConstructed
// was delayed. hence we must do the same for the child for integrity
fromCtor = true;
}
IF_DEBUG(cout << "objectAdded: " << hex << obj
<< (fromCtor ? " (from ctor)" : "")
<< ", p: " << obj->parent() << endl;)
if (fromCtor) {
Q_ASSERT(!instance()->m_queuedObjects.contains(obj));
instance()->m_queuedObjects << obj;
if (!instance()->m_queueTimer->isActive()) {
// timers must not be started from a different thread
QMetaObject::invokeMethod(instance()->m_queueTimer, "start", Qt::AutoConnection);
}
} else {
instance()->objectFullyConstructed(obj);
}
} else {
IF_DEBUG(cout
<< "objectAdded Before: "
<< hex << obj
<< (fromCtor ? " (from ctor)" : "") << endl;)
s_addedBeforeProbeInsertion()->push_back(obj);
}
}
void Probe::queuedObjectsFullyConstructed()
{
QWriteLocker lock(s_lock());
IF_DEBUG(cout << Q_FUNC_INFO << " " << m_queuedObjects.size() << endl;)
// must be called from the main thread via timeout
Q_ASSERT(QThread::currentThread() == thread());
// when this is called no object must be in the queue twice
// otherwise the cleanup procedures failed
Q_ASSERT(m_queuedObjects.size() == m_queuedObjects.toSet().size());
foreach (QObject *obj, m_queuedObjects) {
objectFullyConstructed(obj);
}
IF_DEBUG(cout << Q_FUNC_INFO << " done" << endl;)
m_queuedObjects.clear();
}
void Probe::objectFullyConstructed(QObject *obj)
{
// must be write locked
Q_ASSERT(!s_lock()->tryLockForRead());
if (!m_validObjects.contains(obj)) {
// deleted already
IF_DEBUG(cout << "stale fully constructed: " << hex << obj << endl;)
return;
} else if (filterObject(obj)) {
// when the call was delayed from the ctor construction,
// the parent might not have been set properly yet. hence
// apply the filter again
m_validObjects.remove(obj);
IF_DEBUG(cout << "now filtered fully constructed: " << hex << obj << endl;)
return;
}
IF_DEBUG(cout << "fully constructed: " << hex << obj << endl;)
// ensure we know the parent already
if (obj->parent() && !m_validObjects.contains(obj->parent())) {
objectAdded(obj->parent());
}
Q_ASSERT(!obj->parent() || m_validObjects.contains(obj->parent()));
m_objectListModel->objectAdded(obj);
m_toolModel->objectAdded(obj);
emit objectCreated(obj);
}
void Probe::objectRemoved(QObject *obj)
{
QWriteLocker lock(s_lock());
if (isInitialized()) {
IF_DEBUG(cout << "object removed:" << hex << obj << " " << obj->parent() << endl;)
bool success = instance()->m_validObjects.remove(obj);
if (!success) {
// object was not tracked by the probe, probably a gammaray object
return;
}
instance()->m_queuedObjects.removeOne(obj);
instance()->m_objectListModel->objectRemoved(obj);
instance()->connectionRemoved(obj, 0, 0, 0);
instance()->connectionRemoved(0, 0, obj, 0);
emit instance()->objectDestroyed(obj);
} else if (s_addedBeforeProbeInsertion()) {
for (QVector<QObject*>::iterator it = s_addedBeforeProbeInsertion()->begin();
it != s_addedBeforeProbeInsertion()->end();) {
if (*it == obj) {
it = s_addedBeforeProbeInsertion()->erase(it);
} else {
++it;
}
}
}
}
void Probe::handleObjectDestroyed(QObject *obj)
{
objectRemoved(obj);
}
void Probe::connectionAdded(QObject *sender, const char *signal, QObject *receiver,
const char *method, Qt::ConnectionType type)
{
if (!isInitialized() || !sender || !receiver ||
s_listener()->filterThread == QThread::currentThread())
{
return;
}
ReadOrWriteLocker lock(s_lock());
if (filterObject(sender) || filterObject(receiver)) {
return;
}
instance()->m_connectionModel->connectionAdded(sender, signal, receiver, method, type);
}
void Probe::connectionRemoved(QObject *sender, const char *signal,
QObject *receiver, const char *method)
{
if (!isInitialized() || !s_listener() ||
s_listener()->filterThread == QThread::currentThread())
{
return;
}
ReadOrWriteLocker lock(s_lock());
if ((sender && filterObject(sender)) || (receiver && filterObject(receiver))) {
return;
}
instance()->m_connectionModel->connectionRemoved(sender, signal, receiver, method);
}
bool Probe::eventFilter(QObject *receiver, QEvent *event)
{
if (s_listener()->filterThread == receiver->thread()) {
return QObject::eventFilter(receiver, event);
}
if (event->type() == QEvent::ChildAdded || event->type() == QEvent::ChildRemoved) {
QChildEvent *childEvent = static_cast<QChildEvent*>(event);
QObject *obj = childEvent->child();
QWriteLocker lock(s_lock());
const bool tracked = m_validObjects.contains(obj);
const bool filtered = filterObject(obj);
IF_DEBUG(cout << "child event: " << hex << obj << ", p: " << obj->parent() << dec
<< ", tracked: " << tracked
<< ", filtered: " << filtered
<< ", type: " << (childEvent->added() ? "added" : "removed") << endl;)
if (!filtered && childEvent->added()) {
if (!tracked) {
// was not tracked before, add to all models
// child added events are sent before qt_addObject is called,
// so we assumes this comes from the ctor
objectAdded(obj, true);
} else if (!m_queuedObjects.contains(obj)) {
// object is known already, just update the position in the tree
// BUT: only when we did not queue this item before
IF_DEBUG(cout << "update pos: " << hex << obj << endl;)
emit objectReparanted(obj);
}
} else if (tracked) {
objectRemoved(obj);
}
}
if (event->type() == QEvent::MouseButtonRelease) {
QMouseEvent *mouseEv = static_cast<QMouseEvent*>(event);
if (mouseEv->button() == Qt::LeftButton &&
mouseEv->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier)) {
QWidget *widget = QApplication::widgetAt(mouseEv->globalPos());
if (widget) {
emit widgetSelected(widget, widget->mapFromGlobal(mouseEv->globalPos()));
}
}
}
// make modal dialogs non-modal so that the gammaray window is still reachable
if (event->type() == QEvent::Show) {
QDialog *dlg = qobject_cast<QDialog*>(receiver);
if (dlg) {
dlg->setWindowModality(Qt::NonModal);
}
}
// we have no preloading hooks, so recover all objects we see
if (s_listener()->trackDestroyed && event->type() != QEvent::ChildAdded &&
event->type() != QEvent::ChildRemoved && !filterObject(receiver)) {
QWriteLocker lock(s_lock());
const bool tracked = m_validObjects.contains(receiver);
if (!tracked) {
objectAdded(receiver);
}
}
return QObject::eventFilter(receiver, event);
}
void Probe::findExistingObjects()
{
addObjectRecursive(QCoreApplication::instance());
foreach (QObject *obj, QApplication::topLevelWidgets()) {
addObjectRecursive(obj);
}
}
void Probe::addObjectRecursive(QObject *obj)
{
if (!obj) {
return;
}
objectRemoved(obj); // in case we find it twice
objectAdded(obj);
foreach (QObject *child, obj->children()) {
addObjectRecursive(child);
}
}
// taken from qobject.cpp
const int gammaray_flagged_locations_count = 2;
static const char *gammaray_flagged_locations[gammaray_flagged_locations_count] = {0};
const char *Probe::connectLocation(const char *member)
{
for (int i = 0; i < gammaray_flagged_locations_count; ++i) {
if (member == gammaray_flagged_locations[i]) {
// signature includes location information after the first null-terminator
const char *location = member + qstrlen(member) + 1;
if (*location != '\0') {
return location;
}
return 0;
}
}
return 0;
}
extern "C" Q_DECL_EXPORT void qt_startup_hook()
{
s_listener()->trackDestroyed = false;
new ProbeCreator(ProbeCreator::CreateOnly);
#if !defined Q_OS_WIN && !defined Q_OS_MAC
if (!functionsOverwritten) {
static void(*next_qt_startup_hook)() = (void (*)()) dlsym(RTLD_NEXT, "qt_startup_hook");
next_qt_startup_hook();
}
#endif
}
extern "C" Q_DECL_EXPORT void qt_addObject(QObject *obj)
{
Probe::objectAdded(obj, true);
#if !defined Q_OS_WIN && !defined Q_OS_MAC
if (!functionsOverwritten) {
static void (*next_qt_addObject)(QObject *obj) =
(void (*)(QObject *obj)) dlsym(RTLD_NEXT, "qt_addObject");
next_qt_addObject(obj);
}
#endif
}
extern "C" Q_DECL_EXPORT void qt_removeObject(QObject *obj)
{
Probe::objectRemoved(obj);
#if !defined Q_OS_WIN && !defined Q_OS_MAC
if (!functionsOverwritten) {
static void (*next_qt_removeObject)(QObject *obj) =
(void (*)(QObject *obj)) dlsym(RTLD_NEXT, "qt_removeObject");
next_qt_removeObject(obj);
}
#endif
}
#ifndef GAMMARAY_UNKNOWN_CXX_MANGLED_NAMES
#ifndef Q_OS_WIN
Q_DECL_EXPORT const char *qFlagLocation(const char *method)
#else
Q_DECL_EXPORT const char *myFlagLocation(const char *method)
#endif
{
static int gammaray_idx = 0;
gammaray_flagged_locations[gammaray_idx] = method;
gammaray_idx = (gammaray_idx+1) % gammaray_flagged_locations_count;
#ifndef Q_OS_WIN
static const char *(*next_qFlagLocation)(const char *method) =
(const char * (*)(const char *method)) dlsym(RTLD_NEXT, "_Z13qFlagLocationPKc");
Q_ASSERT_X(next_qFlagLocation, "",
"Recompile with GAMMARAY_UNKNOWN_CXX_MANGLED_NAMES enabled, "
"your compiler uses an unsupported C++ name mangling scheme");
return next_qFlagLocation(method);
#else
return method;
#endif
}
#endif
void overwriteQtFunctions()
{
functionsOverwritten = true;
AbstractFunctionOverwriter *overwriter = FunctionOverwriterFactory::createFunctionOverwriter();
overwriter->overwriteFunction(QLatin1String("qt_startup_hook"), (void*)qt_startup_hook);
overwriter->overwriteFunction(QLatin1String("qt_addObject"), (void*)qt_addObject);
overwriter->overwriteFunction(QLatin1String("qt_removeObject"), (void*)qt_removeObject);
#if defined(Q_OS_WIN)
#ifdef ARCH_64
#ifdef __MINGW32__
overwriter->overwriteFunction(
QLatin1String("_Z13qFlagLocationPKc"), (void*)myFlagLocation);
#else
overwriter->overwriteFunction(
QLatin1String("?qFlagLocation@@YAPEBDPEBD@Z"), (void*)myFlagLocation);
#endif
#else
# ifdef __MINGW32__
overwriter->overwriteFunction(
QLatin1String("_Z13qFlagLocationPKc"), (void*)myFlagLocation);
# else
overwriter->overwriteFunction(
QLatin1String("?qFlagLocation@@YAPBDPBD@Z"), (void*)myFlagLocation);
# endif
#endif
#endif
}
#ifdef Q_OS_WIN
extern "C" Q_DECL_EXPORT void gammaray_probe_inject();
extern "C" BOOL WINAPI DllMain(HINSTANCE/*hInstance*/, DWORD dwReason, LPVOID/*lpvReserved*/)
{
switch(dwReason) {
case DLL_PROCESS_ATTACH:
{
overwriteQtFunctions();
gammaray_probe_inject();
break;
}
case DLL_PROCESS_DETACH:
{
//Unloading does not work, because we overwrite existing code
exit(-1);
break;
}
};
return TRUE;
}
#endif
extern "C" Q_DECL_EXPORT void gammaray_probe_inject()
{
if (!qApp) {
return;
}
printf("gammaray_probe_inject()\n");
// make it possible to re-attach
new ProbeCreator(ProbeCreator::CreateAndFindExisting);
}
#ifdef Q_OS_MAC
// we need a way to execute some code upon load, so let's abuse
// static initialization
class HitMeBabyOneMoreTime
{
public:
HitMeBabyOneMoreTime()
{
overwriteQtFunctions();
}
};
static HitMeBabyOneMoreTime britney;
#endif
#include "probe.moc"