1 | //===-- ClangUserExpression.cpp -------------------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include <cstdio> |
10 | #include <sys/types.h> |
11 | |
12 | #include <cstdlib> |
13 | #include <map> |
14 | #include <string> |
15 | |
16 | #include "ClangUserExpression.h" |
17 | |
18 | #include "ASTResultSynthesizer.h" |
19 | #include "ClangASTMetadata.h" |
20 | #include "ClangDiagnostic.h" |
21 | #include "ClangExpressionDeclMap.h" |
22 | #include "ClangExpressionParser.h" |
23 | #include "ClangModulesDeclVendor.h" |
24 | #include "ClangPersistentVariables.h" |
25 | #include "CppModuleConfiguration.h" |
26 | |
27 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
28 | #include "lldb/Core/Debugger.h" |
29 | #include "lldb/Core/Module.h" |
30 | #include "lldb/Expression/ExpressionSourceCode.h" |
31 | #include "lldb/Expression/IRExecutionUnit.h" |
32 | #include "lldb/Expression/IRInterpreter.h" |
33 | #include "lldb/Expression/Materializer.h" |
34 | #include "lldb/Host/HostInfo.h" |
35 | #include "lldb/Symbol/Block.h" |
36 | #include "lldb/Symbol/CompileUnit.h" |
37 | #include "lldb/Symbol/Function.h" |
38 | #include "lldb/Symbol/ObjectFile.h" |
39 | #include "lldb/Symbol/SymbolFile.h" |
40 | #include "lldb/Symbol/SymbolVendor.h" |
41 | #include "lldb/Symbol/Type.h" |
42 | #include "lldb/Symbol/VariableList.h" |
43 | #include "lldb/Target/ExecutionContext.h" |
44 | #include "lldb/Target/Process.h" |
45 | #include "lldb/Target/StackFrame.h" |
46 | #include "lldb/Target/Target.h" |
47 | #include "lldb/Target/ThreadPlan.h" |
48 | #include "lldb/Target/ThreadPlanCallUserExpression.h" |
49 | #include "lldb/Utility/ConstString.h" |
50 | #include "lldb/Utility/LLDBLog.h" |
51 | #include "lldb/Utility/Log.h" |
52 | #include "lldb/Utility/StreamString.h" |
53 | #include "lldb/ValueObject/ValueObjectConstResult.h" |
54 | |
55 | #include "clang/AST/DeclCXX.h" |
56 | #include "clang/AST/DeclObjC.h" |
57 | |
58 | #include "llvm/ADT/ScopeExit.h" |
59 | #include "llvm/BinaryFormat/Dwarf.h" |
60 | |
61 | using namespace lldb_private; |
62 | |
63 | char ClangUserExpression::ID; |
64 | |
65 | ClangUserExpression::ClangUserExpression( |
66 | ExecutionContextScope &exe_scope, llvm::StringRef expr, |
67 | llvm::StringRef prefix, SourceLanguage language, ResultType desired_type, |
68 | const EvaluateExpressionOptions &options, ValueObject *ctx_obj) |
69 | : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type, |
70 | options), |
71 | m_type_system_helper(*m_target_wp.lock(), options.GetExecutionPolicy() == |
72 | eExecutionPolicyTopLevel), |
73 | m_result_delegate(exe_scope.CalculateTarget()), m_ctx_obj(ctx_obj) { |
74 | switch (m_language.name) { |
75 | case llvm::dwarf::DW_LNAME_C_plus_plus: |
76 | m_allow_cxx = true; |
77 | break; |
78 | case llvm::dwarf::DW_LNAME_ObjC: |
79 | m_allow_objc = true; |
80 | break; |
81 | case llvm::dwarf::DW_LNAME_ObjC_plus_plus: |
82 | default: |
83 | m_allow_cxx = true; |
84 | m_allow_objc = true; |
85 | break; |
86 | } |
87 | } |
88 | |
89 | ClangUserExpression::~ClangUserExpression() = default; |
90 | |
91 | void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) { |
92 | Log *log = GetLog(mask: LLDBLog::Expressions); |
93 | |
94 | LLDB_LOGF(log, "ClangUserExpression::ScanContext()" ); |
95 | |
96 | m_target = exe_ctx.GetTargetPtr(); |
97 | |
98 | if (!(m_allow_cxx || m_allow_objc)) { |
99 | LLDB_LOGF(log, " [CUE::SC] Settings inhibit C++ and Objective-C" ); |
100 | return; |
101 | } |
102 | |
103 | StackFrame *frame = exe_ctx.GetFramePtr(); |
104 | if (frame == nullptr) { |
105 | LLDB_LOGF(log, " [CUE::SC] Null stack frame" ); |
106 | return; |
107 | } |
108 | |
109 | SymbolContext sym_ctx = frame->GetSymbolContext(resolve_scope: lldb::eSymbolContextFunction | |
110 | lldb::eSymbolContextBlock); |
111 | |
112 | if (!sym_ctx.function) { |
113 | LLDB_LOGF(log, " [CUE::SC] Null function" ); |
114 | return; |
115 | } |
116 | |
117 | // Find the block that defines the function represented by "sym_ctx" |
118 | Block *function_block = sym_ctx.GetFunctionBlock(); |
119 | |
120 | if (!function_block) { |
121 | LLDB_LOGF(log, " [CUE::SC] Null function block" ); |
122 | return; |
123 | } |
124 | |
125 | CompilerDeclContext decl_context = function_block->GetDeclContext(); |
126 | |
127 | if (!decl_context) { |
128 | LLDB_LOGF(log, " [CUE::SC] Null decl context" ); |
129 | return; |
130 | } |
131 | |
132 | if (m_ctx_obj) { |
133 | switch (m_ctx_obj->GetObjectRuntimeLanguage()) { |
134 | case lldb::eLanguageTypeC: |
135 | case lldb::eLanguageTypeC89: |
136 | case lldb::eLanguageTypeC99: |
137 | case lldb::eLanguageTypeC11: |
138 | case lldb::eLanguageTypeC_plus_plus: |
139 | case lldb::eLanguageTypeC_plus_plus_03: |
140 | case lldb::eLanguageTypeC_plus_plus_11: |
141 | case lldb::eLanguageTypeC_plus_plus_14: |
142 | m_in_cplusplus_method = true; |
143 | break; |
144 | case lldb::eLanguageTypeObjC: |
145 | case lldb::eLanguageTypeObjC_plus_plus: |
146 | m_in_objectivec_method = true; |
147 | break; |
148 | default: |
149 | break; |
150 | } |
151 | m_needs_object_ptr = true; |
152 | } else if (clang::CXXMethodDecl *method_decl = |
153 | TypeSystemClang::DeclContextGetAsCXXMethodDecl(dc: decl_context)) { |
154 | if (m_allow_cxx && method_decl->isInstance()) { |
155 | if (m_enforce_valid_object) { |
156 | lldb::VariableListSP variable_list_sp( |
157 | function_block->GetBlockVariableList(can_create: true)); |
158 | |
159 | const char *thisErrorString = "Stopped in a C++ method, but 'this' " |
160 | "isn't available; pretending we are in a " |
161 | "generic context" ; |
162 | |
163 | if (!variable_list_sp) { |
164 | err = Status::FromErrorString(str: thisErrorString); |
165 | return; |
166 | } |
167 | |
168 | lldb::VariableSP this_var_sp( |
169 | variable_list_sp->FindVariable(name: ConstString("this" ))); |
170 | |
171 | if (!this_var_sp || !this_var_sp->IsInScope(frame) || |
172 | !this_var_sp->LocationIsValidForFrame(frame)) { |
173 | err = Status::FromErrorString(str: thisErrorString); |
174 | return; |
175 | } |
176 | } |
177 | |
178 | m_in_cplusplus_method = true; |
179 | m_needs_object_ptr = true; |
180 | } |
181 | } else if (clang::ObjCMethodDecl *method_decl = |
182 | TypeSystemClang::DeclContextGetAsObjCMethodDecl( |
183 | dc: decl_context)) { |
184 | if (m_allow_objc) { |
185 | if (m_enforce_valid_object) { |
186 | lldb::VariableListSP variable_list_sp( |
187 | function_block->GetBlockVariableList(can_create: true)); |
188 | |
189 | const char *selfErrorString = "Stopped in an Objective-C method, but " |
190 | "'self' isn't available; pretending we " |
191 | "are in a generic context" ; |
192 | |
193 | if (!variable_list_sp) { |
194 | err = Status::FromErrorString(str: selfErrorString); |
195 | return; |
196 | } |
197 | |
198 | lldb::VariableSP self_variable_sp = |
199 | variable_list_sp->FindVariable(name: ConstString("self" )); |
200 | |
201 | if (!self_variable_sp || !self_variable_sp->IsInScope(frame) || |
202 | !self_variable_sp->LocationIsValidForFrame(frame)) { |
203 | err = Status::FromErrorString(str: selfErrorString); |
204 | return; |
205 | } |
206 | } |
207 | |
208 | m_in_objectivec_method = true; |
209 | m_needs_object_ptr = true; |
210 | |
211 | if (!method_decl->isInstanceMethod()) |
212 | m_in_static_method = true; |
213 | } |
214 | } else if (clang::FunctionDecl *function_decl = |
215 | TypeSystemClang::DeclContextGetAsFunctionDecl(dc: decl_context)) { |
216 | // We might also have a function that said in the debug information that it |
217 | // captured an object pointer. The best way to deal with getting to the |
218 | // ivars at present is by pretending that this is a method of a class in |
219 | // whatever runtime the debug info says the object pointer belongs to. Do |
220 | // that here. |
221 | |
222 | if (std::optional<ClangASTMetadata> metadata = |
223 | TypeSystemClang::DeclContextGetMetaData(decl_context, |
224 | function_decl); |
225 | metadata && metadata->HasObjectPtr()) { |
226 | lldb::LanguageType language = metadata->GetObjectPtrLanguage(); |
227 | if (language == lldb::eLanguageTypeC_plus_plus) { |
228 | if (m_enforce_valid_object) { |
229 | lldb::VariableListSP variable_list_sp( |
230 | function_block->GetBlockVariableList(can_create: true)); |
231 | |
232 | const char *thisErrorString = "Stopped in a context claiming to " |
233 | "capture a C++ object pointer, but " |
234 | "'this' isn't available; pretending we " |
235 | "are in a generic context" ; |
236 | |
237 | if (!variable_list_sp) { |
238 | err = Status::FromErrorString(str: thisErrorString); |
239 | return; |
240 | } |
241 | |
242 | lldb::VariableSP this_var_sp( |
243 | variable_list_sp->FindVariable(name: ConstString("this" ))); |
244 | |
245 | if (!this_var_sp || !this_var_sp->IsInScope(frame) || |
246 | !this_var_sp->LocationIsValidForFrame(frame)) { |
247 | err = Status::FromErrorString(str: thisErrorString); |
248 | return; |
249 | } |
250 | } |
251 | |
252 | m_in_cplusplus_method = true; |
253 | m_needs_object_ptr = true; |
254 | } else if (language == lldb::eLanguageTypeObjC) { |
255 | if (m_enforce_valid_object) { |
256 | lldb::VariableListSP variable_list_sp( |
257 | function_block->GetBlockVariableList(can_create: true)); |
258 | |
259 | const char *selfErrorString = |
260 | "Stopped in a context claiming to capture an Objective-C object " |
261 | "pointer, but 'self' isn't available; pretending we are in a " |
262 | "generic context" ; |
263 | |
264 | if (!variable_list_sp) { |
265 | err = Status::FromErrorString(str: selfErrorString); |
266 | return; |
267 | } |
268 | |
269 | lldb::VariableSP self_variable_sp = |
270 | variable_list_sp->FindVariable(name: ConstString("self" )); |
271 | |
272 | if (!self_variable_sp || !self_variable_sp->IsInScope(frame) || |
273 | !self_variable_sp->LocationIsValidForFrame(frame)) { |
274 | err = Status::FromErrorString(str: selfErrorString); |
275 | return; |
276 | } |
277 | |
278 | Type *self_type = self_variable_sp->GetType(); |
279 | |
280 | if (!self_type) { |
281 | err = Status::FromErrorString(str: selfErrorString); |
282 | return; |
283 | } |
284 | |
285 | CompilerType self_clang_type = self_type->GetForwardCompilerType(); |
286 | |
287 | if (!self_clang_type) { |
288 | err = Status::FromErrorString(str: selfErrorString); |
289 | return; |
290 | } |
291 | |
292 | if (TypeSystemClang::IsObjCClassType(type: self_clang_type)) { |
293 | return; |
294 | } else if (TypeSystemClang::IsObjCObjectPointerType( |
295 | type: self_clang_type)) { |
296 | m_in_objectivec_method = true; |
297 | m_needs_object_ptr = true; |
298 | } else { |
299 | err = Status::FromErrorString(str: selfErrorString); |
300 | return; |
301 | } |
302 | } else { |
303 | m_in_objectivec_method = true; |
304 | m_needs_object_ptr = true; |
305 | } |
306 | } |
307 | } |
308 | } |
309 | } |
310 | |
311 | // This is a really nasty hack, meant to fix Objective-C expressions of the |
312 | // form (int)[myArray count]. Right now, because the type information for |
313 | // count is not available, [myArray count] returns id, which can't be directly |
314 | // cast to int without causing a clang error. |
315 | static void ApplyObjcCastHack(std::string &expr) { |
316 | const std::string from = "(int)[" ; |
317 | const std::string to = "(int)(long long)[" ; |
318 | |
319 | size_t offset; |
320 | |
321 | while ((offset = expr.find(str: from)) != expr.npos) |
322 | expr.replace(pos: offset, n: from.size(), str: to); |
323 | } |
324 | |
325 | bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager, |
326 | ExecutionContext &exe_ctx) { |
327 | if (Target *target = exe_ctx.GetTargetPtr()) { |
328 | if (PersistentExpressionState *persistent_state = |
329 | target->GetPersistentExpressionStateForLanguage( |
330 | language: lldb::eLanguageTypeC)) { |
331 | m_clang_state = llvm::cast<ClangPersistentVariables>(Val: persistent_state); |
332 | m_result_delegate.RegisterPersistentState(persistent_state); |
333 | } else { |
334 | diagnostic_manager.PutString( |
335 | severity: lldb::eSeverityError, str: "couldn't start parsing (no persistent data)" ); |
336 | return false; |
337 | } |
338 | } else { |
339 | diagnostic_manager.PutString(severity: lldb::eSeverityError, |
340 | str: "error: couldn't start parsing (no target)" ); |
341 | return false; |
342 | } |
343 | return true; |
344 | } |
345 | |
346 | static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target, |
347 | DiagnosticManager &diagnostic_manager) { |
348 | if (!target->GetEnableAutoImportClangModules()) |
349 | return; |
350 | |
351 | auto *persistent_state = llvm::cast<ClangPersistentVariables>( |
352 | Val: target->GetPersistentExpressionStateForLanguage(language: lldb::eLanguageTypeC)); |
353 | if (!persistent_state) |
354 | return; |
355 | |
356 | std::shared_ptr<ClangModulesDeclVendor> decl_vendor = |
357 | persistent_state->GetClangModulesDeclVendor(); |
358 | if (!decl_vendor) |
359 | return; |
360 | |
361 | StackFrame *frame = exe_ctx.GetFramePtr(); |
362 | if (!frame) |
363 | return; |
364 | |
365 | Block *block = frame->GetFrameBlock(); |
366 | if (!block) |
367 | return; |
368 | SymbolContext sc; |
369 | |
370 | block->CalculateSymbolContext(sc: &sc); |
371 | |
372 | if (!sc.comp_unit) |
373 | return; |
374 | StreamString error_stream; |
375 | |
376 | ClangModulesDeclVendor::ModuleVector modules_for_macros = |
377 | persistent_state->GetHandLoadedClangModules(); |
378 | if (decl_vendor->AddModulesForCompileUnit(cu&: *sc.comp_unit, exported_modules&: modules_for_macros, |
379 | error_stream)) |
380 | return; |
381 | |
382 | // Failed to load some modules, so emit the error stream as a diagnostic. |
383 | if (!error_stream.Empty()) { |
384 | // The error stream already contains several Clang diagnostics that might |
385 | // be either errors or warnings, so just print them all as one remark |
386 | // diagnostic to prevent that the message starts with "error: error:". |
387 | diagnostic_manager.PutString(severity: lldb::eSeverityInfo, str: error_stream.GetString()); |
388 | return; |
389 | } |
390 | |
391 | diagnostic_manager.PutString(severity: lldb::eSeverityError, |
392 | str: "Unknown error while loading modules needed for " |
393 | "current compilation unit." ); |
394 | } |
395 | |
396 | ClangExpressionSourceCode::WrapKind ClangUserExpression::GetWrapKind() const { |
397 | assert(m_options.GetExecutionPolicy() != eExecutionPolicyTopLevel && |
398 | "Top level expressions aren't wrapped." ); |
399 | using Kind = ClangExpressionSourceCode::WrapKind; |
400 | if (m_in_cplusplus_method) |
401 | return Kind::CppMemberFunction; |
402 | else if (m_in_objectivec_method) { |
403 | if (m_in_static_method) |
404 | return Kind::ObjCStaticMethod; |
405 | return Kind::ObjCInstanceMethod; |
406 | } |
407 | // Not in any kind of 'special' function, so just wrap it in a normal C |
408 | // function. |
409 | return Kind::Function; |
410 | } |
411 | |
412 | void ClangUserExpression::CreateSourceCode( |
413 | DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, |
414 | std::vector<std::string> modules_to_import, bool for_completion) { |
415 | |
416 | std::string prefix = m_expr_prefix; |
417 | |
418 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { |
419 | m_transformed_text = m_expr_text; |
420 | } else { |
421 | m_source_code.reset(p: ClangExpressionSourceCode::CreateWrapped( |
422 | filename: m_filename, prefix, body: m_expr_text, wrap_kind: GetWrapKind())); |
423 | |
424 | if (!m_source_code->GetText(text&: m_transformed_text, exe_ctx, add_locals: !m_ctx_obj, |
425 | force_add_all_locals: for_completion, modules: modules_to_import)) { |
426 | diagnostic_manager.PutString(severity: lldb::eSeverityError, |
427 | str: "couldn't construct expression body" ); |
428 | return; |
429 | } |
430 | |
431 | // Find and store the start position of the original code inside the |
432 | // transformed code. We need this later for the code completion. |
433 | std::size_t original_start; |
434 | std::size_t original_end; |
435 | bool found_bounds = m_source_code->GetOriginalBodyBounds( |
436 | transformed_text: m_transformed_text, start_loc&: original_start, end_loc&: original_end); |
437 | if (found_bounds) |
438 | m_user_expression_start_pos = original_start; |
439 | } |
440 | } |
441 | |
442 | static bool SupportsCxxModuleImport(lldb::LanguageType language) { |
443 | switch (language) { |
444 | case lldb::eLanguageTypeC_plus_plus: |
445 | case lldb::eLanguageTypeC_plus_plus_03: |
446 | case lldb::eLanguageTypeC_plus_plus_11: |
447 | case lldb::eLanguageTypeC_plus_plus_14: |
448 | case lldb::eLanguageTypeObjC_plus_plus: |
449 | return true; |
450 | default: |
451 | return false; |
452 | } |
453 | } |
454 | |
455 | /// Utility method that puts a message into the expression log and |
456 | /// returns an invalid module configuration. |
457 | static CppModuleConfiguration LogConfigError(const std::string &msg) { |
458 | Log *log = GetLog(mask: LLDBLog::Expressions); |
459 | LLDB_LOG(log, "[C++ module config] {0}" , msg); |
460 | return CppModuleConfiguration(); |
461 | } |
462 | |
463 | CppModuleConfiguration GetModuleConfig(lldb::LanguageType language, |
464 | ExecutionContext &exe_ctx) { |
465 | Log *log = GetLog(mask: LLDBLog::Expressions); |
466 | |
467 | // Don't do anything if this is not a C++ module configuration. |
468 | if (!SupportsCxxModuleImport(language)) |
469 | return LogConfigError(msg: "Language doesn't support C++ modules" ); |
470 | |
471 | Target *target = exe_ctx.GetTargetPtr(); |
472 | if (!target) |
473 | return LogConfigError(msg: "No target" ); |
474 | |
475 | StackFrame *frame = exe_ctx.GetFramePtr(); |
476 | if (!frame) |
477 | return LogConfigError(msg: "No frame" ); |
478 | |
479 | Block *block = frame->GetFrameBlock(); |
480 | if (!block) |
481 | return LogConfigError(msg: "No block" ); |
482 | |
483 | SymbolContext sc; |
484 | block->CalculateSymbolContext(sc: &sc); |
485 | if (!sc.comp_unit) |
486 | return LogConfigError(msg: "Couldn't calculate symbol context" ); |
487 | |
488 | // Build a list of files we need to analyze to build the configuration. |
489 | FileSpecList files; |
490 | for (auto &f : sc.comp_unit->GetSupportFiles()) |
491 | files.AppendIfUnique(file: f->Materialize()); |
492 | // We also need to look at external modules in the case of -gmodules as they |
493 | // contain the support files for libc++ and the C library. |
494 | llvm::DenseSet<SymbolFile *> visited_symbol_files; |
495 | sc.comp_unit->ForEachExternalModule( |
496 | visited_symbol_files, lambda: [&files](Module &module) { |
497 | for (std::size_t i = 0; i < module.GetNumCompileUnits(); ++i) { |
498 | const SupportFileList &support_files = |
499 | module.GetCompileUnitAtIndex(idx: i)->GetSupportFiles(); |
500 | for (auto &f : support_files) { |
501 | files.AppendIfUnique(file: f->Materialize()); |
502 | } |
503 | } |
504 | return false; |
505 | }); |
506 | |
507 | LLDB_LOG(log, "[C++ module config] Found {0} support files to analyze" , |
508 | files.GetSize()); |
509 | if (log && log->GetVerbose()) { |
510 | for (auto &f : files) |
511 | LLDB_LOGV(log, "[C++ module config] Analyzing support file: {0}" , |
512 | f.GetPath()); |
513 | } |
514 | |
515 | // Try to create a configuration from the files. If there is no valid |
516 | // configuration possible with the files, this just returns an invalid |
517 | // configuration. |
518 | return CppModuleConfiguration(files, target->GetArchitecture().GetTriple()); |
519 | } |
520 | |
521 | bool ClangUserExpression::PrepareForParsing( |
522 | DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, |
523 | bool for_completion) { |
524 | InstallContext(exe_ctx); |
525 | |
526 | if (!SetupPersistentState(diagnostic_manager, exe_ctx)) |
527 | return false; |
528 | |
529 | Status err; |
530 | ScanContext(exe_ctx, err); |
531 | |
532 | if (!err.Success()) { |
533 | diagnostic_manager.PutString(severity: lldb::eSeverityWarning, str: err.AsCString()); |
534 | } |
535 | |
536 | //////////////////////////////////// |
537 | // Generate the expression |
538 | // |
539 | |
540 | ApplyObjcCastHack(expr&: m_expr_text); |
541 | |
542 | SetupDeclVendor(exe_ctx, target: m_target, diagnostic_manager); |
543 | |
544 | m_filename = m_clang_state->GetNextExprFileName(); |
545 | |
546 | if (m_target->GetImportStdModule() == eImportStdModuleTrue) |
547 | SetupCppModuleImports(exe_ctx); |
548 | |
549 | CreateSourceCode(diagnostic_manager, exe_ctx, modules_to_import: m_imported_cpp_modules, |
550 | for_completion); |
551 | return true; |
552 | } |
553 | |
554 | bool ClangUserExpression::TryParse( |
555 | DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, |
556 | lldb_private::ExecutionPolicy execution_policy, bool keep_result_in_memory, |
557 | bool generate_debug_info) { |
558 | m_materializer_up = std::make_unique<Materializer>(); |
559 | |
560 | ResetDeclMap(exe_ctx, result_delegate&: m_result_delegate, keep_result_in_memory); |
561 | |
562 | auto on_exit = llvm::make_scope_exit(F: [this]() { ResetDeclMap(); }); |
563 | |
564 | if (!DeclMap()->WillParse(exe_ctx, materializer: GetMaterializer())) { |
565 | diagnostic_manager.PutString( |
566 | severity: lldb::eSeverityError, |
567 | str: "current process state is unsuitable for expression parsing" ); |
568 | return false; |
569 | } |
570 | |
571 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { |
572 | DeclMap()->SetLookupsEnabled(true); |
573 | } |
574 | |
575 | m_parser = std::make_unique<ClangExpressionParser>( |
576 | args: exe_ctx.GetBestExecutionContextScope(), args&: *this, args&: generate_debug_info, |
577 | args&: m_include_directories, args&: m_filename); |
578 | |
579 | unsigned num_errors = m_parser->Parse(diagnostic_manager); |
580 | |
581 | // Check here for FixItHints. If there are any try to apply the fixits and |
582 | // set the fixed text in m_fixed_text before returning an error. |
583 | if (num_errors) { |
584 | if (diagnostic_manager.HasFixIts()) { |
585 | if (m_parser->RewriteExpression(diagnostic_manager)) { |
586 | size_t fixed_start; |
587 | size_t fixed_end; |
588 | m_fixed_text = diagnostic_manager.GetFixedExpression(); |
589 | // Retrieve the original expression in case we don't have a top level |
590 | // expression (which has no surrounding source code). |
591 | if (m_source_code && m_source_code->GetOriginalBodyBounds( |
592 | transformed_text: m_fixed_text, start_loc&: fixed_start, end_loc&: fixed_end)) |
593 | m_fixed_text = |
594 | m_fixed_text.substr(pos: fixed_start, n: fixed_end - fixed_start); |
595 | } |
596 | } |
597 | return false; |
598 | } |
599 | |
600 | ////////////////////////////////////////////////////////////////////////////// |
601 | // Prepare the output of the parser for execution, evaluating it statically |
602 | // if possible |
603 | // |
604 | |
605 | { |
606 | Status jit_error = m_parser->PrepareForExecution( |
607 | func_addr&: m_jit_start_addr, func_end&: m_jit_end_addr, execution_unit_sp&: m_execution_unit_sp, exe_ctx, |
608 | can_interpret&: m_can_interpret, execution_policy); |
609 | |
610 | if (!jit_error.Success()) { |
611 | const char *error_cstr = jit_error.AsCString(); |
612 | if (error_cstr && error_cstr[0]) |
613 | diagnostic_manager.PutString(severity: lldb::eSeverityError, str: error_cstr); |
614 | else |
615 | diagnostic_manager.PutString(severity: lldb::eSeverityError, |
616 | str: "expression can't be interpreted or run" ); |
617 | return false; |
618 | } |
619 | } |
620 | return true; |
621 | } |
622 | |
623 | void ClangUserExpression::SetupCppModuleImports(ExecutionContext &exe_ctx) { |
624 | Log *log = GetLog(mask: LLDBLog::Expressions); |
625 | |
626 | CppModuleConfiguration module_config = |
627 | GetModuleConfig(language: m_language.AsLanguageType(), exe_ctx); |
628 | m_imported_cpp_modules = module_config.GetImportedModules(); |
629 | m_include_directories = module_config.GetIncludeDirs(); |
630 | |
631 | LLDB_LOG(log, "List of imported modules in expression: {0}" , |
632 | llvm::make_range(m_imported_cpp_modules.begin(), |
633 | m_imported_cpp_modules.end())); |
634 | LLDB_LOG(log, "List of include directories gathered for modules: {0}" , |
635 | llvm::make_range(m_include_directories.begin(), |
636 | m_include_directories.end())); |
637 | } |
638 | |
639 | static bool shouldRetryWithCppModule(Target &target, ExecutionPolicy exe_policy) { |
640 | // Top-level expression don't yet support importing C++ modules. |
641 | if (exe_policy == ExecutionPolicy::eExecutionPolicyTopLevel) |
642 | return false; |
643 | return target.GetImportStdModule() == eImportStdModuleFallback; |
644 | } |
645 | |
646 | bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager, |
647 | ExecutionContext &exe_ctx, |
648 | lldb_private::ExecutionPolicy execution_policy, |
649 | bool keep_result_in_memory, |
650 | bool generate_debug_info) { |
651 | Log *log = GetLog(mask: LLDBLog::Expressions); |
652 | |
653 | if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ false)) |
654 | return false; |
655 | |
656 | LLDB_LOGF(log, "Parsing the following code:\n%s" , m_transformed_text.c_str()); |
657 | |
658 | //////////////////////////////////// |
659 | // Set up the target and compiler |
660 | // |
661 | |
662 | Target *target = exe_ctx.GetTargetPtr(); |
663 | |
664 | if (!target) { |
665 | diagnostic_manager.PutString(severity: lldb::eSeverityError, str: "invalid target" ); |
666 | return false; |
667 | } |
668 | |
669 | ////////////////////////// |
670 | // Parse the expression |
671 | // |
672 | |
673 | bool parse_success = TryParse(diagnostic_manager, exe_ctx, execution_policy, |
674 | keep_result_in_memory, generate_debug_info); |
675 | // If the expression failed to parse, check if retrying parsing with a loaded |
676 | // C++ module is possible. |
677 | if (!parse_success && shouldRetryWithCppModule(target&: *target, exe_policy: execution_policy)) { |
678 | // Load the loaded C++ modules. |
679 | SetupCppModuleImports(exe_ctx); |
680 | // If we did load any modules, then retry parsing. |
681 | if (!m_imported_cpp_modules.empty()) { |
682 | // Create a dedicated diagnostic manager for the second parse attempt. |
683 | // These diagnostics are only returned to the caller if using the fallback |
684 | // actually succeeded in getting the expression to parse. This prevents |
685 | // that module-specific issues regress diagnostic quality with the |
686 | // fallback mode. |
687 | DiagnosticManager retry_manager; |
688 | // The module imports are injected into the source code wrapper, |
689 | // so recreate those. |
690 | CreateSourceCode(diagnostic_manager&: retry_manager, exe_ctx, modules_to_import: m_imported_cpp_modules, |
691 | /*for_completion*/ false); |
692 | parse_success = TryParse(diagnostic_manager&: retry_manager, exe_ctx, execution_policy, |
693 | keep_result_in_memory, generate_debug_info); |
694 | // Return the parse diagnostics if we were successful. |
695 | if (parse_success) |
696 | diagnostic_manager = std::move(retry_manager); |
697 | } |
698 | } |
699 | if (!parse_success) |
700 | return false; |
701 | |
702 | if (m_execution_unit_sp) { |
703 | bool register_execution_unit = false; |
704 | |
705 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { |
706 | register_execution_unit = true; |
707 | } |
708 | |
709 | // If there is more than one external function in the execution unit, it |
710 | // needs to keep living even if it's not top level, because the result |
711 | // could refer to that function. |
712 | |
713 | if (m_execution_unit_sp->GetJittedFunctions().size() > 1) { |
714 | register_execution_unit = true; |
715 | } |
716 | |
717 | if (register_execution_unit) { |
718 | if (auto *persistent_state = |
719 | exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage( |
720 | language: m_language.AsLanguageType())) |
721 | persistent_state->RegisterExecutionUnit(execution_unit_sp&: m_execution_unit_sp); |
722 | } |
723 | } |
724 | |
725 | if (generate_debug_info) { |
726 | lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule()); |
727 | |
728 | if (jit_module_sp) { |
729 | ConstString const_func_name(FunctionName()); |
730 | FileSpec jit_file; |
731 | jit_file.SetFilename(const_func_name); |
732 | jit_module_sp->SetFileSpecAndObjectName(file: jit_file, object_name: ConstString()); |
733 | m_jit_module_wp = jit_module_sp; |
734 | target->GetImages().Append(module_sp: jit_module_sp); |
735 | } |
736 | } |
737 | |
738 | Process *process = exe_ctx.GetProcessPtr(); |
739 | if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS) |
740 | m_jit_process_wp = lldb::ProcessWP(process->shared_from_this()); |
741 | return true; |
742 | } |
743 | |
744 | /// Converts an absolute position inside a given code string into |
745 | /// a column/line pair. |
746 | /// |
747 | /// \param[in] abs_pos |
748 | /// A absolute position in the code string that we want to convert |
749 | /// to a column/line pair. |
750 | /// |
751 | /// \param[in] code |
752 | /// A multi-line string usually representing source code. |
753 | /// |
754 | /// \param[out] line |
755 | /// The line in the code that contains the given absolute position. |
756 | /// The first line in the string is indexed as 1. |
757 | /// |
758 | /// \param[out] column |
759 | /// The column in the line that contains the absolute position. |
760 | /// The first character in a line is indexed as 0. |
761 | static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code, |
762 | unsigned &line, unsigned &column) { |
763 | // Reset to code position to beginning of the file. |
764 | line = 0; |
765 | column = 0; |
766 | |
767 | assert(abs_pos <= code.size() && "Absolute position outside code string?" ); |
768 | |
769 | // We have to walk up to the position and count lines/columns. |
770 | for (std::size_t i = 0; i < abs_pos; ++i) { |
771 | // If we hit a line break, we go back to column 0 and enter a new line. |
772 | // We only handle \n because that's what we internally use to make new |
773 | // lines for our temporary code strings. |
774 | if (code[i] == '\n') { |
775 | ++line; |
776 | column = 0; |
777 | continue; |
778 | } |
779 | ++column; |
780 | } |
781 | } |
782 | |
783 | bool ClangUserExpression::Complete(ExecutionContext &exe_ctx, |
784 | CompletionRequest &request, |
785 | unsigned complete_pos) { |
786 | Log *log = GetLog(mask: LLDBLog::Expressions); |
787 | |
788 | // We don't want any visible feedback when completing an expression. Mostly |
789 | // because the results we get from an incomplete invocation are probably not |
790 | // correct. |
791 | DiagnosticManager diagnostic_manager; |
792 | |
793 | if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ true)) |
794 | return false; |
795 | |
796 | LLDB_LOGF(log, "Parsing the following code:\n%s" , m_transformed_text.c_str()); |
797 | |
798 | ////////////////////////// |
799 | // Parse the expression |
800 | // |
801 | |
802 | m_materializer_up = std::make_unique<Materializer>(); |
803 | |
804 | ResetDeclMap(exe_ctx, result_delegate&: m_result_delegate, /*keep result in memory*/ keep_result_in_memory: true); |
805 | |
806 | auto on_exit = llvm::make_scope_exit(F: [this]() { ResetDeclMap(); }); |
807 | |
808 | if (!DeclMap()->WillParse(exe_ctx, materializer: GetMaterializer())) { |
809 | diagnostic_manager.PutString( |
810 | severity: lldb::eSeverityError, |
811 | str: "current process state is unsuitable for expression parsing" ); |
812 | |
813 | return false; |
814 | } |
815 | |
816 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { |
817 | DeclMap()->SetLookupsEnabled(true); |
818 | } |
819 | |
820 | ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this, |
821 | false); |
822 | |
823 | // We have to find the source code location where the user text is inside |
824 | // the transformed expression code. When creating the transformed text, we |
825 | // already stored the absolute position in the m_transformed_text string. The |
826 | // only thing left to do is to transform it into the line:column format that |
827 | // Clang expects. |
828 | |
829 | // The line and column of the user expression inside the transformed source |
830 | // code. |
831 | unsigned user_expr_line, user_expr_column; |
832 | if (m_user_expression_start_pos) |
833 | AbsPosToLineColumnPos(abs_pos: *m_user_expression_start_pos, code: m_transformed_text, |
834 | line&: user_expr_line, column&: user_expr_column); |
835 | else |
836 | return false; |
837 | |
838 | // The actual column where we have to complete is the start column of the |
839 | // user expression + the offset inside the user code that we were given. |
840 | const unsigned completion_column = user_expr_column + complete_pos; |
841 | parser.Complete(request, line: user_expr_line, pos: completion_column, typed_pos: complete_pos); |
842 | |
843 | return true; |
844 | } |
845 | |
846 | lldb::addr_t ClangUserExpression::GetCppObjectPointer( |
847 | lldb::StackFrameSP frame_sp, llvm::StringRef object_name, Status &err) { |
848 | auto valobj_sp = |
849 | GetObjectPointerValueObject(frame: std::move(frame_sp), object_name, err); |
850 | |
851 | // We're inside a C++ class method. This could potentially be an unnamed |
852 | // lambda structure. If the lambda captured a "this", that should be |
853 | // the object pointer. |
854 | if (auto thisChildSP = valobj_sp->GetChildMemberWithName(name: "this" )) { |
855 | valobj_sp = thisChildSP; |
856 | } |
857 | |
858 | if (!err.Success() || !valobj_sp.get()) |
859 | return LLDB_INVALID_ADDRESS; |
860 | |
861 | lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
862 | |
863 | if (ret == LLDB_INVALID_ADDRESS) { |
864 | err = Status::FromErrorStringWithFormatv( |
865 | format: "Couldn't load '{0}' because its value couldn't be evaluated" , |
866 | args&: object_name); |
867 | return LLDB_INVALID_ADDRESS; |
868 | } |
869 | |
870 | return ret; |
871 | } |
872 | |
873 | bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx, |
874 | std::vector<lldb::addr_t> &args, |
875 | lldb::addr_t struct_address, |
876 | DiagnosticManager &diagnostic_manager) { |
877 | lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS; |
878 | lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS; |
879 | |
880 | if (m_needs_object_ptr) { |
881 | lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP(); |
882 | if (!frame_sp) |
883 | return true; |
884 | |
885 | if (!m_in_cplusplus_method && !m_in_objectivec_method) { |
886 | diagnostic_manager.PutString( |
887 | severity: lldb::eSeverityError, |
888 | str: "need object pointer but don't know the language" ); |
889 | return false; |
890 | } |
891 | |
892 | static constexpr llvm::StringLiteral g_cplusplus_object_name("this" ); |
893 | static constexpr llvm::StringLiteral g_objc_object_name("self" ); |
894 | llvm::StringRef object_name = |
895 | m_in_cplusplus_method ? g_cplusplus_object_name : g_objc_object_name; |
896 | |
897 | Status object_ptr_error; |
898 | |
899 | if (m_ctx_obj) { |
900 | ValueObject::AddrAndType address = m_ctx_obj->GetAddressOf(scalar_is_load_address: false); |
901 | if (address.address == LLDB_INVALID_ADDRESS || |
902 | address.type != eAddressTypeLoad) |
903 | object_ptr_error = Status::FromErrorString(str: "Can't get context object's " |
904 | "debuggee address" ); |
905 | else |
906 | object_ptr = address.address; |
907 | } else { |
908 | if (m_in_cplusplus_method) { |
909 | object_ptr = |
910 | GetCppObjectPointer(frame_sp, object_name, err&: object_ptr_error); |
911 | } else { |
912 | object_ptr = GetObjectPointer(frame_sp, object_name, err&: object_ptr_error); |
913 | } |
914 | } |
915 | |
916 | if (!object_ptr_error.Success()) { |
917 | exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Format( |
918 | format: "warning: `{0}' is not accessible (substituting 0). {1}\n" , |
919 | args&: object_name, args: object_ptr_error.AsCString()); |
920 | object_ptr = 0; |
921 | } |
922 | |
923 | if (m_in_objectivec_method) { |
924 | static constexpr llvm::StringLiteral cmd_name("_cmd" ); |
925 | |
926 | cmd_ptr = GetObjectPointer(frame_sp, object_name: cmd_name, err&: object_ptr_error); |
927 | |
928 | if (!object_ptr_error.Success()) { |
929 | diagnostic_manager.Printf( |
930 | severity: lldb::eSeverityWarning, |
931 | format: "couldn't get cmd pointer (substituting NULL): %s" , |
932 | object_ptr_error.AsCString()); |
933 | cmd_ptr = 0; |
934 | } |
935 | } |
936 | |
937 | args.push_back(x: object_ptr); |
938 | |
939 | if (m_in_objectivec_method) |
940 | args.push_back(x: cmd_ptr); |
941 | |
942 | args.push_back(x: struct_address); |
943 | } else { |
944 | args.push_back(x: struct_address); |
945 | } |
946 | return true; |
947 | } |
948 | |
949 | lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization( |
950 | ExecutionContextScope *exe_scope) { |
951 | return m_result_delegate.GetVariable(); |
952 | } |
953 | |
954 | char ClangUserExpression::ClangUserExpressionHelper::ID; |
955 | |
956 | void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap( |
957 | ExecutionContext &exe_ctx, |
958 | Materializer::PersistentVariableDelegate &delegate, |
959 | bool keep_result_in_memory, |
960 | ValueObject *ctx_obj) { |
961 | std::shared_ptr<ClangASTImporter> ast_importer; |
962 | auto *state = exe_ctx.GetTargetSP()->GetPersistentExpressionStateForLanguage( |
963 | language: lldb::eLanguageTypeC); |
964 | if (state) { |
965 | auto *persistent_vars = llvm::cast<ClangPersistentVariables>(Val: state); |
966 | ast_importer = persistent_vars->GetClangASTImporter(); |
967 | } |
968 | m_expr_decl_map_up = std::make_unique<ClangExpressionDeclMap>( |
969 | args&: keep_result_in_memory, args: &delegate, args: exe_ctx.GetTargetSP(), args&: ast_importer, |
970 | args&: ctx_obj); |
971 | } |
972 | |
973 | clang::ASTConsumer * |
974 | ClangUserExpression::ClangUserExpressionHelper::ASTTransformer( |
975 | clang::ASTConsumer *passthrough) { |
976 | m_result_synthesizer_up = std::make_unique<ASTResultSynthesizer>( |
977 | args&: passthrough, args&: m_top_level, args&: m_target); |
978 | |
979 | return m_result_synthesizer_up.get(); |
980 | } |
981 | |
982 | void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() { |
983 | if (m_result_synthesizer_up) { |
984 | m_result_synthesizer_up->CommitPersistentDecls(); |
985 | } |
986 | } |
987 | |
988 | ConstString ClangUserExpression::ResultDelegate::GetName() { |
989 | return m_persistent_state->GetNextPersistentVariableName(is_error: false); |
990 | } |
991 | |
992 | void ClangUserExpression::ResultDelegate::DidDematerialize( |
993 | lldb::ExpressionVariableSP &variable) { |
994 | m_variable = variable; |
995 | } |
996 | |
997 | void ClangUserExpression::ResultDelegate::RegisterPersistentState( |
998 | PersistentExpressionState *persistent_state) { |
999 | m_persistent_state = persistent_state; |
1000 | } |
1001 | |
1002 | lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() { |
1003 | return m_variable; |
1004 | } |
1005 | |