File: OutputDebugStringThread.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (159 lines) | stat: -rw-r--r-- 4,444 bytes parent folder | download
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
#include "OutputDebugStringThread.h"
#include "file_logger.h"
#include "event_notifier.h"

wxDEFINE_EVENT(wxEVT_OUTPUT_DEBUG_STRING, clCommandEvent);

OutputDebugStringThread::OutputDebugStringThread()
    : wxThread(wxTHREAD_JOINABLE)
#ifdef __WXMSW__
    , m_hDBWinMutex(INVALID_HANDLE_VALUE)
    , m_hDBMonBuffer(INVALID_HANDLE_VALUE)
    , m_hEventBufferReady(INVALID_HANDLE_VALUE)
    , m_hEventDataReady(INVALID_HANDLE_VALUE)
    , m_pDBBuffer(NULL)
#endif
    , m_collecting(false)
{
#ifdef __WXMSW__
    SetLastError(0);

    // Mutex: DBWin
    // ---------------------------------------------------------
    m_hDBWinMutex = ::OpenMutex(MUTEX_ALL_ACCESS, FALSE, L"DBWinMutex");
    
    if(m_hDBWinMutex == NULL) {
        clWARNING() << "Failed to open mutex: 'DBWinMutex'." << GetLastError() << clEndl;
        return;
    }

    m_hEventBufferReady = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, L"DBWIN_BUFFER_READY");

    if(m_hEventBufferReady == NULL) {
        m_hEventBufferReady = ::CreateEvent(NULL,
            FALSE, // auto-reset
            TRUE,  // initial state: signaled
            L"DBWIN_BUFFER_READY");

        if(m_hEventBufferReady == NULL) {
            clWARNING() << "Failed to create event: 'DBWIN_BUFFER_READY'." << GetLastError() << clEndl;
            return;
        }
    }
    // Event: data ready
    // ---------------------------------------------------------
    m_hEventDataReady = ::OpenEvent(SYNCHRONIZE, FALSE, L"DBWIN_DATA_READY");

    if(m_hEventDataReady == NULL) {
        m_hEventDataReady = ::CreateEvent(NULL,
            FALSE, // auto-reset
            FALSE, // initial state: nonsignaled
            L"DBWIN_DATA_READY");

        if(m_hEventDataReady == NULL) {
            clWARNING() << "Failed to create event: 'DBWIN_DATA_READY'." << GetLastError() << clEndl;
            return;
        }
    }

    // Shared memory
    // ---------------------------------------------------------
    wxString DBWIN_BUFFER = L"DBWIN_BUFFER";
    m_hDBMonBuffer = ::OpenFileMapping(FILE_MAP_READ, FALSE, DBWIN_BUFFER);

    if(m_hDBMonBuffer == NULL) {
        m_hDBMonBuffer = ::CreateFileMapping(
            INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(struct dbwin_buffer), DBWIN_BUFFER);

        if(m_hDBMonBuffer == NULL) {
            clWARNING() << "Failed to CreateFileMapping:" << DBWIN_BUFFER << "." << GetLastError() << clEndl;
            return;
        }
    }

    m_pDBBuffer = (struct dbwin_buffer*)::MapViewOfFile(m_hDBMonBuffer, SECTION_MAP_READ, 0, 0, 0);

    if(m_pDBBuffer == NULL) {
        clWARNING() << "Failed to MapViewOfFile:" << DBWIN_BUFFER << "." << GetLastError() << clEndl;
        return;
    }
#endif
}

OutputDebugStringThread::~OutputDebugStringThread()
{
#ifdef __WXMSW__
    if(m_hDBWinMutex != NULL) {
        CloseHandle(m_hDBWinMutex);
        m_hDBWinMutex = NULL;
    }

    if(m_hDBMonBuffer != NULL) {
        ::UnmapViewOfFile(m_pDBBuffer);
        CloseHandle(m_hDBMonBuffer);
        m_hDBMonBuffer = NULL;
    }

    if(m_hEventBufferReady != NULL) {
        CloseHandle(m_hEventBufferReady);
        m_hEventBufferReady = NULL;
    }

    if(m_hEventDataReady != NULL) {
        CloseHandle(m_hEventDataReady);
        m_hEventDataReady = NULL;
    }

    m_pDBBuffer = NULL;
#endif
}

void* OutputDebugStringThread::Entry()
{
#ifdef __WXMSW__
    while(!TestDestroy()) {
        if(m_collecting) {
            DWORD ret = 0;

            // wait for data ready
            ret = ::WaitForSingleObject(m_hEventDataReady, 100);

            if(ret == WAIT_OBJECT_0) {
                // Send the data
                wxString data = m_pDBBuffer->data;
                int processID = m_pDBBuffer->dwProcessId;
                clCommandEvent event(wxEVT_OUTPUT_DEBUG_STRING);
                event.SetInt(processID);
                event.SetString(data);

                EventNotifier::Get()->AddPendingEvent(event);
                // signal buffer ready
                SetEvent(m_hEventBufferReady);
            }

        } else {
            wxMilliSleep(100);
        }
    }
#endif
    return NULL;
}

void OutputDebugStringThread::Stop()
{
    // Notify the thread to exit and
    // wait for it
    if(IsAlive()) {
        Delete(NULL, wxTHREAD_WAIT_BLOCK);

    } else {
        Wait(wxTHREAD_WAIT_BLOCK);
    }
}

void OutputDebugStringThread::Start(int priority)
{
    Create();
    SetPriority(priority);
    Run();
}