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