aboutsummaryrefslogtreecommitdiff
path: root/source/Breakpoint/BreakpointResolverName.cpp
blob: 37054d4afa7f07c96843aea203ac5ef8f328a676 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//===-- BreakpointResolverName.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/Breakpoint/BreakpointResolverName.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Breakpoint/BreakpointLocation.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Target/Target.h"

using namespace lldb;
using namespace lldb_private;

BreakpointResolverName::BreakpointResolverName
(
    Breakpoint *bkpt,
    const char *func_name,
    uint32_t func_name_type_mask,
    Breakpoint::MatchType type,
    bool skip_prologue
) :
    BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
    m_func_name (),
    m_basename_filter (),
    m_func_name_type_mask (func_name_type_mask),
    m_class_name (),
    m_regex (),
    m_match_type (type),
    m_skip_prologue (skip_prologue)
{
    if (func_name_type_mask == eFunctionNameTypeAuto)
    {
        if ((::strchr (func_name, '(' ) != NULL) ||
            (::strstr (func_name, "-[") == func_name) || 
            (::strstr (func_name, "+[") == func_name))
        {
            // We have a name that contains an open parens, or starts with 
            // "+[" or "-[", so this looks like a complete function prototype
            m_func_name_type_mask = eFunctionNameTypeFull;
        }
        else
        {
            // We don't have a full function name, but we might have a partial
            // function basename with namespaces or classes
            if (::strstr (func_name, "::") != NULL)
            {
                // Keep the full name in "m_basename_filter"
                m_basename_filter = func_name;
                // Now set "m_func_name" to just the function basename
                m_func_name.SetCString(m_basename_filter.c_str() + m_basename_filter.rfind("::") + 2);
                // We have a name with a double colon which means we have a
                // function name that is a C++ method or a function in a C++ 
                // namespace
                m_func_name_type_mask = eFunctionNameTypeBase | eFunctionNameTypeMethod;
            }
            else if (::strstr (func_name, ":") != NULL)
            {
                // Single colon => selector
                m_func_name_type_mask = eFunctionNameTypeSelector;
            }
            else
            {
                // just a  basename by default
                m_func_name_type_mask = eFunctionNameTypeBase;
            }
        }
    }

    if (!m_func_name)
        m_func_name.SetCString(func_name);
    
    if (m_match_type == Breakpoint::Regexp)
    {
        if (!m_regex.Compile (m_func_name.AsCString()))
        {
            LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));

            if (log)
                log->Warning ("function name regexp: \"%s\" did not compile.", m_func_name.AsCString());
        }
    }
}

BreakpointResolverName::BreakpointResolverName
(
    Breakpoint *bkpt,
    RegularExpression &func_regex,
    bool skip_prologue
) :
    BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
    m_func_name (NULL),
    m_class_name (NULL),
    m_regex (func_regex),
    m_match_type (Breakpoint::Regexp),
    m_skip_prologue (skip_prologue)
{

}

BreakpointResolverName::BreakpointResolverName
(
    Breakpoint *bkpt,
    const char *class_name,
    const char *method,
    Breakpoint::MatchType type,
    bool skip_prologue
) :
    BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
    m_func_name (method),
    m_class_name (class_name),
    m_regex (),
    m_match_type (type),
    m_skip_prologue (skip_prologue)
{

}

BreakpointResolverName::~BreakpointResolverName ()
{
}

// FIXME: Right now we look at the module level, and call the module's "FindFunctions".
// Greg says he will add function tables, maybe at the CompileUnit level to accelerate function
// lookup.  At that point, we should switch the depth to CompileUnit, and look in these tables.

Searcher::CallbackReturn
BreakpointResolverName::SearchCallback
(
    SearchFilter &filter,
    SymbolContext &context,
    Address *addr,
    bool containing
)
{
    SymbolContextList func_list;
    SymbolContextList sym_list;
    
    uint32_t i;
    bool new_location;
    SymbolContext sc;
    Address break_addr;
    assert (m_breakpoint != NULL);
    
    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
    
    if (m_class_name)
    {
        if (log)
            log->Warning ("Class/method function specification not supported yet.\n");
        return Searcher::eCallbackReturnStop;
    }
    
    const bool include_symbols = false;
    const bool append = false;
    switch (m_match_type)
    {
        case Breakpoint::Exact:
            if (context.module_sp)
            {
                if (m_func_name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull))
                    context.module_sp->FindSymbolsWithNameAndType (m_func_name, eSymbolTypeCode, sym_list);
                context.module_sp->FindFunctions (m_func_name, 
                                                  m_func_name_type_mask, 
                                                  include_symbols, 
                                                  append, 
                                                  func_list);
            }
            break;
        case Breakpoint::Regexp:
            if (context.module_sp)
            {
                context.module_sp->FindSymbolsMatchingRegExAndType (m_regex, eSymbolTypeCode, sym_list);
                context.module_sp->FindFunctions (m_regex, 
                                                  include_symbols, 
                                                  append, 
                                                  func_list);
            }
            break;
        case Breakpoint::Glob:
            if (log)
                log->Warning ("glob is not supported yet.");
            break;
    }
    
    if (!m_basename_filter.empty())
    {
        // Filter out any matches whose names don't contain the basename filter
        const char *basename_filter = m_basename_filter.c_str();
        if (func_list.GetSize())
        {
            bool remove = false;
            for (i = 0; i < func_list.GetSize(); remove = false)
            {
                if (func_list.GetContextAtIndex(i, sc) == false)
                    remove = true;
                else if (sc.function == NULL)
                    remove = true;
                else
                {
                    const InlineFunctionInfo* inlined_info = NULL;
                    
                    if (sc.block)
                        inlined_info = sc.block->GetInlinedFunctionInfo();

                    if (inlined_info)
                    {
                        if (::strstr (inlined_info->GetName().AsCString(), basename_filter) == NULL)
                            remove = true;
                    }
                    else if (::strstr (sc.function->GetName().AsCString(), basename_filter) == NULL)
                        remove = true;
                }

                if (remove)
                {
                    func_list.RemoveContextAtIndex(i);
                    continue;
                }
                i++;
            }
        }

        if (sym_list.GetSize())
        {
            bool remove = false;
            for (i = 0; i < sym_list.GetSize(); remove = false)
            {
                if (sym_list.GetContextAtIndex(i, sc) == false)
                    remove = true;
                else if (sc.symbol == NULL)
                    remove = true;
                else if (::strstr (sc.symbol->GetName().AsCString(), basename_filter) == NULL)
                    remove = true;

                if (remove)
                {
                    sym_list.RemoveContextAtIndex(i);
                    continue;
                }
                i++;
            }
        }
    }
    
    // Remove any duplicates between the funcion list and the symbol list
    if (func_list.GetSize())
    {
        for (i = 0; i < func_list.GetSize(); i++)
        {
            if (func_list.GetContextAtIndex(i, sc) == false)
                continue;
            
            if (sc.function == NULL)
                continue;
            uint32_t j = 0;
            while (j < sym_list.GetSize())
            {
                SymbolContext symbol_sc;
                if (sym_list.GetContextAtIndex(j, symbol_sc))
                {
                    if (symbol_sc.symbol && symbol_sc.symbol->GetAddressRangePtr())
                    {
                        if (sc.function->GetAddressRange().GetBaseAddress() == symbol_sc.symbol->GetAddressRangePtr()->GetBaseAddress())
                        {
                            sym_list.RemoveContextAtIndex(j);
                            continue;   // Don't increment j
                        }
                    }
                }
                
                j++;
            }
        }
        
        for (i = 0; i < func_list.GetSize(); i++)
        {
            if (func_list.GetContextAtIndex(i, sc))
            {
                if (sc.block && sc.block->GetInlinedFunctionInfo())
                {
                    if (!sc.block->GetStartAddress(break_addr))
                        break_addr.Clear();
                }
                else if (sc.function)
                {
                    break_addr = sc.function->GetAddressRange().GetBaseAddress();
                    if (m_skip_prologue)
                    {
                        if (break_addr.IsValid())
                        {
                            const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
                            if (prologue_byte_size)
                                break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
                        }
                    }
                }
                
                if (break_addr.IsValid())
                {
                    if (filter.AddressPasses(break_addr))
                    {
                        BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(break_addr, &new_location));
                        if (bp_loc_sp && new_location && !m_breakpoint->IsInternal())
                        {
                            if (log)
                            {
                                StreamString s;
                                bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
                                log->Printf ("Added location: %s\n", s.GetData());
                            }
                        }
                    }
                }
            }
        }
    }
    
    for (i = 0; i < sym_list.GetSize(); i++)
    {
        if (sym_list.GetContextAtIndex(i, sc))
        {
            if (sc.symbol && sc.symbol->GetAddressRangePtr())
            {
                break_addr = sc.symbol->GetAddressRangePtr()->GetBaseAddress();
                
                if (m_skip_prologue)
                {
                    const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
                    if (prologue_byte_size)
                        break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
                }
                
                if (filter.AddressPasses(break_addr))
                {
                    BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(break_addr, &new_location));
                    if (bp_loc_sp && new_location && !m_breakpoint->IsInternal())
                    {
                        StreamString s;
                        bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
                        if (log) 
                            log->Printf ("Added location: %s\n", s.GetData());
                    }
                }
            }
        }
    }
    return Searcher::eCallbackReturnContinue;
}

Searcher::Depth
BreakpointResolverName::GetDepth()
{
    return Searcher::eDepthModule;
}

void
BreakpointResolverName::GetDescription (Stream *s)
{
    if (m_match_type == Breakpoint::Regexp)
        s->Printf("regex = '%s'", m_regex.GetText());
    else if (m_basename_filter.empty())
        s->Printf("name = '%s'", m_func_name.AsCString());
    else
        s->Printf("name = '%s'", m_basename_filter.c_str());
}

void
BreakpointResolverName::Dump (Stream *s) const
{

}