blob: 628952fe6b85b193028956ef3bb8a9825d415ee6 [file] [log] [blame]
Jim Ingham151c0322015-09-15 21:13:50 +00001//===-- ClangFunctionCallerCaller.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
11// C Includes
12// C++ Includes
13// Other libraries and framework includes
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/RecordLayout.h"
16#include "clang/CodeGen/CodeGenAction.h"
17#include "clang/CodeGen/ModuleBuilder.h"
18#include "clang/Frontend/CompilerInstance.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Triple.h"
21#include "llvm/ExecutionEngine/ExecutionEngine.h"
22#include "llvm/IR/Module.h"
23
24// Project includes
25#include "lldb/Core/DataExtractor.h"
26#include "lldb/Core/Log.h"
27#include "lldb/Core/Module.h"
28#include "lldb/Core/State.h"
29#include "lldb/Core/ValueObject.h"
30#include "lldb/Core/ValueObjectList.h"
31#include "lldb/Expression/ASTStructExtractor.h"
32#include "lldb/Expression/ClangExpressionParser.h"
33#include "lldb/Expression/ClangFunctionCaller.h"
34#include "lldb/Expression/IRExecutionUnit.h"
35#include "lldb/Interpreter/CommandReturnObject.h"
36#include "lldb/Symbol/ClangASTContext.h"
37#include "lldb/Symbol/Function.h"
38#include "lldb/Symbol/Type.h"
39#include "lldb/Target/ExecutionContext.h"
40#include "lldb/Target/Process.h"
41#include "lldb/Target/RegisterContext.h"
42#include "lldb/Target/Target.h"
43#include "lldb/Target/Thread.h"
44#include "lldb/Target/ThreadPlan.h"
45#include "lldb/Target/ThreadPlanCallFunction.h"
46
47using namespace lldb_private;
48
49//----------------------------------------------------------------------
50// ClangFunctionCaller constructor
51//----------------------------------------------------------------------
52ClangFunctionCaller::ClangFunctionCaller
53(
54 ExecutionContextScope &exe_scope,
55 const CompilerType &return_type,
56 const Address& functionAddress,
57 const ValueList &arg_value_list,
58 const char *name
59) :
60 FunctionCaller(exe_scope, return_type, functionAddress, arg_value_list, name),
61 m_type_system_helper (*this)
62{
63 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
64 // Can't make a ClangFunctionCaller without a process.
65 assert (m_jit_process_wp.lock());
66}
67
68//----------------------------------------------------------------------
69// Destructor
70//----------------------------------------------------------------------
71ClangFunctionCaller::~ClangFunctionCaller()
72{
73}
74
75unsigned
76ClangFunctionCaller::CompileFunction (Stream &errors)
77{
78 if (m_compiled)
79 return 0;
80
81 // FIXME: How does clang tell us there's no return value? We need to handle that case.
82 unsigned num_errors = 0;
83
84 std::string return_type_str (m_function_return_type.GetTypeName().AsCString(""));
85
86 // Cons up the function we're going to wrap our call in, then compile it...
87 // We declare the function "extern "C"" because the compiler might be in C++
88 // mode which would mangle the name and then we couldn't find it again...
89 m_wrapper_function_text.clear();
90 m_wrapper_function_text.append ("extern \"C\" void ");
91 m_wrapper_function_text.append (m_wrapper_function_name);
92 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
93 m_wrapper_function_text.append (m_wrapper_struct_name);
94 m_wrapper_function_text.append (" \n {\n");
95 m_wrapper_function_text.append (" ");
96 m_wrapper_function_text.append (return_type_str);
97 m_wrapper_function_text.append (" (*fn_ptr) (");
98
99 // Get the number of arguments. If we have a function type and it is prototyped,
100 // trust that, otherwise use the values we were given.
101
102 // FIXME: This will need to be extended to handle Variadic functions. We'll need
103 // to pull the defined arguments out of the function, then add the types from the
104 // arguments list for the variable arguments.
105
106 uint32_t num_args = UINT32_MAX;
107 bool trust_function = false;
108 // GetArgumentCount returns -1 for an unprototyped function.
109 CompilerType function_clang_type;
110 if (m_function_ptr)
111 {
112 function_clang_type = m_function_ptr->GetCompilerType();
113 if (function_clang_type)
114 {
115 int num_func_args = function_clang_type.GetFunctionArgumentCount();
116 if (num_func_args >= 0)
117 {
118 trust_function = true;
119 num_args = num_func_args;
120 }
121 }
122 }
123
124 if (num_args == UINT32_MAX)
125 num_args = m_arg_values.GetSize();
126
127 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
128 std::string args_list_buffer; // This one stores the argument list called from the structure.
129 for (size_t i = 0; i < num_args; i++)
130 {
131 std::string type_name;
132
133 if (trust_function)
134 {
135 type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i).GetTypeName().AsCString("");
136 }
137 else
138 {
139 CompilerType clang_qual_type = m_arg_values.GetValueAtIndex(i)->GetCompilerType ();
140 if (clang_qual_type)
141 {
142 type_name = clang_qual_type.GetTypeName().AsCString("");
143 }
144 else
145 {
146 errors.Printf("Could not determine type of input value %" PRIu64 ".", (uint64_t)i);
147 return 1;
148 }
149 }
150
151 m_wrapper_function_text.append (type_name);
152 if (i < num_args - 1)
153 m_wrapper_function_text.append (", ");
154
155 char arg_buf[32];
156 args_buffer.append (" ");
157 args_buffer.append (type_name);
158 snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
159 args_buffer.push_back (' ');
160 args_buffer.append (arg_buf);
161 args_buffer.append (";\n");
162
163 args_list_buffer.append ("__lldb_fn_data->");
164 args_list_buffer.append (arg_buf);
165 if (i < num_args - 1)
166 args_list_buffer.append (", ");
167
168 }
169 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
170
171 m_wrapper_function_text.append (args_buffer);
172
173 m_wrapper_function_text.append (" ");
174 m_wrapper_function_text.append (return_type_str);
175 m_wrapper_function_text.append (" return_value;");
176 m_wrapper_function_text.append ("\n };\n struct ");
177 m_wrapper_function_text.append (m_wrapper_struct_name);
178 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
179 m_wrapper_function_text.append (m_wrapper_struct_name);
180 m_wrapper_function_text.append (" *) input;\n");
181
182 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
183 m_wrapper_function_text.append (args_list_buffer);
184 m_wrapper_function_text.append (");\n}\n");
185
186 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
187 if (log)
188 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
189
190 // Okay, now compile this expression
191
192 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
193 if (jit_process_sp)
194 {
195 const bool generate_debug_info = true;
196 m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this, generate_debug_info));
197
198 num_errors = m_parser->Parse (errors);
199 }
200 else
201 {
202 errors.Printf("no process - unable to inject function");
203 num_errors = 1;
204 }
205
206 m_compiled = (num_errors == 0);
207
208 if (!m_compiled)
209 return num_errors;
210
211 return num_errors;
212}
213
214clang::ASTConsumer *
215ClangFunctionCaller::ClangFunctionCallerHelper::ASTTransformer (clang::ASTConsumer *passthrough)
216{
217 m_struct_extractor.reset(new ASTStructExtractor(passthrough, m_owner.GetWrapperStructName(), m_owner));
218
219 return m_struct_extractor.get();
220}