ClangUserExpression.h revision 47beabb1386be44e3f90dbc30a0b22c23b93a4dc
1//===-- ClangUserExpression.h -----------------------------------*- 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#ifndef liblldb_ClangUserExpression_h_ 11#define liblldb_ClangUserExpression_h_ 12 13// C Includes 14// C++ Includes 15#include <string> 16#include <map> 17#include <memory> 18#include <vector> 19 20// Other libraries and framework includes 21// Project includes 22 23#include "lldb/lldb-forward.h" 24#include "lldb/lldb-private.h" 25#include "lldb/Core/ClangForward.h" 26#include "lldb/Expression/ClangExpression.h" 27#include "lldb/Expression/ClangExpressionVariable.h" 28#include "lldb/Expression/IRForTarget.h" 29#include "lldb/Expression/ProcessDataAllocator.h" 30#include "lldb/Symbol/TaggedASTType.h" 31#include "lldb/Target/ExecutionContext.h" 32 33#include "llvm/ExecutionEngine/JITMemoryManager.h" 34 35namespace lldb_private 36{ 37 38//---------------------------------------------------------------------- 39/// @class ClangUserExpression ClangUserExpression.h "lldb/Expression/ClangUserExpression.h" 40/// @brief Encapsulates a single expression for use with Clang 41/// 42/// LLDB uses expressions for various purposes, notably to call functions 43/// and as a backend for the expr command. ClangUserExpression encapsulates 44/// the objects needed to parse and interpret or JIT an expression. It 45/// uses the Clang parser to produce LLVM IR from the expression. 46//---------------------------------------------------------------------- 47class ClangUserExpression : public ClangExpression 48{ 49public: 50 typedef STD_SHARED_PTR(ClangUserExpression) ClangUserExpressionSP; 51 52 //------------------------------------------------------------------ 53 /// Constructor 54 /// 55 /// @param[in] expr 56 /// The expression to parse. 57 /// 58 /// @param[in] expr_prefix 59 /// If non-NULL, a C string containing translation-unit level 60 /// definitions to be included when the expression is parsed. 61 /// 62 /// @param[in] language 63 /// If not eLanguageTypeUnknown, a language to use when parsing 64 /// the expression. Currently restricted to those languages 65 /// supported by Clang. 66 /// 67 /// @param[in] desired_type 68 /// If not eResultTypeAny, the type to use for the expression 69 /// result. 70 //------------------------------------------------------------------ 71 ClangUserExpression (const char *expr, 72 const char *expr_prefix, 73 lldb::LanguageType language, 74 ResultType desired_type); 75 76 //------------------------------------------------------------------ 77 /// Destructor 78 //------------------------------------------------------------------ 79 virtual 80 ~ClangUserExpression (); 81 82 //------------------------------------------------------------------ 83 /// Parse the expression 84 /// 85 /// @param[in] error_stream 86 /// A stream to print parse errors and warnings to. 87 /// 88 /// @param[in] exe_ctx 89 /// The execution context to use when looking up entities that 90 /// are needed for parsing (locations of functions, types of 91 /// variables, persistent variables, etc.) 92 /// 93 /// @param[in] execution_policy 94 /// Determines whether interpretation is possible or mandatory. 95 /// 96 /// @param[in] keep_result_in_memory 97 /// True if the resulting persistent variable should reside in 98 /// target memory, if applicable. 99 /// 100 /// @return 101 /// True on success (no errors); false otherwise. 102 //------------------------------------------------------------------ 103 bool 104 Parse (Stream &error_stream, 105 ExecutionContext &exe_ctx, 106 lldb_private::ExecutionPolicy execution_policy, 107 bool keep_result_in_memory); 108 109 //------------------------------------------------------------------ 110 /// Execute the parsed expression 111 /// 112 /// @param[in] error_stream 113 /// A stream to print errors to. 114 /// 115 /// @param[in] exe_ctx 116 /// The execution context to use when looking up entities that 117 /// are needed for parsing (locations of variables, etc.) 118 /// 119 /// @param[in] discard_on_error 120 /// If true, and the execution stops before completion, we unwind the 121 /// function call, and return the program state to what it was before the 122 /// execution. If false, we leave the program in the stopped state. 123 /// 124 /// @param[in] shared_ptr_to_me 125 /// This is a shared pointer to this ClangUserExpression. This is 126 /// needed because Execute can push a thread plan that will hold onto 127 /// the ClangUserExpression for an unbounded period of time. So you 128 /// need to give the thread plan a reference to this object that can 129 /// keep it alive. 130 /// 131 /// @param[in] result 132 /// A pointer to direct at the persistent variable in which the 133 /// expression's result is stored. 134 /// 135 /// @param[in] try_all_threads 136 /// If true, then we will try to run all threads if the function doesn't complete on 137 /// one thread. See timeout_usec for the interaction of this variable and 138 /// the timeout. 139 /// 140 /// @param[in] timeout_usec 141 /// Timeout value (0 for no timeout). If try_all_threads is true, then we 142 /// will try on one thread for the lesser of .25 sec and half the total timeout. 143 /// then switch to running all threads, otherwise this will be the total timeout. 144 /// 145 /// 146 /// @return 147 /// A Process::Execution results value. 148 //------------------------------------------------------------------ 149 ExecutionResults 150 Execute (Stream &error_stream, 151 ExecutionContext &exe_ctx, 152 bool discard_on_error, 153 ClangUserExpressionSP &shared_ptr_to_me, 154 lldb::ClangExpressionVariableSP &result, 155 bool try_all_threads = true, 156 uint32_t timeout_usec = 500000); 157 158 ThreadPlan * 159 GetThreadPlanToExecuteJITExpression (Stream &error_stream, 160 ExecutionContext &exe_ctx); 161 162 //------------------------------------------------------------------ 163 /// Apply the side effects of the function to program state. 164 /// 165 /// @param[in] error_stream 166 /// A stream to print errors to. 167 /// 168 /// @param[in] exe_ctx 169 /// The execution context to use when looking up entities that 170 /// are needed for parsing (locations of variables, etc.) 171 /// 172 /// @param[in] result 173 /// A pointer to direct at the persistent variable in which the 174 /// expression's result is stored. 175 /// 176 /// @param[in] function_stack_pointer 177 /// A pointer to the base of the function's stack frame. This 178 /// is used to determine whether the expession result resides in 179 /// memory that will still be valid, or whether it needs to be 180 /// treated as homeless for the purpose of future expressions. 181 /// 182 /// @return 183 /// A Process::Execution results value. 184 //------------------------------------------------------------------ 185 bool 186 FinalizeJITExecution (Stream &error_stream, 187 ExecutionContext &exe_ctx, 188 lldb::ClangExpressionVariableSP &result, 189 lldb::addr_t function_stack_pointer = LLDB_INVALID_ADDRESS); 190 191 //------------------------------------------------------------------ 192 /// Return the string that the parser should parse. Must be a full 193 /// translation unit. 194 //------------------------------------------------------------------ 195 const char * 196 Text () 197 { 198 return m_transformed_text.c_str(); 199 } 200 201 //------------------------------------------------------------------ 202 /// Return the string that the user typed. 203 //------------------------------------------------------------------ 204 const char * 205 GetUserText () 206 { 207 return m_expr_text.c_str(); 208 } 209 210 //------------------------------------------------------------------ 211 /// Return the function name that should be used for executing the 212 /// expression. Text() should contain the definition of this 213 /// function. 214 //------------------------------------------------------------------ 215 const char * 216 FunctionName () 217 { 218 return "$__lldb_expr"; 219 } 220 221 //------------------------------------------------------------------ 222 /// Return the language that should be used when parsing. To use 223 /// the default, return eLanguageTypeUnknown. 224 //------------------------------------------------------------------ 225 virtual lldb::LanguageType 226 Language () 227 { 228 return m_language; 229 } 230 231 //------------------------------------------------------------------ 232 /// Return the object that the parser should use when resolving external 233 /// values. May be NULL if everything should be self-contained. 234 //------------------------------------------------------------------ 235 ClangExpressionDeclMap * 236 DeclMap () 237 { 238 return m_expr_decl_map.get(); 239 } 240 241 //------------------------------------------------------------------ 242 /// Return the object that the parser should use when registering 243 /// local variables. May be NULL if the Expression doesn't care. 244 //------------------------------------------------------------------ 245 ClangExpressionVariableList * 246 LocalVariables () 247 { 248 return m_local_variables.get(); 249 } 250 251 //------------------------------------------------------------------ 252 /// Return the object that the parser should allow to access ASTs. 253 /// May be NULL if the ASTs do not need to be transformed. 254 /// 255 /// @param[in] passthrough 256 /// The ASTConsumer that the returned transformer should send 257 /// the ASTs to after transformation. 258 //------------------------------------------------------------------ 259 clang::ASTConsumer * 260 ASTTransformer (clang::ASTConsumer *passthrough); 261 262 //------------------------------------------------------------------ 263 /// Return the desired result type of the function, or 264 /// eResultTypeAny if indifferent. 265 //------------------------------------------------------------------ 266 virtual ResultType 267 DesiredResultType () 268 { 269 return m_desired_type; 270 } 271 272 //------------------------------------------------------------------ 273 /// Return true if validation code should be inserted into the 274 /// expression. 275 //------------------------------------------------------------------ 276 bool 277 NeedsValidation () 278 { 279 return true; 280 } 281 282 //------------------------------------------------------------------ 283 /// Return true if external variables in the expression should be 284 /// resolved. 285 //------------------------------------------------------------------ 286 bool 287 NeedsVariableResolution () 288 { 289 return true; 290 } 291 292 //------------------------------------------------------------------ 293 /// Evaluate one expression and return its result. 294 /// 295 /// @param[in] exe_ctx 296 /// The execution context to use when evaluating the expression. 297 /// 298 /// @param[in] execution_policy 299 /// Determines whether or not to try using the IR interpreter to 300 /// avoid running the expression on the parser. 301 /// 302 /// @param[in] language 303 /// If not eLanguageTypeUnknown, a language to use when parsing 304 /// the expression. Currently restricted to those languages 305 /// supported by Clang. 306 /// 307 /// @param[in] discard_on_error 308 /// True if the thread's state should be restored in the case 309 /// of an error. 310 /// 311 /// @param[in] result_type 312 /// If not eResultTypeAny, the type of the desired result. Will 313 /// result in parse errors if impossible. 314 /// 315 /// @param[in] expr_cstr 316 /// A C string containing the expression to be evaluated. 317 /// 318 /// @param[in] expr_prefix 319 /// If non-NULL, a C string containing translation-unit level 320 /// definitions to be included when the expression is parsed. 321 /// 322 /// @param[in/out] result_valobj_sp 323 /// If execution is successful, the result valobj is placed here. 324 /// 325 /// @param[in] try_all_threads 326 /// If true, then we will try to run all threads if the function doesn't complete on 327 /// one thread. See timeout_usec for the interaction of this variable and 328 /// the timeout. 329 /// 330 /// @param[in] timeout_usec 331 /// Timeout value (0 for no timeout). If try_all_threads is true, then we 332 /// will try on one thread for the lesser of .25 sec and half the total timeout. 333 /// then switch to running all threads, otherwise this will be the total timeout. 334 /// 335 /// @result 336 /// A Process::ExecutionResults value. eExecutionCompleted for success. 337 //------------------------------------------------------------------ 338 static ExecutionResults 339 Evaluate (ExecutionContext &exe_ctx, 340 lldb_private::ExecutionPolicy execution_policy, 341 lldb::LanguageType language, 342 ResultType desired_type, 343 bool discard_on_error, 344 const char *expr_cstr, 345 const char *expr_prefix, 346 lldb::ValueObjectSP &result_valobj_sp, 347 bool try_all_threads = true, 348 uint32_t timeout_usec = 500000); 349 350 static ExecutionResults 351 EvaluateWithError (ExecutionContext &exe_ctx, 352 lldb_private::ExecutionPolicy execution_policy, 353 lldb::LanguageType language, 354 ResultType desired_type, 355 bool discard_on_error, 356 const char *expr_cstr, 357 const char *expr_prefix, 358 lldb::ValueObjectSP &result_valobj_sp, 359 Error &error, 360 bool try_all_threads = true, 361 uint32_t timeout_usec = 500000); 362 363 static const Error::ValueType kNoResult = 0x1001; ///< ValueObject::GetError() returns this if there is no result from the expression. 364private: 365 //------------------------------------------------------------------ 366 /// Populate m_cplusplus and m_objetivec based on the environment. 367 //------------------------------------------------------------------ 368 369 void 370 ScanContext (ExecutionContext &exe_ctx, 371 lldb_private::Error &err); 372 373 bool 374 PrepareToExecuteJITExpression (Stream &error_stream, 375 ExecutionContext &exe_ctx, 376 lldb::addr_t &struct_address, 377 lldb::addr_t &object_ptr, 378 lldb::addr_t &cmd_ptr); 379 380 bool 381 EvaluatedStatically () 382 { 383 return m_evaluated_statically; 384 } 385 386 void 387 InstallContext (ExecutionContext &exe_ctx) 388 { 389 m_process_wp = exe_ctx.GetProcessSP(); 390 m_target_wp = exe_ctx.GetTargetSP(); 391 m_frame_wp = exe_ctx.GetFrameSP(); 392 } 393 394 bool 395 LockAndCheckContext (ExecutionContext &exe_ctx, 396 lldb::TargetSP &target_sp, 397 lldb::ProcessSP &process_sp, 398 lldb::StackFrameSP &frame_sp) 399 { 400 target_sp = m_target_wp.lock(); 401 process_sp = m_process_wp.lock(); 402 frame_sp = m_frame_wp.lock(); 403 404 if ((target_sp && target_sp.get() != exe_ctx.GetTargetPtr()) || 405 (process_sp && process_sp.get() != exe_ctx.GetProcessPtr()) || 406 (frame_sp && frame_sp.get() != exe_ctx.GetFramePtr())) 407 return false; 408 409 return true; 410 } 411 412 lldb::TargetWP m_target_wp; ///< The target used as the context for the expression. 413 lldb::ProcessWP m_process_wp; ///< The process used as the context for the expression. 414 lldb::StackFrameWP m_frame_wp; ///< The stack frame used as context for the expression. 415 416 std::string m_expr_text; ///< The text of the expression, as typed by the user 417 std::string m_expr_prefix; ///< The text of the translation-level definitions, as provided by the user 418 lldb::LanguageType m_language; ///< The language to use when parsing (eLanguageTypeUnknown means use defaults) 419 bool m_allow_cxx; ///< True if the language allows C++. 420 bool m_allow_objc; ///< True if the language allows Objective-C. 421 std::string m_transformed_text; ///< The text of the expression, as send to the parser 422 ResultType m_desired_type; ///< The type to coerce the expression's result to. If eResultTypeAny, inferred from the expression. 423 424 std::auto_ptr<ClangExpressionDeclMap> m_expr_decl_map; ///< The map to use when parsing and materializing the expression. 425 std::auto_ptr<ClangExpressionVariableList> m_local_variables; ///< The local expression variables, if the expression is DWARF. 426 std::auto_ptr<ProcessDataAllocator> m_data_allocator; ///< The allocator that the parser uses to place strings for use by JIT-compiled code. 427 428 std::auto_ptr<ASTResultSynthesizer> m_result_synthesizer; ///< The result synthesizer, if one is needed. 429 430 bool m_enforce_valid_object; ///< True if the expression parser should enforce the presence of a valid class pointer in order to generate the expression as a method. 431 bool m_cplusplus; ///< True if the expression is compiled as a C++ member function (true if it was parsed when exe_ctx was in a C++ method). 432 bool m_objectivec; ///< True if the expression is compiled as an Objective-C method (true if it was parsed when exe_ctx was in an Objective-C method). 433 bool m_static_method; ///< True if the expression is compiled as a static (or class) method (currently true if it was parsed when exe_ctx was in an Objective-C class method). 434 bool m_needs_object_ptr; ///< True if "this" or "self" must be looked up and passed in. False if the expression doesn't really use them and they can be NULL. 435 bool m_const_object; ///< True if "this" is const. 436 Target *m_target; ///< The target for storing persistent data like types and variables. 437 438 bool m_evaluated_statically; ///< True if the expression could be evaluated statically; false otherwise. 439 lldb::ClangExpressionVariableSP m_const_result; ///< The statically-computed result of the expression. NULL if it could not be computed statically or the expression has side effects. 440}; 441 442} // namespace lldb_private 443 444#endif // liblldb_ClangUserExpression_h_ 445