summaryrefslogtreecommitdiffstats
path: root/lldb/source/API/SBValue.cpp
blob: f92be9f1037bc231ebd08da633030af4c3943649 (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
//===-- SBValue.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/API/SBValue.h"

#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/Stream.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/Value.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Thread.h"

#include "lldb/API/SBProcess.h"
#include "lldb/API/SBTarget.h"
#include "lldb/API/SBThread.h"
#include "lldb/API/SBFrame.h"
#include "lldb/API/SBDebugger.h"

using namespace lldb;
using namespace lldb_private;

SBValue::SBValue () :
    m_opaque_sp ()
{
}

SBValue::SBValue (const lldb::ValueObjectSP &value_sp) :
    m_opaque_sp (value_sp)
{
}

SBValue::~SBValue()
{
}

bool
SBValue::IsValid () const
{
    return  (m_opaque_sp.get() != NULL);
}

void
SBValue::Print (FILE *out_file, SBFrame *frame, bool print_type, bool print_value)
{
    if (out_file == NULL)
        return;

    if (IsValid())
    {

        SBThread sb_thread = frame->GetThread();
        SBProcess sb_process = sb_thread.GetProcess();

        lldb_private::StackFrame *lldb_frame = frame->GetLLDBObjectPtr();
        lldb_private::Thread *lldb_thread = sb_thread.GetLLDBObjectPtr();
        lldb_private::Process *lldb_process = sb_process.get();

        lldb_private::ExecutionContext context (lldb_process, lldb_thread, lldb_frame);

        lldb_private::StreamFile out_stream (out_file);

        out_stream.Printf ("%s ", m_opaque_sp->GetName().AsCString (NULL));
        if (! m_opaque_sp->IsInScope (lldb_frame))
            out_stream.Printf ("[out-of-scope] ");
        if (print_type)
        {
            out_stream.Printf ("(%s) ", m_opaque_sp->GetTypeName().AsCString ("<unknown-type>"));
        }

        if (print_value)
        {
            ExecutionContextScope *exe_scope = frame->get();
            const char *val_cstr = m_opaque_sp->GetValueAsCString(exe_scope);
            const char *err_cstr = m_opaque_sp->GetError().AsCString();

            if (!err_cstr)
            {
                const char *sum_cstr = m_opaque_sp->GetSummaryAsCString(exe_scope);
                const bool is_aggregate =
                ClangASTContext::IsAggregateType (m_opaque_sp->GetOpaqueClangQualType());
                if (val_cstr)
                    out_stream.Printf ("= %s ", val_cstr);

                if (sum_cstr)
                    out_stream.Printf ("%s ", sum_cstr);

                if (is_aggregate)
                {
                    out_stream.PutChar ('{');
                    const uint32_t num_children = m_opaque_sp->GetNumChildren();
                    if (num_children)
                    {
                        out_stream.IndentMore();
                        for (uint32_t idx = 0; idx < num_children; ++idx)
                        {
                            lldb::ValueObjectSP child_sp (m_opaque_sp->GetChildAtIndex (idx, true));
                            if (child_sp.get())
                            {
                                out_stream.EOL();
                                out_stream.Indent();
                                out_stream.Printf ("%s (%s) = %s", child_sp.get()->GetName().AsCString (""),
                                                   child_sp.get()->GetTypeName().AsCString ("<unknown type>"),
                                                   child_sp.get()->GetValueAsCString(exe_scope));
                            }
                        }
                        out_stream.IndentLess();
                    }
                    out_stream.EOL();
                    out_stream.Indent ("}");
                }
            }
        }
        out_stream.EOL ();
    }
}

const char *
SBValue::GetName()
{
    if (IsValid())
        return m_opaque_sp->GetName().AsCString();
    else
        return NULL;
}

const char *
SBValue::GetTypeName ()
{
    if (IsValid())
        return m_opaque_sp->GetTypeName().AsCString();
    else
        return NULL;
}

size_t
SBValue::GetByteSize ()
{
    size_t result = 0;

    if (IsValid())
        result = m_opaque_sp->GetByteSize();

    return result;
}

bool
SBValue::IsInScope (const SBFrame &frame)
{
    bool result = false;

    if (IsValid())
        result = m_opaque_sp->IsInScope (frame.get());

    return result;
}

const char *
SBValue::GetValue (const SBFrame &frame)
{
    const char *value_string = NULL;
    if ( m_opaque_sp)
        value_string = m_opaque_sp->GetValueAsCString (frame.get());
    return value_string;
}

bool
SBValue::GetValueDidChange (const SBFrame &frame)
{
    if (IsValid())
        return m_opaque_sp->GetValueDidChange (frame.get());
    return false;
}

const char *
SBValue::GetSummary (const SBFrame &frame)
{
    const char *value_string = NULL;
    if ( m_opaque_sp)
        value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
    return value_string;
}

const char *
SBValue::GetLocation (const SBFrame &frame)
{
    const char *value_string = NULL;
    if (IsValid())
        value_string = m_opaque_sp->GetLocationAsCString(frame.get());
    return value_string;
}

bool
SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
{
    bool success = false;
    if (IsValid())
        success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
    return success;
}

SBValue
SBValue::GetChildAtIndex (uint32_t idx)
{
    lldb::ValueObjectSP child_sp;

    if (IsValid())
    {
        child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
    }

    SBValue sb_value (child_sp);
    return sb_value;
}

uint32_t
SBValue::GetIndexOfChildWithName (const char *name)
{
    if (IsValid())
        return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
    return UINT32_MAX;
}

SBValue
SBValue::GetChildMemberWithName (const char *name)
{
    lldb::ValueObjectSP child_sp;
    const ConstString str_name (name);

    if (IsValid())
    {
        child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
    }

    SBValue sb_value (child_sp);
    return sb_value;
}


uint32_t
SBValue::GetNumChildren ()
{
    uint32_t num_children = 0;

    if (IsValid())
    {
        num_children = m_opaque_sp->GetNumChildren();
    }

    return num_children;
}

bool
SBValue::ValueIsStale ()
{
    bool result = true;

    if (IsValid())
    {
        result = m_opaque_sp->GetValueIsValid();
    }

    return result;
}


SBValue
SBValue::Dereference ()
{
    if (IsValid())
    {
        if (m_opaque_sp->IsPointerType())
        {
            return GetChildAtIndex(0);
        }
    }
    return *this;
}

bool
SBValue::TypeIsPtrType ()
{
    bool is_ptr_type = false;

    if (IsValid())
    {
        is_ptr_type = m_opaque_sp->IsPointerType();
    }

    return is_ptr_type;
}

void *
SBValue::GetOpaqueType()
{
    if (m_opaque_sp)
        return m_opaque_sp->GetOpaqueClangQualType();
    return NULL;
}

// Mimic shared pointer...
lldb_private::ValueObject *
SBValue::get() const
{
    return m_opaque_sp.get();
}

lldb_private::ValueObject *
SBValue::operator->() const
{
    return m_opaque_sp.get();
}

lldb::ValueObjectSP &
SBValue::operator*()
{
    return m_opaque_sp;
}

const lldb::ValueObjectSP &
SBValue::operator*() const
{
    return m_opaque_sp;
}
OpenPOWER on IntegriCloud