]> git.cworth.org Git - apitrace/blob - gui/traceprocess.cpp
Put glxtrace.so also in the wrappers directory.
[apitrace] / gui / traceprocess.cpp
1 #include "traceprocess.h"
2
3 #include <QDebug>
4 #include <QDir>
5 #include <QFile>
6 #include <QFileInfo>
7
8 static QString
9 findPreloader()
10 {
11     QString libPath;
12     QFileInfo fi;
13
14 #ifdef APITRACE_BINARY_DIR
15     libPath = QString::fromLatin1(APITRACE_BINARY_DIR "/wrappers/glxtrace.so");
16     fi = QFileInfo(libPath);
17     if (fi.exists())
18         return libPath;
19 #endif
20
21 #ifdef APITRACE_WRAPPER_INSTALL_DIR
22     libPath = QString::fromLatin1(APITRACE_WRAPPER_INSTALL_DIR "/glxtrace.so");
23     fi = QFileInfo(libPath);
24     if (fi.exists())
25         return libPath;
26 #endif
27
28     return QString();
29 }
30
31 TraceProcess::TraceProcess(QObject *parent)
32     : QObject(parent),
33       m_canTrace(true)
34 {
35     m_process = new QProcess(this);
36
37     connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
38             this, SLOT(traceFinished()));
39     connect(m_process, SIGNAL(error(QProcess::ProcessError)),
40             this, SLOT(traceError(QProcess::ProcessError)));
41
42 #ifdef Q_OS_WIN
43     qWarning()<<"Windows tracing isn't supported right now!";
44     m_canTrace = false;
45 #else
46     QString var = QLatin1String("LD_PRELOAD");
47     QString libPath = findPreloader();
48     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
49
50     if (libPath.isEmpty()) {
51         m_canTrace = false;
52     }
53
54     env.insert("LD_PRELOAD", libPath);
55     qputenv("LD_PRELOAD", env.value("LD_PRELOAD").toLatin1());
56
57     m_process->setProcessEnvironment(env);
58 #endif
59 }
60
61 TraceProcess::~TraceProcess()
62 {
63 }
64
65 void TraceProcess::setExecutablePath(const QString &str)
66 {
67     m_execPath = str;
68
69     QFileInfo fi(m_execPath);
70
71     m_process->setWorkingDirectory(fi.absolutePath());
72
73     QString format = QString::fromLatin1("%1%2%3.trace");
74
75     m_tracePath = format
76                   .arg(fi.absolutePath())
77                   .arg(QDir::separator())
78                   .arg(fi.baseName());
79
80     int i = 1;
81     while (QFile::exists(m_tracePath)) {
82         QString format = QString::fromLatin1("%1%2%3.%4.trace");
83         m_tracePath = format
84                       .arg(fi.absolutePath())
85                       .arg(QDir::separator())
86                       .arg(fi.baseName())
87                       .arg(i++);
88     }
89 }
90
91 QString TraceProcess::executablePath() const
92 {
93     return m_execPath;
94 }
95
96 void TraceProcess::setArguments(const QStringList &args)
97 {
98     m_args = args;
99 }
100
101 QStringList TraceProcess::arguments() const
102 {
103     return m_args;
104 }
105
106 void TraceProcess::traceFinished()
107 {
108 #if 0
109     qDebug()<<"trace finished on " << m_tracePath;
110     qDebug()<<"\terr = "<<m_process->readAllStandardError();
111     qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
112 #endif
113     emit tracedFile(m_tracePath);
114 }
115
116 void TraceProcess::traceError(QProcess::ProcessError err)
117 {
118 #if 1
119     qDebug()<<"trace error = "<<m_tracePath;
120     qDebug()<<"\terr = "<<m_process->readAllStandardError();
121     qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
122 #endif
123     emit error(m_process->readAllStandardError());
124 }
125
126
127 void TraceProcess::start()
128 {
129     m_process->start(m_execPath, m_args);
130 }
131
132 bool TraceProcess::canTrace() const
133 {
134     return m_canTrace;
135 }
136
137 #include "traceprocess.moc"