Sean Callanan | 9bc8384 | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 1 | //===-- ExpressionSourceCode.cpp --------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sean Callanan | 9bc8384 | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Expression/ExpressionSourceCode.h" |
| 10 | |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 11 | #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h" |
| 12 | #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include "lldb/Symbol/Block.h" |
Siva Chandra | d8335e9 | 2015-12-16 00:22:08 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/CompileUnit.h" |
| 15 | #include "lldb/Symbol/DebugMacros.h" |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/TypeSystem.h" |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/VariableList.h" |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 18 | #include "lldb/Target/ExecutionContext.h" |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 19 | #include "lldb/Target/Language.h" |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 20 | #include "lldb/Target/Platform.h" |
Sean Callanan | 68f85e7 | 2015-04-30 21:49:58 +0000 | [diff] [blame] | 21 | #include "lldb/Target/StackFrame.h" |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Target.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 23 | #include "lldb/Utility/StreamString.h" |
Sean Callanan | 9bc8384 | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace lldb_private; |
| 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | const char *ExpressionSourceCode::g_expression_prefix = R"( |
Sean Callanan | 68f85e7 | 2015-04-30 21:49:58 +0000 | [diff] [blame] | 28 | #ifndef NULL |
Greg Clayton | 399107a | 2013-02-13 23:57:48 +0000 | [diff] [blame] | 29 | #define NULL (__null) |
Sean Callanan | 68f85e7 | 2015-04-30 21:49:58 +0000 | [diff] [blame] | 30 | #endif |
| 31 | #ifndef Nil |
Greg Clayton | 399107a | 2013-02-13 23:57:48 +0000 | [diff] [blame] | 32 | #define Nil (__null) |
Sean Callanan | 68f85e7 | 2015-04-30 21:49:58 +0000 | [diff] [blame] | 33 | #endif |
| 34 | #ifndef nil |
Greg Clayton | 399107a | 2013-02-13 23:57:48 +0000 | [diff] [blame] | 35 | #define nil (__null) |
Sean Callanan | 68f85e7 | 2015-04-30 21:49:58 +0000 | [diff] [blame] | 36 | #endif |
| 37 | #ifndef YES |
Greg Clayton | 399107a | 2013-02-13 23:57:48 +0000 | [diff] [blame] | 38 | #define YES ((BOOL)1) |
Sean Callanan | 68f85e7 | 2015-04-30 21:49:58 +0000 | [diff] [blame] | 39 | #endif |
| 40 | #ifndef NO |
Greg Clayton | 399107a | 2013-02-13 23:57:48 +0000 | [diff] [blame] | 41 | #define NO ((BOOL)0) |
Sean Callanan | 68f85e7 | 2015-04-30 21:49:58 +0000 | [diff] [blame] | 42 | #endif |
Reid Kleckner | 56d861f | 2014-07-29 21:59:33 +0000 | [diff] [blame] | 43 | typedef __INT8_TYPE__ int8_t; |
| 44 | typedef __UINT8_TYPE__ uint8_t; |
| 45 | typedef __INT16_TYPE__ int16_t; |
| 46 | typedef __UINT16_TYPE__ uint16_t; |
| 47 | typedef __INT32_TYPE__ int32_t; |
| 48 | typedef __UINT32_TYPE__ uint32_t; |
| 49 | typedef __INT64_TYPE__ int64_t; |
| 50 | typedef __UINT64_TYPE__ uint64_t; |
| 51 | typedef __INTPTR_TYPE__ intptr_t; |
| 52 | typedef __UINTPTR_TYPE__ uintptr_t; |
Greg Clayton | 399107a | 2013-02-13 23:57:48 +0000 | [diff] [blame] | 53 | typedef __SIZE_TYPE__ size_t; |
| 54 | typedef __PTRDIFF_TYPE__ ptrdiff_t; |
| 55 | typedef unsigned short unichar; |
Kate Stone | cc391a0 | 2015-01-21 23:39:16 +0000 | [diff] [blame] | 56 | extern "C" |
| 57 | { |
| 58 | int printf(const char * __restrict, ...); |
| 59 | } |
Greg Clayton | 399107a | 2013-02-13 23:57:48 +0000 | [diff] [blame] | 60 | )"; |
Sean Callanan | a199e82 | 2012-09-13 23:35:30 +0000 | [diff] [blame] | 61 | |
Jim Ingham | e5ee6f0 | 2016-03-29 22:00:08 +0000 | [diff] [blame] | 62 | static const char *c_start_marker = " /*LLDB_BODY_START*/\n "; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | static const char *c_end_marker = ";\n /*LLDB_BODY_END*/\n"; |
Jim Ingham | e5ee6f0 | 2016-03-29 22:00:08 +0000 | [diff] [blame] | 64 | |
Siva Chandra | d8335e9 | 2015-12-16 00:22:08 +0000 | [diff] [blame] | 65 | namespace { |
| 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | class AddMacroState { |
| 68 | enum State { |
| 69 | CURRENT_FILE_NOT_YET_PUSHED, |
| 70 | CURRENT_FILE_PUSHED, |
| 71 | CURRENT_FILE_POPPED |
| 72 | }; |
Siva Chandra | d8335e9 | 2015-12-16 00:22:08 +0000 | [diff] [blame] | 73 | |
| 74 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | AddMacroState(const FileSpec ¤t_file, const uint32_t current_file_line) |
| 76 | : m_state(CURRENT_FILE_NOT_YET_PUSHED), m_current_file(current_file), |
| 77 | m_current_file_line(current_file_line) {} |
Siva Chandra | d8335e9 | 2015-12-16 00:22:08 +0000 | [diff] [blame] | 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | void StartFile(const FileSpec &file) { |
| 80 | m_file_stack.push_back(file); |
| 81 | if (file == m_current_file) |
| 82 | m_state = CURRENT_FILE_PUSHED; |
| 83 | } |
| 84 | |
| 85 | void EndFile() { |
| 86 | if (m_file_stack.size() == 0) |
| 87 | return; |
| 88 | |
| 89 | FileSpec old_top = m_file_stack.back(); |
| 90 | m_file_stack.pop_back(); |
| 91 | if (old_top == m_current_file) |
| 92 | m_state = CURRENT_FILE_POPPED; |
| 93 | } |
| 94 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 95 | // An entry is valid if it occurs before the current line in the current |
| 96 | // file. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | bool IsValidEntry(uint32_t line) { |
| 98 | switch (m_state) { |
| 99 | case CURRENT_FILE_NOT_YET_PUSHED: |
| 100 | return true; |
| 101 | case CURRENT_FILE_PUSHED: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 102 | // If we are in file included in the current file, the entry should be |
| 103 | // added. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | if (m_file_stack.back() != m_current_file) |
| 105 | return true; |
| 106 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 107 | return line < m_current_file_line; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | default: |
| 109 | return false; |
Siva Chandra | d8335e9 | 2015-12-16 00:22:08 +0000 | [diff] [blame] | 110 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | } |
Siva Chandra | d8335e9 | 2015-12-16 00:22:08 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | std::vector<FileSpec> m_file_stack; |
| 115 | State m_state; |
| 116 | FileSpec m_current_file; |
| 117 | uint32_t m_current_file_line; |
Siva Chandra | d8335e9 | 2015-12-16 00:22:08 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // anonymous namespace |
| 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit, |
| 123 | AddMacroState &state, StreamString &stream) { |
| 124 | if (dm == nullptr) |
| 125 | return; |
| 126 | |
| 127 | for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) { |
| 128 | const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i); |
| 129 | uint32_t line; |
| 130 | |
| 131 | switch (entry.GetType()) { |
| 132 | case DebugMacroEntry::DEFINE: |
| 133 | if (state.IsValidEntry(entry.GetLineNumber())) |
| 134 | stream.Printf("#define %s\n", entry.GetMacroString().AsCString()); |
| 135 | else |
Siva Chandra | d8335e9 | 2015-12-16 00:22:08 +0000 | [diff] [blame] | 136 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | break; |
| 138 | case DebugMacroEntry::UNDEF: |
| 139 | if (state.IsValidEntry(entry.GetLineNumber())) |
| 140 | stream.Printf("#undef %s\n", entry.GetMacroString().AsCString()); |
| 141 | else |
| 142 | return; |
| 143 | break; |
| 144 | case DebugMacroEntry::START_FILE: |
| 145 | line = entry.GetLineNumber(); |
| 146 | if (state.IsValidEntry(line)) |
| 147 | state.StartFile(entry.GetFileSpec(comp_unit)); |
| 148 | else |
| 149 | return; |
| 150 | break; |
| 151 | case DebugMacroEntry::END_FILE: |
| 152 | state.EndFile(); |
| 153 | break; |
| 154 | case DebugMacroEntry::INDIRECT: |
| 155 | AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream); |
| 156 | break; |
Jim Ingham | e5ee6f0 | 2016-03-29 22:00:08 +0000 | [diff] [blame] | 157 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | // This is an unknown/invalid entry. Ignore. |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp, |
| 165 | StreamString &stream) { |
| 166 | for (size_t i = 0; i < var_list_sp->GetSize(); i++) { |
| 167 | lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i); |
| 168 | |
| 169 | ConstString var_name = var_sp->GetName(); |
| 170 | if (!var_name || var_name == ConstString("this") || |
| 171 | var_name == ConstString(".block_descriptor")) |
| 172 | continue; |
| 173 | |
| 174 | stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString()); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | bool ExpressionSourceCode::GetText(std::string &text, |
| 179 | lldb::LanguageType wrapping_language, |
| 180 | bool static_method, |
| 181 | ExecutionContext &exe_ctx) const { |
| 182 | const char *target_specific_defines = "typedef signed char BOOL;\n"; |
| 183 | std::string module_macros; |
| 184 | |
| 185 | Target *target = exe_ctx.GetTargetPtr(); |
| 186 | if (target) { |
| 187 | if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64) { |
| 188 | target_specific_defines = "typedef bool BOOL;\n"; |
| 189 | } |
| 190 | if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) { |
| 191 | if (lldb::PlatformSP platform_sp = target->GetPlatform()) { |
| 192 | static ConstString g_platform_ios_simulator("ios-simulator"); |
| 193 | if (platform_sp->GetPluginName() == g_platform_ios_simulator) { |
| 194 | target_specific_defines = "typedef bool BOOL;\n"; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (ClangModulesDeclVendor *decl_vendor = |
| 200 | target->GetClangModulesDeclVendor()) { |
| 201 | ClangPersistentVariables *persistent_vars = |
| 202 | llvm::cast<ClangPersistentVariables>( |
| 203 | target->GetPersistentExpressionStateForLanguage( |
| 204 | lldb::eLanguageTypeC)); |
| 205 | const ClangModulesDeclVendor::ModuleVector &hand_imported_modules = |
| 206 | persistent_vars->GetHandLoadedClangModules(); |
| 207 | ClangModulesDeclVendor::ModuleVector modules_for_macros; |
| 208 | |
| 209 | for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) { |
| 210 | modules_for_macros.push_back(module); |
| 211 | } |
| 212 | |
| 213 | if (target->GetEnableAutoImportClangModules()) { |
| 214 | if (StackFrame *frame = exe_ctx.GetFramePtr()) { |
| 215 | if (Block *block = frame->GetFrameBlock()) { |
| 216 | SymbolContext sc; |
| 217 | |
| 218 | block->CalculateSymbolContext(&sc); |
| 219 | |
| 220 | if (sc.comp_unit) { |
| 221 | StreamString error_stream; |
| 222 | |
| 223 | decl_vendor->AddModulesForCompileUnit( |
| 224 | *sc.comp_unit, modules_for_macros, error_stream); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | decl_vendor->ForEachMacro( |
| 231 | modules_for_macros, |
| 232 | [&module_macros](const std::string &expansion) -> bool { |
| 233 | module_macros.append(expansion); |
| 234 | module_macros.append("\n"); |
| 235 | return false; |
| 236 | }); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | StreamString debug_macros_stream; |
| 241 | StreamString lldb_local_var_decls; |
| 242 | if (StackFrame *frame = exe_ctx.GetFramePtr()) { |
| 243 | const SymbolContext &sc = frame->GetSymbolContext( |
| 244 | lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry); |
| 245 | |
| 246 | if (sc.comp_unit && sc.line_entry.IsValid()) { |
| 247 | DebugMacros *dm = sc.comp_unit->GetDebugMacros(); |
| 248 | if (dm) { |
| 249 | AddMacroState state(sc.line_entry.file, sc.line_entry.line); |
| 250 | AddMacros(dm, sc.comp_unit, state, debug_macros_stream); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | ConstString object_name; |
Krasimir Georgiev | a35912d | 2018-10-18 03:10:43 +0000 | [diff] [blame] | 255 | if (Language::LanguageIsCPlusPlus(frame->GetLanguage())) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | if (target->GetInjectLocalVariables(&exe_ctx)) { |
| 257 | lldb::VariableListSP var_list_sp = |
| 258 | frame->GetInScopeVariableList(false, true); |
| 259 | AddLocalVariableDecls(var_list_sp, lldb_local_var_decls); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (m_wrap) { |
| 265 | switch (wrapping_language) { |
| 266 | default: |
| 267 | return false; |
Jim Ingham | e5ee6f0 | 2016-03-29 22:00:08 +0000 | [diff] [blame] | 268 | case lldb::eLanguageTypeC: |
| 269 | case lldb::eLanguageTypeC_plus_plus: |
| 270 | case lldb::eLanguageTypeObjC: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | break; |
Jim Ingham | e5ee6f0 | 2016-03-29 22:00:08 +0000 | [diff] [blame] | 272 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | |
| 274 | StreamString wrap_stream; |
| 275 | |
| 276 | wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", module_macros.c_str(), |
| 277 | debug_macros_stream.GetData(), g_expression_prefix, |
| 278 | target_specific_defines, m_prefix.c_str()); |
| 279 | |
| 280 | // First construct a tagged form of the user expression so we can find it |
| 281 | // later: |
| 282 | std::string tagged_body; |
| 283 | switch (wrapping_language) { |
| 284 | default: |
| 285 | tagged_body = m_body; |
| 286 | break; |
| 287 | case lldb::eLanguageTypeC: |
| 288 | case lldb::eLanguageTypeC_plus_plus: |
| 289 | case lldb::eLanguageTypeObjC: |
| 290 | tagged_body.append(c_start_marker); |
| 291 | tagged_body.append(m_body); |
| 292 | tagged_body.append(c_end_marker); |
| 293 | break; |
| 294 | } |
| 295 | switch (wrapping_language) { |
| 296 | default: |
| 297 | break; |
| 298 | case lldb::eLanguageTypeC: |
| 299 | wrap_stream.Printf("void \n" |
| 300 | "%s(void *$__lldb_arg) \n" |
| 301 | "{ \n" |
| 302 | " %s; \n" |
| 303 | "%s" |
| 304 | "} \n", |
| 305 | m_name.c_str(), lldb_local_var_decls.GetData(), |
| 306 | tagged_body.c_str()); |
| 307 | break; |
| 308 | case lldb::eLanguageTypeC_plus_plus: |
| 309 | wrap_stream.Printf("void \n" |
| 310 | "$__lldb_class::%s(void *$__lldb_arg) \n" |
| 311 | "{ \n" |
| 312 | " %s; \n" |
| 313 | "%s" |
| 314 | "} \n", |
| 315 | m_name.c_str(), lldb_local_var_decls.GetData(), |
| 316 | tagged_body.c_str()); |
| 317 | break; |
| 318 | case lldb::eLanguageTypeObjC: |
| 319 | if (static_method) { |
| 320 | wrap_stream.Printf( |
| 321 | "@interface $__lldb_objc_class ($__lldb_category) \n" |
| 322 | "+(void)%s:(void *)$__lldb_arg; \n" |
| 323 | "@end \n" |
| 324 | "@implementation $__lldb_objc_class ($__lldb_category) \n" |
| 325 | "+(void)%s:(void *)$__lldb_arg \n" |
| 326 | "{ \n" |
| 327 | "%s" |
| 328 | "} \n" |
| 329 | "@end \n", |
| 330 | m_name.c_str(), m_name.c_str(), tagged_body.c_str()); |
| 331 | } else { |
| 332 | wrap_stream.Printf( |
| 333 | "@interface $__lldb_objc_class ($__lldb_category) \n" |
| 334 | "-(void)%s:(void *)$__lldb_arg; \n" |
| 335 | "@end \n" |
| 336 | "@implementation $__lldb_objc_class ($__lldb_category) \n" |
| 337 | "-(void)%s:(void *)$__lldb_arg \n" |
| 338 | "{ \n" |
| 339 | "%s" |
| 340 | "} \n" |
| 341 | "@end \n", |
| 342 | m_name.c_str(), m_name.c_str(), tagged_body.c_str()); |
| 343 | } |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | text = wrap_stream.GetString(); |
| 348 | } else { |
| 349 | text.append(m_body); |
| 350 | } |
| 351 | |
| 352 | return true; |
Jim Ingham | e5ee6f0 | 2016-03-29 22:00:08 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 355 | bool ExpressionSourceCode::GetOriginalBodyBounds( |
| 356 | std::string transformed_text, lldb::LanguageType wrapping_language, |
| 357 | size_t &start_loc, size_t &end_loc) { |
| 358 | const char *start_marker; |
| 359 | const char *end_marker; |
| 360 | |
| 361 | switch (wrapping_language) { |
| 362 | default: |
| 363 | return false; |
| 364 | case lldb::eLanguageTypeC: |
| 365 | case lldb::eLanguageTypeC_plus_plus: |
| 366 | case lldb::eLanguageTypeObjC: |
| 367 | start_marker = c_start_marker; |
| 368 | end_marker = c_end_marker; |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | start_loc = transformed_text.find(start_marker); |
| 373 | if (start_loc == std::string::npos) |
| 374 | return false; |
| 375 | start_loc += strlen(start_marker); |
| 376 | end_loc = transformed_text.find(end_marker); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 377 | return end_loc != std::string::npos; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 378 | } |