LLDB  mainline
ClangUtilityFunction.cpp
Go to the documentation of this file.
1 //===-- ClangUtilityFunction.cpp ---------------------------------*- C++-*-===//
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 "ClangUtilityFunction.h"
10 #include "ClangExpressionDeclMap.h"
11 #include "ClangExpressionParser.h"
13 
14 #include <stdio.h>
15 #if HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18 
19 
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Host/Host.h"
25 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/Stream.h"
29 
30 using namespace lldb_private;
31 
32 /// Constructor
33 ///
34 /// \param[in] text
35 /// The text of the function. Must be a full translation unit.
36 ///
37 /// \param[in] name
38 /// The name of the function, as used in the text.
40  const char *text, const char *name)
41  : UtilityFunction(exe_scope, text, name, eKindClangUtilityFunction) {
43  if (text && text[0])
44  m_function_text.append(text);
45 }
46 
48 
49 /// Install the utility function into a process
50 ///
51 /// \param[in] diagnostic_manager
52 /// A diagnostic manager to report errors and warnings to.
53 ///
54 /// \param[in] exe_ctx
55 /// The execution context to install the utility function to.
56 ///
57 /// \return
58 /// True on success (no errors); false otherwise.
60  ExecutionContext &exe_ctx) {
62  diagnostic_manager.PutString(eDiagnosticSeverityWarning,
63  "already installed");
64  return false;
65  }
66 
67  ////////////////////////////////////
68  // Set up the target and compiler
69  //
70 
71  Target *target = exe_ctx.GetTargetPtr();
72 
73  if (!target) {
74  diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
75  return false;
76  }
77 
78  Process *process = exe_ctx.GetProcessPtr();
79 
80  if (!process) {
81  diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid process");
82  return false;
83  }
84 
85  //////////////////////////
86  // Parse the expression
87  //
88 
89  bool keep_result_in_memory = false;
90 
91  ResetDeclMap(exe_ctx, keep_result_in_memory);
92 
93  if (!DeclMap()->WillParse(exe_ctx, NULL)) {
94  diagnostic_manager.PutString(
96  "current process state is unsuitable for expression parsing");
97  return false;
98  }
99 
100  const bool generate_debug_info = true;
101  ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this,
102  generate_debug_info);
103 
104  unsigned num_errors = parser.Parse(diagnostic_manager);
105 
106  if (num_errors) {
107  ResetDeclMap();
108 
109  return false;
110  }
111 
112  //////////////////////////////////
113  // JIT the output of the parser
114  //
115 
116  bool can_interpret = false; // should stay that way
117 
118  Status jit_error = parser.PrepareForExecution(
120  can_interpret, eExecutionPolicyAlways);
121 
123  m_jit_process_wp = process->shared_from_this();
124  if (parser.GetGenerateDebugInfo()) {
125  lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
126 
127  if (jit_module_sp) {
128  ConstString const_func_name(FunctionName());
129  FileSpec jit_file;
130  jit_file.GetFilename() = const_func_name;
131  jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
132  m_jit_module_wp = jit_module_sp;
133  target->GetImages().Append(jit_module_sp);
134  }
135  }
136  }
137 
138  DeclMap()->DidParse();
139 
140  ResetDeclMap();
141 
142  if (jit_error.Success()) {
143  return true;
144  } else {
145  const char *error_cstr = jit_error.AsCString();
146  if (error_cstr && error_cstr[0]) {
147  diagnostic_manager.Printf(eDiagnosticSeverityError, "%s", error_cstr);
148  } else {
149  diagnostic_manager.PutString(eDiagnosticSeverityError,
150  "expression can't be interpreted or run");
151  }
152  return false;
153  }
154 }
155 
157  ExecutionContext &exe_ctx, bool keep_result_in_memory) {
158  m_expr_decl_map_up.reset(
159  new ClangExpressionDeclMap(keep_result_in_memory, nullptr, exe_ctx,
160  nullptr));
161 }
ConstString & GetFilename()
Filename string get accessor.
Definition: FileSpec.cpp:369
lldb::addr_t m_jit_start_addr
An expression might have a process, but it doesn&#39;t need to (e.g.
Definition: Expression.h:104
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
std::string m_function_text
The text of the function.
A file utility class.
Definition: FileSpec.h:55
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void DidParse()
Disable the state needed for parsing and IR transformation.
std::shared_ptr< IRExecutionUnit > m_execution_unit_sp
Target * GetTargetPtr() const
Returns a pointer to the target object.
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
bool Install(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) override
Install the utility function into a process.
unsigned Parse(DiagnosticManager &diagnostic_manager) override
Parse a single expression and convert it to IR using Clang.
size_t Printf(DiagnosticSeverity severity, const char *format,...) __attribute__((format(printf
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
Process * GetProcessPtr() const
Returns a pointer to the process object.
size_t size_t PutString(DiagnosticSeverity severity, llvm::StringRef str)
lldb::ProcessWP m_jit_process_wp
Expression&#39;s always have to have a target...
Definition: Expression.h:101
bool Success() const
Test for success condition.
Definition: Status.cpp:287
const char * FunctionName() override
Return the function name that should be used for executing the expression.
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:899
lldb::addr_t m_jit_end_addr
The address of the JITted function within the JIT allocation.
Definition: Expression.h:107
A uniqued constant string class.
Definition: ConstString.h:38
ClangUtilityFunction(ExecutionContextScope &exe_scope, const char *text, const char *name)
Constructor.
"lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of Clang that can parse expression...
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
"lldb/Expression/UtilityFunction.h" Encapsulates a bit of source code that provides a function that i...
ExecutionContextScope * GetBestExecutionContextScope() const
ClangExpressionDeclMap * DeclMap()
"lldb/Expression/ClangExpressionDeclMap.h" Manages named entities that are defined in LLDB&#39;s debug in...
An error handling class.
Definition: Status.h:44