Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 1 | //===-- ClangUserExpression.cpp -------------------------------------*- 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 | // C Includes |
| 11 | #include <stdio.h> |
| 12 | #if HAVE_SYS_TYPES_H |
| 13 | # include <sys/types.h> |
| 14 | #endif |
| 15 | |
| 16 | // C++ Includes |
| 17 | |
| 18 | #include "lldb/Core/ConstString.h" |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Log.h" |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Stream.h" |
Greg Clayton | c71899e | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 21 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 23 | #include "lldb/Expression/ClangExpressionParser.h" |
| 24 | #include "lldb/Expression/ClangUtilityFunction.h" |
| 25 | #include "lldb/Host/Host.h" |
| 26 | #include "lldb/Target/ExecutionContext.h" |
| 27 | #include "lldb/Target/Target.h" |
| 28 | |
| 29 | using namespace lldb_private; |
| 30 | |
| 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. |
| 39 | //------------------------------------------------------------------ |
| 40 | ClangUtilityFunction::ClangUtilityFunction (const char *text, |
| 41 | const char *name) : |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 42 | ClangExpression (), |
| 43 | m_function_text (text), |
| 44 | m_function_name (name) |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
Greg Clayton | 6a5aa8a | 2010-09-24 23:07:41 +0000 | [diff] [blame] | 48 | ClangUtilityFunction::~ClangUtilityFunction () |
| 49 | { |
| 50 | } |
| 51 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 52 | //------------------------------------------------------------------ |
| 53 | /// Install the utility function into a process |
| 54 | /// |
| 55 | /// @param[in] error_stream |
| 56 | /// A stream to print parse errors and warnings to. |
| 57 | /// |
| 58 | /// @param[in] exe_ctx |
| 59 | /// The execution context to install the utility function to. |
| 60 | /// |
| 61 | /// @return |
| 62 | /// True on success (no errors); false otherwise. |
| 63 | //------------------------------------------------------------------ |
| 64 | bool |
| 65 | ClangUtilityFunction::Install (Stream &error_stream, |
| 66 | ExecutionContext &exe_ctx) |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 67 | { |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 68 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 69 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 70 | if (m_jit_start_addr != LLDB_INVALID_ADDRESS) |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 71 | { |
| 72 | error_stream.PutCString("error: already installed\n"); |
| 73 | return false; |
| 74 | } |
| 75 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 76 | //////////////////////////////////// |
| 77 | // Set up the target and compiler |
| 78 | // |
| 79 | |
| 80 | Target *target = exe_ctx.target; |
| 81 | |
| 82 | if (!target) |
| 83 | { |
| 84 | error_stream.PutCString ("error: invalid target\n"); |
| 85 | return false; |
| 86 | } |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 87 | |
| 88 | Process *process = exe_ctx.process; |
| 89 | |
| 90 | if (!process) |
| 91 | { |
| 92 | error_stream.PutCString ("error: invalid process\n"); |
| 93 | return false; |
| 94 | } |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 95 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 96 | ////////////////////////// |
| 97 | // Parse the expression |
| 98 | // |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 99 | |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 100 | bool keep_result_in_memory = false; |
| 101 | |
| 102 | m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory)); |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 103 | |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 104 | m_data_allocator.reset(new ProcessDataAllocator(*exe_ctx.process)); |
| 105 | |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 106 | m_expr_decl_map->WillParse(exe_ctx); |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 107 | |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 108 | ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this); |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 109 | |
| 110 | unsigned num_errors = parser.Parse (error_stream); |
| 111 | |
| 112 | if (num_errors) |
| 113 | { |
| 114 | error_stream.Printf ("error: %d errors parsing expression\n", num_errors); |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 115 | |
| 116 | m_expr_decl_map.reset(); |
| 117 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
| 121 | ////////////////////////////////// |
| 122 | // JIT the output of the parser |
| 123 | // |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 124 | |
| 125 | lldb::ClangExpressionVariableSP const_result; |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 126 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 127 | |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 128 | Error jit_error = parser.MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx, m_data_allocator.get(), const_result); |
| 129 | |
| 130 | if (log) |
| 131 | { |
| 132 | StreamString dump_string; |
| 133 | m_data_allocator->Dump(dump_string); |
| 134 | |
| 135 | log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str()); |
| 136 | } |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 137 | |
| 138 | if (exe_ctx.process && m_jit_start_addr != LLDB_INVALID_ADDRESS) |
| 139 | m_jit_process_sp = exe_ctx.process->GetSP(); |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 140 | |
Greg Clayton | c71899e | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 141 | #if 0 |
| 142 | // jingham: look here |
| 143 | StreamFile logfile ("/tmp/exprs.txt", "a"); |
| 144 | logfile.Printf ("0x%16.16llx: func = %s, source =\n%s\n", |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 145 | m_jit_start_addr, |
Greg Clayton | c71899e | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 146 | m_function_name.c_str(), |
| 147 | m_function_text.c_str()); |
| 148 | #endif |
| 149 | |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 150 | m_expr_decl_map->DidParse(); |
| 151 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 152 | m_expr_decl_map.reset(); |
| 153 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 154 | if (jit_error.Success()) |
| 155 | { |
| 156 | return true; |
| 157 | } |
| 158 | else |
| 159 | { |
Greg Clayton | 3058197 | 2011-05-17 03:51:29 +0000 | [diff] [blame] | 160 | const char *error_cstr = jit_error.AsCString(); |
| 161 | if (error_cstr && error_cstr[0]) |
| 162 | error_stream.Printf ("error: %s\n", error_cstr); |
| 163 | else |
| 164 | error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors); |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |