summaryrefslogtreecommitdiffstats
path: root/src/plugins/tracing/qctflib_p.h
blob: 297d38ee50909a13a25cca0c12c82860fb0b1d43 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QT_CTFLIB_H
#define QT_CTFLIB_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
//

#include <private/qctf_p.h>
#include "qctfplugin_p.h"
#include <qstring.h>
#include <qmutex.h>
#include <qelapsedtimer.h>
#include <qhash.h>
#include <qset.h>
#include <qthreadstorage.h>
#include <qthread.h>
#include <qloggingcategory.h>
#include "qctfserver_p.h"

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(lcDebugTrace)

struct QCtfTracePointPrivate
{
    QString metadata;
    quint32 id = 0;
    quint32 payloadSize = 0;
    bool metadataWritten = false;
};

class QCtfLibImpl : public QCtfLib, public QCtfServer::ServerCallback
{
    struct Session
    {
        QString name;
        QStringList tracepoints;
        bool all = false;
    };
    struct Channel
    {
        char channelName[512];
        QByteArray data;
        quint64 minTimestamp = 0;
        quint64 maxTimestamp = 0;
        quint64 seqnumber = 0;
        QThread *thread = nullptr;
        quint32 threadIndex = 0;
        QByteArray threadName;
        quint32 threadNameLength = 0;
        bool locked = false;
        QCtfLibImpl *impl = nullptr;
        Channel()
        {
            memset(channelName, 0, sizeof(channelName));
        }

        ~Channel();
    };

public:
    QCtfLibImpl();
    ~QCtfLibImpl();

    bool tracepointEnabled(const QCtfTracePointEvent &point) override;
    void doTracepoint(const QCtfTracePointEvent &point, const QByteArray &arr) override;
    bool sessionEnabled() override;
    QCtfTracePointPrivate *initializeTracepoint(const QCtfTracePointEvent &point) override;
    void registerMetadata(const QCtfTraceMetadata &metadata);
    int eventId();
    void shutdown(bool *) override
    {

    }

    static QCtfLib *instance();
    static void cleanup();
private:
    static QCtfLibImpl *s_instance;
    QHash<QString, QCtfTracePointPrivate *> m_eventPrivs;
    void removeChannel(Channel *ch);
    void updateMetadata(const QCtfTracePointEvent &point);
    void writeMetadata(const QString &metadata, bool overwrite = false);
    void clearLocation();
    void handleSessionChange() override;
    void handleStatusChange(QCtfServer::ServerStatus status) override;
    void writeCtfPacket(Channel &ch);
    void buildMetadata();

    static constexpr QUuid s_TraceUuid = QUuid(0x3e589c95, 0xed11, 0xc159, 0x42, 0x02, 0x6a, 0x9b, 0x02, 0x00, 0x12, 0xac);
    static constexpr quint32 s_CtfHeaderMagic = 0xC1FC1FC1;

    QMutex m_mutex;
    QElapsedTimer m_timer;
    QString m_metadata;
    QString m_location;
    Session m_session;
    QHash<QThread*, quint32> m_threadIndices;
    QThreadStorage<Channel> m_threadData;
    QList<Channel *> m_channels;
    QHash<QString, const QCtfTraceMetadata *> m_additionalMetadata;
    QSet<QString> m_newAdditionalMetadata;
    QDateTime m_datetime;
    int m_eventId = 0;
    bool m_streaming = false;
    std::atomic_bool m_sessionChanged = false;
    std::atomic_bool m_serverClosed = false;
    QScopedPointer<QCtfServer> m_server;
    friend struct Channel;
};

QT_END_NAMESPACE

#endif