CTF: Fix minor problems in the implementation
- Check if trace location is writable - Call fclose only if file is opened - Convert metadata to QByteArray in one place. - Add constraint to array operator. Change-Id: Id01998fe8e754dab7a4b7d8ce1361ac822d01390 Reviewed-by: Antti Määttä <antti.maatta@qt.io> (cherry picked from commit 1d0da1d683cc71ea14cfd85f5e9aa227d7f75762) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
967609ad49
commit
78ef28b45f
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
Q_LOGGING_CATEGORY(lcDebugTrace, "qt.core.ctf");
|
Q_LOGGING_CATEGORY(lcDebugTrace, "qt.core.ctf");
|
||||||
|
|
||||||
static const size_t packetHeaderSize = 24 + 6 * 8 + 4;
|
static const size_t packetHeaderSize = 24 + 6 * 8 + 4;
|
||||||
@ -29,6 +31,7 @@ static const size_t traceMetadataSize = sizeof(traceMetadataTemplate);
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
QByteArray &operator<<(QByteArray &arr, T val)
|
QByteArray &operator<<(QByteArray &arr, T val)
|
||||||
{
|
{
|
||||||
|
static_assert(std::is_arithmetic_v<T>);
|
||||||
arr.append((char *)&val, sizeof(val));
|
arr.append((char *)&val, sizeof(val));
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
@ -55,19 +58,28 @@ QCtfLibImpl::QCtfLibImpl()
|
|||||||
qCWarning (lcDebugTrace) << "QTRACE_LOCATION not set";
|
qCWarning (lcDebugTrace) << "QTRACE_LOCATION not set";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the location is writable
|
||||||
FILE *file = nullptr;
|
FILE *file = nullptr;
|
||||||
file = fopen(qPrintable(location + QStringLiteral("/session.json")), "rb");
|
file = fopen(qPrintable(location + "/metadata"_L1), "w+b");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
qCWarning (lcDebugTrace) << "unable to open session file: " << (location + QStringLiteral("/session.json"));
|
qCWarning (lcDebugTrace) << "Unable to write to location";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
const QString filename = location + QStringLiteral("/session.json");
|
||||||
|
file = fopen(qPrintable(filename), "rb");
|
||||||
|
if (!file) {
|
||||||
|
qCWarning (lcDebugTrace) << "unable to open session file: " << filename;
|
||||||
m_location = location;
|
m_location = location;
|
||||||
m_session.tracepoints.append(QStringLiteral("all"));
|
m_session.tracepoints.append(QStringLiteral("all"));
|
||||||
m_session.name = QStringLiteral("default");
|
m_session.name = QStringLiteral("default");
|
||||||
} else {
|
} else {
|
||||||
QByteArray data;
|
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
long pos = ftell(file);
|
long pos = ftell(file);
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
data.resize(pos);
|
QByteArray data(pos, Qt::Uninitialized);
|
||||||
long size = (long)fread(data.data(), pos, 1, file);
|
long size = (long)fread(data.data(), pos, 1, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
if (size != 1)
|
if (size != 1)
|
||||||
@ -104,20 +116,23 @@ QCtfLibImpl::QCtfLibImpl()
|
|||||||
#else
|
#else
|
||||||
metadata.replace(QStringLiteral("$ENDIANNESS"), QStringLiteral("le"));
|
metadata.replace(QStringLiteral("$ENDIANNESS"), QStringLiteral("le"));
|
||||||
#endif
|
#endif
|
||||||
writeMetadata(metadata.toUtf8(), true);
|
writeMetadata(metadata, true);
|
||||||
|
|
||||||
m_timer.start();
|
m_timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QCtfLibImpl::writeMetadata(const QByteArray &data, bool overwrite)
|
void QCtfLibImpl::writeMetadata(const QString &metadata, bool overwrite)
|
||||||
{
|
{
|
||||||
FILE *file = nullptr;
|
FILE *file = nullptr;
|
||||||
file = fopen(qPrintable(m_location + QStringLiteral("/metadata")), overwrite ? "w+b": "ab");
|
file = fopen(qPrintable(m_location + "/metadata"_L1), overwrite ? "w+b": "ab");
|
||||||
if (!file)
|
if (!file)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!overwrite)
|
if (!overwrite)
|
||||||
fputs("\n", file);
|
fputs("\n", file);
|
||||||
|
|
||||||
|
// data contains zero at the end, hence size - 1.
|
||||||
|
const QByteArray data = metadata.toUtf8();
|
||||||
fwrite(data.data(), data.size() - 1, 1, file);
|
fwrite(data.data(), data.size() - 1, 1, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
@ -154,9 +169,9 @@ void QCtfLibImpl::writeCtfPacket(QCtfLibImpl::Channel &ch)
|
|||||||
fwrite(packet.data(), packet.size(), 1, file);
|
fwrite(packet.data(), packet.size(), 1, file);
|
||||||
ch.data.resize(packetSize - packet.size(), 0);
|
ch.data.resize(packetSize - packet.size(), 0);
|
||||||
fwrite(ch.data.data(), ch.data.size(), 1, file);
|
fwrite(ch.data.data(), ch.data.size(), 1, file);
|
||||||
}
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QCtfLibImpl::~QCtfLibImpl()
|
QCtfLibImpl::~QCtfLibImpl()
|
||||||
{
|
{
|
||||||
@ -229,10 +244,10 @@ void QCtfLibImpl::doTracepoint(const QCtfTracePointEvent &point, const QByteArra
|
|||||||
}
|
}
|
||||||
if (m_newAdditionalMetadata.size()) {
|
if (m_newAdditionalMetadata.size()) {
|
||||||
for (const QString &name : m_newAdditionalMetadata)
|
for (const QString &name : m_newAdditionalMetadata)
|
||||||
writeMetadata(m_additionalMetadata[name]->metadata.toUtf8());
|
writeMetadata(m_additionalMetadata[name]->metadata);
|
||||||
m_newAdditionalMetadata.clear();
|
m_newAdditionalMetadata.clear();
|
||||||
}
|
}
|
||||||
writeMetadata(priv->metadata.toUtf8());
|
writeMetadata(priv->metadata);
|
||||||
}
|
}
|
||||||
timestamp = m_timer.nsecsElapsed();
|
timestamp = m_timer.nsecsElapsed();
|
||||||
}
|
}
|
||||||
@ -254,6 +269,7 @@ void QCtfLibImpl::doTracepoint(const QCtfTracePointEvent &point, const QByteArra
|
|||||||
sprintf(ch.channelName, "%s/channel_%d", qPrintable(m_location), m_threadIndices[thread]);
|
sprintf(ch.channelName, "%s/channel_%d", qPrintable(m_location), m_threadIndices[thread]);
|
||||||
FILE *f = nullptr;
|
FILE *f = nullptr;
|
||||||
f = fopen(ch.channelName, "wb");
|
f = fopen(ch.channelName, "wb");
|
||||||
|
if (f)
|
||||||
fclose(f);
|
fclose(f);
|
||||||
ch.minTimestamp = ch.maxTimestamp = timestamp;
|
ch.minTimestamp = ch.maxTimestamp = timestamp;
|
||||||
ch.thread = thread;
|
ch.thread = thread;
|
||||||
|
@ -85,7 +85,7 @@ private:
|
|||||||
static QCtfLibImpl *s_instance;
|
static QCtfLibImpl *s_instance;
|
||||||
QHash<QString, QCtfTracePointPrivate *> m_eventPrivs;
|
QHash<QString, QCtfTracePointPrivate *> m_eventPrivs;
|
||||||
void updateMetadata(const QCtfTracePointEvent &point);
|
void updateMetadata(const QCtfTracePointEvent &point);
|
||||||
void writeMetadata(const QByteArray &data, bool overwrite = false);
|
void writeMetadata(const QString &metadata, bool overwrite = false);
|
||||||
static void writeCtfPacket(Channel &ch);
|
static void writeCtfPacket(Channel &ch);
|
||||||
|
|
||||||
static constexpr QUuid s_TraceUuid = QUuid(0x3e589c95, 0xed11, 0xc159, 0x42, 0x02, 0x6a, 0x9b, 0x02, 0x00, 0x12, 0xac);
|
static constexpr QUuid s_TraceUuid = QUuid(0x3e589c95, 0xed11, 0xc159, 0x42, 0x02, 0x6a, 0x9b, 0x02, 0x00, 0x12, 0xac);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user