summaryrefslogtreecommitdiffstats
path: root/lldb/source/Core/FormatClasses.cpp
blob: f3306d4af54e528c753370cb8dabe712e2ea07e2 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//===-- FormatClasses.cpp ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// C Includes

// C++ Includes

// Other libraries and framework includes

// Project includes
#include "lldb/lldb-public.h"
#include "lldb/lldb-enumerations.h"

#include "lldb/Core/Debugger.h"
#include "lldb/Core/FormatClasses.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Core/Timer.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Symbol/ClangASTType.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"

using namespace lldb;
using namespace lldb_private;

TypeFormatImpl::TypeFormatImpl (lldb::Format f,
                          const Flags& flags) : 
    m_flags(flags),
    m_format (f)
{
}

std::string
TypeFormatImpl::GetDescription()
{
    StreamString sstr;
    sstr.Printf ("%s%s%s%s\n", 
                 FormatManager::GetFormatAsCString (GetFormat()),
                 Cascades() ? "" : " (not cascading)",
                 SkipsPointers() ? " (skip pointers)" : "",
                 SkipsReferences() ? " (skip references)" : "");
    return sstr.GetString();
}

TypeSummaryImpl::TypeSummaryImpl(const TypeSummaryImpl::Flags& flags) :
    m_flags(flags)
{
}


StringSummaryFormat::StringSummaryFormat(const TypeSummaryImpl::Flags& flags,
                                         const char *format_cstr) :
    TypeSummaryImpl(flags),
    m_format()
{
  if (format_cstr)
    m_format.assign(format_cstr);
}

bool
StringSummaryFormat::FormatObject(ValueObject *valobj,
                                  std::string& retval)
{
    if (!valobj)
    {
        retval.assign("NULL ValueObject");
        return false;
    }
    
    StreamString s;
    ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
    SymbolContext sc;
    StackFrame *frame = exe_ctx.GetFramePtr();
    if (frame)
        sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
    
    if (IsOneliner())
    {
        ValueObject* object;
        
        ValueObjectSP synth_valobj = valobj->GetSyntheticValue();
        if (synth_valobj)
            object = synth_valobj.get();
        else
            object = valobj;
        
        const uint32_t num_children = object->GetNumChildren();
        if (num_children)
        {
            s.PutChar('(');
            
            for (uint32_t idx=0; idx<num_children; ++idx)
            {
                lldb::ValueObjectSP child_sp(object->GetChildAtIndex(idx, true));
                if (child_sp.get())
                {
                    if (idx)
                        s.PutCString(", ");
                    if (!HideNames())
                    {
                        s.PutCString(child_sp.get()->GetName().AsCString());
                        s.PutCString(" = ");
                    }
                    child_sp.get()->DumpPrintableRepresentation(s,
                                                                ValueObject::eValueObjectRepresentationStyleSummary,
                                                                lldb::eFormatInvalid,
                                                                ValueObject::ePrintableRepresentationSpecialCasesDisable);
                }
            }
            
            s.PutChar(')');
            
            retval.assign(s.GetString());
            return true;
        }
        else
        {
            retval.assign("error: oneliner for no children");
            return false;
        }
        
    }
    else
    {
        if (Debugger::FormatPrompt(m_format.c_str(), &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, NULL, valobj))
        {
            retval.assign(s.GetString());
            return true;
        }
        else
        {
            retval.assign("error: summary string parsing error");
            return false;
        }
    }
}

std::string
StringSummaryFormat::GetDescription()
{
    StreamString sstr;
    
    sstr.Printf ("`%s`%s%s%s%s%s%s%s",      m_format.c_str(),
                 Cascades() ? "" : " (not cascading)",
                 !DoesPrintChildren() ? "" : " (show children)",
                 !DoesPrintValue() ? " (hide value)" : "",
                 IsOneliner() ? " (one-line printout)" : "",
                 SkipsPointers() ? " (skip pointers)" : "",
                 SkipsReferences() ? " (skip references)" : "",
                 HideNames() ? " (hide member names)" : "");
    return sstr.GetString();
}

CXXFunctionSummaryFormat::CXXFunctionSummaryFormat (const TypeSummaryImpl::Flags& flags,
                                                    Callback impl,
                                                    const char* description) :
                                                    TypeSummaryImpl(flags),
                                                    m_impl(impl),
                                                    m_description(description ? description : "")
{
}
    
bool
CXXFunctionSummaryFormat::FormatObject(ValueObject *valobj,
                                       std::string& dest)
{
    dest.clear();
    StreamString stream;
    if (!m_impl || m_impl(*valobj,stream) == false)
        return false;
    dest.assign(stream.GetData());
    return true;
}

std::string
CXXFunctionSummaryFormat::GetDescription()
{
    StreamString sstr;
    sstr.Printf ("`%s (%p) `%s%s%s%s%s%s%s",      m_description.c_str(),m_impl,
                 Cascades() ? "" : " (not cascading)",
                 !DoesPrintChildren() ? "" : " (show children)",
                 !DoesPrintValue() ? " (hide value)" : "",
                 IsOneliner() ? " (one-line printout)" : "",
                 SkipsPointers() ? " (skip pointers)" : "",
                 SkipsReferences() ? " (skip references)" : "",
                 HideNames() ? " (hide member names)" : "");
    return sstr.GetString();
}

#ifndef LLDB_DISABLE_PYTHON


ScriptSummaryFormat::ScriptSummaryFormat(const TypeSummaryImpl::Flags& flags,
                                         const char * function_name,
                                         const char * python_script) :
    TypeSummaryImpl(flags),
    m_function_name(),
    m_python_script(),
    m_script_function_sp()
{
   if (function_name)
     m_function_name.assign(function_name);
   if (python_script)
     m_python_script.assign(python_script);
}

bool
ScriptSummaryFormat::FormatObject(ValueObject *valobj,
                                  std::string& retval)
{
    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);

    TargetSP target_sp(valobj->GetTargetSP());

    if (!target_sp)
    {
        retval.assign("error: no target");
        return false;
    }

    ScriptInterpreter *script_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();

    if (!script_interpreter)
    {
        retval.assign("error: no ScriptInterpreter");
        return false;
    }
        
    return script_interpreter->GetScriptedSummary(m_function_name.c_str(),
                                                  valobj->GetSP(),
                                                  m_script_function_sp,
                                                  retval);

}

std::string
ScriptSummaryFormat::GetDescription()
{
    StreamString sstr;
    sstr.Printf ("%s%s%s%s%s%s%s\n%s",       Cascades() ? "" : " (not cascading)",
                 !DoesPrintChildren() ? "" : " (show children)",
                 !DoesPrintValue() ? " (hide value)" : "",
                 IsOneliner() ? " (one-line printout)" : "",
                 SkipsPointers() ? " (skip pointers)" : "",
                 SkipsReferences() ? " (skip references)" : "",
                 HideNames() ? " (hide member names)" : "",
                 m_python_script.c_str());
    return sstr.GetString();
    
}

#endif // #ifndef LLDB_DISABLE_PYTHON

std::string
TypeFilterImpl::GetDescription()
{
    StreamString sstr;
    sstr.Printf("%s%s%s {\n",
                Cascades() ? "" : " (not cascading)",
                SkipsPointers() ? " (skip pointers)" : "",
                SkipsReferences() ? " (skip references)" : "");
    
    for (int i = 0; i < GetCount(); i++)
    {
        sstr.Printf("    %s\n",
                    GetExpressionPathAtIndex(i));
    }
                    
    sstr.Printf("}");
    return sstr.GetString();
}

std::string
CXXSyntheticChildren::GetDescription()
{
    StreamString sstr;
    sstr.Printf("%s%s%s Generator at %p - %s\n",
                Cascades() ? "" : " (not cascading)",
                SkipsPointers() ? " (skip pointers)" : "",
                SkipsReferences() ? " (skip references)" : "",
                m_create_callback,
                m_description.c_str());
    
    return sstr.GetString();
}

std::string
SyntheticArrayView::GetDescription()
{
    StreamString sstr;
    sstr.Printf("%s%s%s {\n",
                Cascades() ? "" : " (not cascading)",
                SkipsPointers() ? " (skip pointers)" : "",
                SkipsReferences() ? " (skip references)" : "");
    
    SyntheticArrayRange* ptr = &m_head;
    while (ptr && ptr != m_tail)
    {
        if (ptr->GetLow() == ptr->GetHigh())
            sstr.Printf("    [%d]\n",
                        ptr->GetLow());
        else
            sstr.Printf("    [%d-%d]\n",
                        ptr->GetLow(),
                        ptr->GetHigh());
        ptr = ptr->GetNext();
    }
    
    sstr.Printf("}");
    return sstr.GetString();
}

#ifndef LLDB_DISABLE_PYTHON

TypeSyntheticImpl::FrontEnd::FrontEnd(std::string pclass, ValueObject &backend) :
    SyntheticChildrenFrontEnd(backend),
    m_python_class(pclass),
    m_wrapper_sp(),
    m_interpreter(NULL)
{
    if (backend == LLDB_INVALID_UID)
        return;
    
    TargetSP target_sp = backend.GetTargetSP();
    
    if (!target_sp)
        return;
    
    m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
    
    if (m_interpreter != NULL)
        m_wrapper_sp = m_interpreter->CreateSyntheticScriptedProvider(m_python_class, backend.GetSP());
}

TypeSyntheticImpl::FrontEnd::~FrontEnd()
{
}

lldb::ValueObjectSP
TypeSyntheticImpl::FrontEnd::GetChildAtIndex (uint32_t idx)
{
    if (!m_wrapper_sp || !m_interpreter)
        return lldb::ValueObjectSP();
    
    return m_interpreter->GetChildAtIndex(m_wrapper_sp, idx);
}

std::string
TypeSyntheticImpl::GetDescription()
{
    StreamString sstr;
    sstr.Printf("%s%s%s Python class %s",
                Cascades() ? "" : " (not cascading)",
                SkipsPointers() ? " (skip pointers)" : "",
                SkipsReferences() ? " (skip references)" : "",
                m_python_class.c_str());
    
    return sstr.GetString();
}

#endif // #ifndef LLDB_DISABLE_PYTHON

int
SyntheticArrayView::GetRealIndexForIndex(int i)
{
    if (i >= GetCount())
        return -1;
    
    SyntheticArrayRange* ptr = &m_head;
    
    int residual = i;
    
    while(ptr && ptr != m_tail)
    {
        if (residual >= ptr->GetSelfCount())
        {
            residual -= ptr->GetSelfCount();
            ptr = ptr->GetNext();
        }
        
        return ptr->GetLow() + residual;
    }
    
    return -1;
}

uint32_t
SyntheticArrayView::FrontEnd::GetIndexOfChildWithName (const ConstString &name_cs)
{
    const char* name_cstr = name_cs.GetCString();
    if (*name_cstr != '[')
        return UINT32_MAX;
    std::string name(name_cstr+1);
    if (name[name.size()-1] != ']')
        return UINT32_MAX;
    name = name.erase(name.size()-1,1);
    int index = Args::StringToSInt32 (name.c_str(), -1);
    if (index < 0)
        return UINT32_MAX;
    return index;
}
OpenPOWER on IntegriCloud