Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ClangExpressionVariable.cpp -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Expression/ClangExpressionVariable.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "clang/AST/ASTContext.h" |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 17 | #include "lldb/Core/ConstString.h" |
| 18 | #include "lldb/Core/DataExtractor.h" |
| 19 | #include "lldb/Core/Stream.h" |
| 20 | #include "lldb/Core/Value.h" |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame^] | 21 | #include "lldb/Target/ExecutionContext.h" |
| 22 | #include "lldb/Target/Process.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace lldb_private; |
| 25 | using namespace clang; |
| 26 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 27 | ClangExpressionVariable::ClangExpressionVariable() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | { |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 29 | m_name = ""; |
| 30 | m_user_type = TypeFromUser(NULL, NULL); |
| 31 | m_parser_vars.reset(NULL); |
| 32 | m_jit_vars.reset(NULL); |
| 33 | m_data_vars.reset(NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 36 | void ClangExpressionVariable::DisableDataVars() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | { |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 38 | if (m_data_vars.get() && m_data_vars->m_data) |
| 39 | delete m_data_vars->m_data; |
| 40 | m_data_vars.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 43 | Error |
| 44 | ClangExpressionVariable::Print (Stream &output_stream, |
| 45 | ExecutionContext &exe_ctx, |
| 46 | lldb::Format format, |
| 47 | bool show_types, |
| 48 | bool show_summary, |
| 49 | bool verbose) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | { |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 51 | Error err; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 53 | if (!m_data_vars.get() || !m_data_vars->m_data) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | { |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 55 | err.SetErrorToGenericError(); |
| 56 | err.SetErrorStringWithFormat("Variable doesn't contain a value"); |
| 57 | return err; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 60 | Value val; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 62 | clang::ASTContext *ast_context = m_user_type.GetASTContext(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 64 | val.SetContext (Value::eContextTypeOpaqueClangQualType, m_user_type.GetOpaqueQualType ()); |
| 65 | val.SetValueType (Value::eValueTypeHostAddress); |
| 66 | val.GetScalar() = (uint64_t)m_data_vars->m_data->GetBytes (); |
| 67 | |
| 68 | val.ResolveValue (&exe_ctx, ast_context); |
| 69 | |
| 70 | if (val.GetContextType () == Value::eContextTypeInvalid && |
| 71 | val.GetValueType () == Value::eValueTypeScalar && |
| 72 | format == lldb::eFormatDefault) |
| 73 | { |
| 74 | // The expression result is just a scalar with no special formatting |
| 75 | val.GetScalar ().GetValue (&output_stream, show_types); |
| 76 | output_stream.EOL (); |
| 77 | return err; |
| 78 | } |
| 79 | |
| 80 | // The expression result is more complex and requires special handling |
| 81 | DataExtractor data; |
| 82 | Error expr_error = val.GetValueAsData (&exe_ctx, ast_context, data, 0); |
| 83 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame^] | 84 | |
| 85 | // Set byte order and pointer size to TARGET byte order and pointer size! |
| 86 | |
| 87 | data.SetByteOrder(exe_ctx.process->GetByteOrder()); |
| 88 | data.SetAddressByteSize(exe_ctx.process->GetAddressByteSize()); |
| 89 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 90 | if (!expr_error.Success ()) |
| 91 | { |
| 92 | err.SetErrorToGenericError (); |
| 93 | err.SetErrorStringWithFormat ("Couldn't resolve variable value: %s", expr_error.AsCString ()); |
| 94 | return err; |
| 95 | } |
| 96 | |
| 97 | if (format == lldb::eFormatDefault) |
| 98 | format = val.GetValueDefaultFormat (); |
| 99 | |
| 100 | void *clang_type = val.GetValueOpaqueClangQualType (); |
| 101 | |
| 102 | output_stream.Printf("%s = ", m_name.c_str()); |
| 103 | |
| 104 | if (clang_type) |
| 105 | { |
| 106 | if (show_types) |
| 107 | output_stream.Printf("(%s) ", ClangASTType::GetClangTypeName (clang_type).GetCString()); |
| 108 | |
| 109 | ClangASTType::DumpValue (ast_context, // The ASTContext that the clang type belongs to |
| 110 | clang_type, // The opaque clang type we want to dump that value of |
| 111 | &exe_ctx, // The execution context for memory and variable access |
| 112 | &output_stream, // Stream to dump to |
| 113 | format, // Format to use when dumping |
| 114 | data, // A buffer containing the bytes for the clang type |
| 115 | 0, // Byte offset within "data" where value is |
| 116 | data.GetByteSize (), // Size in bytes of the value we are dumping |
| 117 | 0, // Bitfield bit size |
| 118 | 0, // Bitfield bit offset |
| 119 | show_types, // Show types? |
| 120 | show_summary, // Show summary? |
| 121 | verbose, // Debug logging output? |
| 122 | UINT32_MAX); // Depth to dump in case this is an aggregate type |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | data.Dump (&output_stream, // Stream to dump to |
| 127 | 0, // Byte offset within "data" |
| 128 | format, // Format to use when dumping |
| 129 | data.GetByteSize (), // Size in bytes of each item we are dumping |
| 130 | 1, // Number of items to dump |
| 131 | UINT32_MAX, // Number of items per line |
| 132 | LLDB_INVALID_ADDRESS, // Invalid address, don't show any offset/address context |
| 133 | 0, // Bitfield bit size |
| 134 | 0); // Bitfield bit offset |
| 135 | } |
| 136 | |
| 137 | output_stream.EOL(); |
| 138 | |
| 139 | return err; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 142 | ClangExpressionVariable::ClangExpressionVariable(const ClangExpressionVariable &cev) : |
| 143 | m_name(cev.m_name), |
Sean Callanan | 8194e42 | 2010-08-30 21:15:33 +0000 | [diff] [blame] | 144 | m_user_type(cev.m_user_type), |
| 145 | m_store(cev.m_store), |
| 146 | m_index(cev.m_index) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | { |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 148 | if (cev.m_parser_vars.get()) |
| 149 | { |
| 150 | m_parser_vars.reset(new struct ParserVars); |
| 151 | *m_parser_vars.get() = *cev.m_parser_vars.get(); |
| 152 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 154 | if (cev.m_jit_vars.get()) |
| 155 | { |
| 156 | m_jit_vars.reset(new struct JITVars); |
| 157 | *m_jit_vars.get() = *cev.m_jit_vars.get(); |
| 158 | } |
| 159 | |
| 160 | if (cev.m_data_vars.get()) |
| 161 | { |
| 162 | m_data_vars.reset(new struct DataVars); |
| 163 | *m_data_vars.get() = *cev.m_data_vars.get(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | bool |
| 168 | ClangExpressionVariable::PointValueAtData(Value &value) |
| 169 | { |
| 170 | if (!m_data_vars.get()) |
| 171 | return false; |
| 172 | |
| 173 | value.SetContext(Value::eContextTypeOpaqueClangQualType, m_user_type.GetOpaqueQualType()); |
| 174 | value.SetValueType(Value::eValueTypeHostAddress); |
| 175 | value.GetScalar() = (uint64_t)m_data_vars->m_data->GetBytes(); |
| 176 | |
| 177 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | } |