summaryrefslogtreecommitdiffstats
path: root/lldb/source/lldb-log.cpp
blob: 4e291fab4abd71da161f6a974184687ed5995864 (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
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
//===-- lldb-log.cpp --------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/lldb-private-log.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Interpreter/Args.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/StreamFile.h"
#include <string.h>

using namespace lldb;
using namespace lldb_private;


static Log *
LogAccessor (bool get, StreamSP *stream_sp_ptr)
{
    static Log* g_log = NULL; // Leak for now as auto_ptr was being cleaned up
                                // by global constructors before other threads
                                // were done with it.
    if (!get)
    {
        if (g_log)
            delete g_log;
        if (stream_sp_ptr)
            g_log = new Log (*stream_sp_ptr);
        else
            g_log = NULL;
    }

    return g_log;

}

uint32_t
lldb_private::GetLogMask ()
{
    Log *log = LogAccessor (true, NULL);
    if (log)
        return log->GetMask().GetAllFlagBits();
    return 0;
}

bool
lldb_private::IsLogVerbose ()
{
    uint32_t mask = GetLogMask();
    return (mask & LIBLLDB_LOG_VERBOSE);
}

Log *
lldb_private::GetLogIfAllCategoriesSet (uint32_t mask)
{
    Log *log = LogAccessor (true, NULL);
    if (log && mask)
    {
        uint32_t log_mask = log->GetMask().GetAllFlagBits();
        if ((log_mask & mask) != mask)
            return NULL;
    }
    return log;
}

void
lldb_private::LogIfAllCategoriesSet (uint32_t mask, const char *format, ...)
{
    Log *log = GetLogIfAllCategoriesSet (mask);
    if (log)
    {
        va_list args;
        va_start (args, format);
        log->VAPrintf (format, args);
        va_end (args);
    }
}

void
lldb_private::LogIfAnyCategoriesSet (uint32_t mask, const char *format, ...)
{
    Log *log = GetLogIfAnyCategoriesSet (mask);
    if (log)
    {
        va_list args;
        va_start (args, format);
        log->VAPrintf (format, args);
        va_end (args);
    }
}

Log *
lldb_private::GetLogIfAnyCategoriesSet (uint32_t mask)
{
    Log *log = LogAccessor (true, NULL);
    if (log && mask && (mask & log->GetMask().GetAllFlagBits()))
        return log;
    return NULL;
}

void
lldb_private::DisableLog ()
{
    LogAccessor (false, NULL);
}


Log *
lldb_private::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
{
    // Try see if there already is a log - that way we can reuse its settings.
    // We could reuse the log in toto, but we don't know that the stream is the same.
    uint32_t flag_bits;
    Log* log = LogAccessor (true, NULL);
    if (log)
        flag_bits = log->GetMask().GetAllFlagBits();
    else
        flag_bits = 0;

    // Now make a new log with this stream.
    log = LogAccessor (false, &log_stream_sp);
    if (log)
    {
        bool got_unknown_category = false;
        const size_t argc = args.GetArgumentCount();
        for (size_t i=0; i<argc; ++i)
        {
            const char *arg = args.GetArgumentAtIndex(i);

            if      (strcasecmp(arg, "all")     == 0  ) flag_bits |= LIBLLDB_LOG_ALL;
            else if (strcasestr(arg, "break")   == arg) flag_bits |= LIBLLDB_LOG_BREAKPOINTS;
            else if (strcasecmp(arg, "default") == 0  ) flag_bits |= LIBLLDB_LOG_DEFAULT;
            else if (strcasestr(arg, "event")   == arg) flag_bits |= LIBLLDB_LOG_EVENTS;
            else if (strcasestr(arg, "expr")    == arg) flag_bits |= LIBLLDB_LOG_EXPRESSIONS;
            else if (strcasestr(arg, "object")  == arg) flag_bits |= LIBLLDB_LOG_OBJECT;
            else if (strcasecmp(arg, "process") == 0  ) flag_bits |= LIBLLDB_LOG_PROCESS;
            else if (strcasecmp(arg, "shlib")   == 0  ) flag_bits |= LIBLLDB_LOG_SHLIB;
            else if (strcasecmp(arg, "state")   == 0  ) flag_bits |= LIBLLDB_LOG_STATE;
            else if (strcasecmp(arg, "step")    == 0  ) flag_bits |= LIBLLDB_LOG_STEP;
            else if (strcasecmp(arg, "thread")  == 0  ) flag_bits |= LIBLLDB_LOG_THREAD;
            else if (strcasecmp(arg, "verbose") == 0  ) flag_bits |= LIBLLDB_LOG_VERBOSE;
            else if (strcasestr(arg, "watch")   == arg) flag_bits |= LIBLLDB_LOG_WATCHPOINTS;
            else if (strcasestr(arg, "temp")   == arg)  flag_bits |= LIBLLDB_LOG_TEMPORARY;
            else if (strcasestr(arg, "comm")   == arg)  flag_bits |= LIBLLDB_LOG_COMMUNICATION;
            else if (strcasestr(arg, "conn")   == arg)  flag_bits |= LIBLLDB_LOG_CONNECTION;
            else if (strcasestr(arg, "host")   == arg)  flag_bits |= LIBLLDB_LOG_HOST;
            else
            {
                feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
                if (got_unknown_category == false)
                {
                    got_unknown_category = true;
                    ListLogCategories (feedback_strm);
                    return log;
                }
            }
        }

        log->GetMask().SetAllFlagBits(flag_bits);
        log->GetOptions().SetAllFlagBits(log_options);
    }
    return log;
}


void
lldb_private::ListLogCategories (Stream *strm)
{
    strm->Printf("Logging categories for 'lldb':\n"
        "\tall - turn on all available logging categories\n"
        "\tdefault - enable the default set of logging categories for liblldb\n"
        "\tbreak - log breakpoints\n"
        "\tevents - log broadcaster, listener and event queue activities\n"
        "\texpr - log expressions\n"
        "\tobject - log object construction/destruction for important objects\n"
        "\tprocess - log process events and activities\n"
        "\tthread - log thread events and activities\n"
        "\tshlib - log shared library related activities\n"
        "\tstate - log private and public process state changes\n"
        "\tstep - log step related activities\n"
        "\tverbose - enable verbose loggging\n"
        "\twatch - log watchpoint related activities\n");
}
OpenPOWER on IntegriCloud