1254721Semaste//===-- ClangFunction.cpp ---------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste
11254721Semaste// C Includes
12254721Semaste// C++ Includes
13254721Semaste// Other libraries and framework includes
14254721Semaste#include "clang/AST/ASTContext.h"
15254721Semaste#include "clang/AST/RecordLayout.h"
16254721Semaste#include "clang/CodeGen/CodeGenAction.h"
17254721Semaste#include "clang/CodeGen/ModuleBuilder.h"
18254721Semaste#include "clang/Frontend/CompilerInstance.h"
19254721Semaste#include "llvm/ADT/StringRef.h"
20254721Semaste#include "llvm/ADT/Triple.h"
21254721Semaste#include "llvm/ExecutionEngine/ExecutionEngine.h"
22254721Semaste#include "llvm/IR/Module.h"
23254721Semaste
24254721Semaste// Project includes
25254721Semaste#include "lldb/Expression/ASTStructExtractor.h"
26254721Semaste#include "lldb/Expression/ClangExpressionParser.h"
27254721Semaste#include "lldb/Expression/ClangFunction.h"
28254721Semaste#include "lldb/Expression/IRExecutionUnit.h"
29254721Semaste#include "lldb/Symbol/Type.h"
30254721Semaste#include "lldb/Core/DataExtractor.h"
31254721Semaste#include "lldb/Core/State.h"
32254721Semaste#include "lldb/Core/ValueObject.h"
33254721Semaste#include "lldb/Core/ValueObjectList.h"
34254721Semaste#include "lldb/Interpreter/CommandReturnObject.h"
35254721Semaste#include "lldb/Symbol/ClangASTContext.h"
36254721Semaste#include "lldb/Symbol/Function.h"
37254721Semaste#include "lldb/Target/ExecutionContext.h"
38254721Semaste#include "lldb/Target/Process.h"
39254721Semaste#include "lldb/Target/RegisterContext.h"
40254721Semaste#include "lldb/Target/Target.h"
41254721Semaste#include "lldb/Target/Thread.h"
42254721Semaste#include "lldb/Target/ThreadPlan.h"
43254721Semaste#include "lldb/Target/ThreadPlanCallFunction.h"
44254721Semaste#include "lldb/Core/Log.h"
45254721Semaste
46254721Semasteusing namespace lldb_private;
47254721Semaste
48254721Semaste//----------------------------------------------------------------------
49254721Semaste// ClangFunction constructor
50254721Semaste//----------------------------------------------------------------------
51254721SemasteClangFunction::ClangFunction
52254721Semaste(
53254721Semaste    ExecutionContextScope &exe_scope,
54254721Semaste    const ClangASTType &return_type,
55254721Semaste    const Address& functionAddress,
56254721Semaste    const ValueList &arg_value_list
57254721Semaste) :
58254721Semaste    m_function_ptr (NULL),
59254721Semaste    m_function_addr (functionAddress),
60254721Semaste    m_function_return_type(return_type),
61254721Semaste    m_wrapper_function_name ("__lldb_caller_function"),
62254721Semaste    m_wrapper_struct_name ("__lldb_caller_struct"),
63254721Semaste    m_wrapper_args_addrs (),
64254721Semaste    m_arg_values (arg_value_list),
65254721Semaste    m_compiled (false),
66254721Semaste    m_JITted (false)
67254721Semaste{
68254721Semaste    m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
69254721Semaste    // Can't make a ClangFunction without a process.
70254721Semaste    assert (m_jit_process_wp.lock());
71254721Semaste}
72254721Semaste
73254721SemasteClangFunction::ClangFunction
74254721Semaste(
75254721Semaste    ExecutionContextScope &exe_scope,
76254721Semaste    Function &function,
77254721Semaste    ClangASTContext *ast_context,
78254721Semaste    const ValueList &arg_value_list
79254721Semaste) :
80254721Semaste    m_function_ptr (&function),
81254721Semaste    m_function_addr (),
82254721Semaste    m_function_return_type (),
83254721Semaste    m_wrapper_function_name ("__lldb_function_caller"),
84254721Semaste    m_wrapper_struct_name ("__lldb_caller_struct"),
85254721Semaste    m_wrapper_args_addrs (),
86254721Semaste    m_arg_values (arg_value_list),
87254721Semaste    m_compiled (false),
88254721Semaste    m_JITted (false)
89254721Semaste{
90254721Semaste    m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
91254721Semaste    // Can't make a ClangFunction without a process.
92254721Semaste    assert (m_jit_process_wp.lock());
93254721Semaste
94254721Semaste    m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
95254721Semaste    m_function_return_type = m_function_ptr->GetClangType().GetFunctionReturnType();
96254721Semaste}
97254721Semaste
98254721Semaste//----------------------------------------------------------------------
99254721Semaste// Destructor
100254721Semaste//----------------------------------------------------------------------
101254721SemasteClangFunction::~ClangFunction()
102254721Semaste{
103254721Semaste}
104254721Semaste
105254721Semasteunsigned
106254721SemasteClangFunction::CompileFunction (Stream &errors)
107254721Semaste{
108254721Semaste    if (m_compiled)
109254721Semaste        return 0;
110254721Semaste
111254721Semaste    // FIXME: How does clang tell us there's no return value?  We need to handle that case.
112254721Semaste    unsigned num_errors = 0;
113254721Semaste
114263363Semaste    std::string return_type_str (m_function_return_type.GetTypeName().AsCString(""));
115254721Semaste
116254721Semaste    // Cons up the function we're going to wrap our call in, then compile it...
117254721Semaste    // We declare the function "extern "C"" because the compiler might be in C++
118254721Semaste    // mode which would mangle the name and then we couldn't find it again...
119254721Semaste    m_wrapper_function_text.clear();
120254721Semaste    m_wrapper_function_text.append ("extern \"C\" void ");
121254721Semaste    m_wrapper_function_text.append (m_wrapper_function_name);
122254721Semaste    m_wrapper_function_text.append (" (void *input)\n{\n    struct ");
123254721Semaste    m_wrapper_function_text.append (m_wrapper_struct_name);
124254721Semaste    m_wrapper_function_text.append (" \n  {\n");
125254721Semaste    m_wrapper_function_text.append ("    ");
126254721Semaste    m_wrapper_function_text.append (return_type_str);
127254721Semaste    m_wrapper_function_text.append (" (*fn_ptr) (");
128254721Semaste
129254721Semaste    // Get the number of arguments.  If we have a function type and it is prototyped,
130254721Semaste    // trust that, otherwise use the values we were given.
131254721Semaste
132254721Semaste    // FIXME: This will need to be extended to handle Variadic functions.  We'll need
133254721Semaste    // to pull the defined arguments out of the function, then add the types from the
134254721Semaste    // arguments list for the variable arguments.
135254721Semaste
136254721Semaste    uint32_t num_args = UINT32_MAX;
137254721Semaste    bool trust_function = false;
138254721Semaste    // GetArgumentCount returns -1 for an unprototyped function.
139254721Semaste    ClangASTType function_clang_type;
140254721Semaste    if (m_function_ptr)
141254721Semaste    {
142254721Semaste        function_clang_type = m_function_ptr->GetClangType();
143254721Semaste        if (function_clang_type)
144254721Semaste        {
145254721Semaste            int num_func_args = function_clang_type.GetFunctionArgumentCount();
146254721Semaste            if (num_func_args >= 0)
147254721Semaste            {
148254721Semaste                trust_function = true;
149254721Semaste                num_args = num_func_args;
150254721Semaste            }
151254721Semaste        }
152254721Semaste    }
153254721Semaste
154254721Semaste    if (num_args == UINT32_MAX)
155254721Semaste        num_args = m_arg_values.GetSize();
156254721Semaste
157254721Semaste    std::string args_buffer;  // This one stores the definition of all the args in "struct caller".
158254721Semaste    std::string args_list_buffer;  // This one stores the argument list called from the structure.
159254721Semaste    for (size_t i = 0; i < num_args; i++)
160254721Semaste    {
161254721Semaste        std::string type_name;
162254721Semaste
163254721Semaste        if (trust_function)
164254721Semaste        {
165263363Semaste            type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i).GetTypeName().AsCString("");
166254721Semaste        }
167254721Semaste        else
168254721Semaste        {
169254721Semaste            ClangASTType clang_qual_type = m_arg_values.GetValueAtIndex(i)->GetClangType ();
170254721Semaste            if (clang_qual_type)
171254721Semaste            {
172263363Semaste                type_name = clang_qual_type.GetTypeName().AsCString("");
173254721Semaste            }
174254721Semaste            else
175254721Semaste            {
176263363Semaste                errors.Printf("Could not determine type of input value %zu.", i);
177254721Semaste                return 1;
178254721Semaste            }
179254721Semaste        }
180254721Semaste
181254721Semaste        m_wrapper_function_text.append (type_name);
182254721Semaste        if (i < num_args - 1)
183254721Semaste            m_wrapper_function_text.append (", ");
184254721Semaste
185254721Semaste        char arg_buf[32];
186254721Semaste        args_buffer.append ("    ");
187254721Semaste        args_buffer.append (type_name);
188254721Semaste        snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
189254721Semaste        args_buffer.push_back (' ');
190254721Semaste        args_buffer.append (arg_buf);
191254721Semaste        args_buffer.append (";\n");
192254721Semaste
193254721Semaste        args_list_buffer.append ("__lldb_fn_data->");
194254721Semaste        args_list_buffer.append (arg_buf);
195254721Semaste        if (i < num_args - 1)
196254721Semaste            args_list_buffer.append (", ");
197254721Semaste
198254721Semaste    }
199254721Semaste    m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
200254721Semaste
201254721Semaste    m_wrapper_function_text.append (args_buffer);
202254721Semaste
203254721Semaste    m_wrapper_function_text.append ("    ");
204254721Semaste    m_wrapper_function_text.append (return_type_str);
205254721Semaste    m_wrapper_function_text.append (" return_value;");
206254721Semaste    m_wrapper_function_text.append ("\n  };\n  struct ");
207254721Semaste    m_wrapper_function_text.append (m_wrapper_struct_name);
208254721Semaste    m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
209254721Semaste    m_wrapper_function_text.append (m_wrapper_struct_name);
210254721Semaste    m_wrapper_function_text.append (" *) input;\n");
211254721Semaste
212254721Semaste    m_wrapper_function_text.append ("  __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
213254721Semaste    m_wrapper_function_text.append (args_list_buffer);
214254721Semaste    m_wrapper_function_text.append (");\n}\n");
215254721Semaste
216254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
217254721Semaste    if (log)
218254721Semaste        log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
219254721Semaste
220254721Semaste    // Okay, now compile this expression
221254721Semaste
222254721Semaste    lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
223254721Semaste    if (jit_process_sp)
224254721Semaste    {
225254721Semaste        m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this));
226254721Semaste
227254721Semaste        num_errors = m_parser->Parse (errors);
228254721Semaste    }
229254721Semaste    else
230254721Semaste    {
231254721Semaste        errors.Printf("no process - unable to inject function");
232254721Semaste        num_errors = 1;
233254721Semaste    }
234254721Semaste
235254721Semaste    m_compiled = (num_errors == 0);
236254721Semaste
237254721Semaste    if (!m_compiled)
238254721Semaste        return num_errors;
239254721Semaste
240254721Semaste    return num_errors;
241254721Semaste}
242254721Semaste
243254721Semastebool
244254721SemasteClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
245254721Semaste{
246254721Semaste    Process *process = exe_ctx.GetProcessPtr();
247254721Semaste
248254721Semaste    if (!process)
249254721Semaste        return false;
250254721Semaste
251254721Semaste    lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
252254721Semaste
253254721Semaste    if (process != jit_process_sp.get())
254254721Semaste        return false;
255254721Semaste
256254721Semaste    if (!m_compiled)
257254721Semaste        return false;
258254721Semaste
259254721Semaste    if (m_JITted)
260254721Semaste        return true;
261254721Semaste
262254721Semaste    bool can_interpret = false; // should stay that way
263254721Semaste
264254721Semaste    Error jit_error (m_parser->PrepareForExecution (m_jit_start_addr,
265254721Semaste                                                    m_jit_end_addr,
266254721Semaste                                                    m_execution_unit_ap,
267254721Semaste                                                    exe_ctx,
268254721Semaste                                                    can_interpret,
269254721Semaste                                                    eExecutionPolicyAlways));
270254721Semaste
271254721Semaste    if (!jit_error.Success())
272254721Semaste        return false;
273254721Semaste
274254721Semaste    if (process && m_jit_start_addr)
275254721Semaste        m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
276254721Semaste
277254721Semaste    m_JITted = true;
278254721Semaste
279254721Semaste    return true;
280254721Semaste}
281254721Semaste
282254721Semastebool
283254721SemasteClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
284254721Semaste{
285254721Semaste    return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
286254721Semaste}
287254721Semaste
288254721Semaste// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
289254721Semaste
290254721Semastebool
291254721SemasteClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
292254721Semaste                                       lldb::addr_t &args_addr_ref,
293254721Semaste                                       Address function_address,
294254721Semaste                                       ValueList &arg_values,
295254721Semaste                                       Stream &errors)
296254721Semaste{
297254721Semaste    // All the information to reconstruct the struct is provided by the
298254721Semaste    // StructExtractor.
299254721Semaste    if (!m_struct_valid)
300254721Semaste    {
301254721Semaste        errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
302254721Semaste        return false;
303254721Semaste    }
304254721Semaste
305254721Semaste    Error error;
306254721Semaste    using namespace clang;
307254721Semaste    ExecutionResults return_value = eExecutionSetupError;
308254721Semaste
309254721Semaste    Process *process = exe_ctx.GetProcessPtr();
310254721Semaste
311254721Semaste    if (process == NULL)
312254721Semaste        return return_value;
313254721Semaste
314254721Semaste    lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
315254721Semaste
316254721Semaste    if (process != jit_process_sp.get())
317254721Semaste        return false;
318254721Semaste
319254721Semaste    if (args_addr_ref == LLDB_INVALID_ADDRESS)
320254721Semaste    {
321254721Semaste        args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
322254721Semaste        if (args_addr_ref == LLDB_INVALID_ADDRESS)
323254721Semaste            return false;
324254721Semaste        m_wrapper_args_addrs.push_back (args_addr_ref);
325254721Semaste    }
326254721Semaste    else
327254721Semaste    {
328254721Semaste        // Make sure this is an address that we've already handed out.
329254721Semaste        if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
330254721Semaste        {
331254721Semaste            return false;
332254721Semaste        }
333254721Semaste    }
334254721Semaste
335254721Semaste    // TODO: verify fun_addr needs to be a callable address
336254721Semaste    Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
337254721Semaste    uint64_t first_offset = m_member_offsets[0];
338254721Semaste    process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
339254721Semaste
340254721Semaste    // FIXME: We will need to extend this for Variadic functions.
341254721Semaste
342254721Semaste    Error value_error;
343254721Semaste
344254721Semaste    size_t num_args = arg_values.GetSize();
345254721Semaste    if (num_args != m_arg_values.GetSize())
346254721Semaste    {
347263363Semaste        errors.Printf ("Wrong number of arguments - was: %zu should be: %zu", num_args, m_arg_values.GetSize());
348254721Semaste        return false;
349254721Semaste    }
350254721Semaste
351254721Semaste    for (size_t i = 0; i < num_args; i++)
352254721Semaste    {
353254721Semaste        // FIXME: We should sanity check sizes.
354254721Semaste
355254721Semaste        uint64_t offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
356254721Semaste        Value *arg_value = arg_values.GetValueAtIndex(i);
357254721Semaste
358254721Semaste        // FIXME: For now just do scalars:
359254721Semaste
360254721Semaste        // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
361254721Semaste
362254721Semaste        if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
363254721Semaste            arg_value->GetContextType() == Value::eContextTypeInvalid &&
364254721Semaste            arg_value->GetClangType().IsPointerType())
365254721Semaste            continue;
366254721Semaste
367254721Semaste        const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx);
368254721Semaste
369254721Semaste        if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
370254721Semaste            return false;
371254721Semaste    }
372254721Semaste
373254721Semaste    return true;
374254721Semaste}
375254721Semaste
376254721Semastebool
377254721SemasteClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
378254721Semaste{
379254721Semaste    using namespace clang;
380254721Semaste
381254721Semaste    if (CompileFunction(errors) != 0)
382254721Semaste        return false;
383254721Semaste    if (!WriteFunctionWrapper(exe_ctx, errors))
384254721Semaste        return false;
385254721Semaste    if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
386254721Semaste        return false;
387254721Semaste
388254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
389254721Semaste    if (log)
390254721Semaste        log->Printf ("Call Address: 0x%" PRIx64 " Struct Address: 0x%" PRIx64 ".\n", m_jit_start_addr, args_addr_ref);
391254721Semaste
392254721Semaste    return true;
393254721Semaste}
394254721Semaste
395254721SemasteThreadPlan *
396254721SemasteClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
397263367Semaste                                            lldb::addr_t args_addr,
398263367Semaste                                            const EvaluateExpressionOptions &options,
399263367Semaste                                            Stream &errors)
400254721Semaste{
401254721Semaste    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
402254721Semaste
403254721Semaste    if (log)
404254721Semaste        log->Printf("-- [ClangFunction::GetThreadPlanToCallFunction] Creating thread plan to call function --");
405254721Semaste
406254721Semaste    // FIXME: Use the errors Stream for better error reporting.
407254721Semaste    Thread *thread = exe_ctx.GetThreadPtr();
408254721Semaste    if (thread == NULL)
409254721Semaste    {
410254721Semaste        errors.Printf("Can't call a function without a valid thread.");
411254721Semaste        return NULL;
412254721Semaste    }
413254721Semaste
414254721Semaste    // Okay, now run the function:
415254721Semaste
416263367Semaste    Address wrapper_address (m_jit_start_addr);
417263367Semaste
418263367Semaste    lldb::addr_t args = { args_addr };
419263367Semaste
420254721Semaste    ThreadPlan *new_plan = new ThreadPlanCallFunction (*thread,
421254721Semaste                                                       wrapper_address,
422254721Semaste                                                       ClangASTType(),
423263367Semaste                                                       args,
424263367Semaste                                                       options);
425254721Semaste    new_plan->SetIsMasterPlan(true);
426254721Semaste    new_plan->SetOkayToDiscard (false);
427254721Semaste    return new_plan;
428254721Semaste}
429254721Semaste
430254721Semastebool
431254721SemasteClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
432254721Semaste{
433254721Semaste    // Read the return value - it is the last field in the struct:
434254721Semaste    // FIXME: How does clang tell us there's no return value?  We need to handle that case.
435254721Semaste    // FIXME: Create our ThreadPlanCallFunction with the return ClangASTType, and then use GetReturnValueObject
436254721Semaste    // to fetch the value.  That way we can fetch any values we need.
437254721Semaste
438254721Semaste    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
439254721Semaste
440254721Semaste    if (log)
441254721Semaste        log->Printf("-- [ClangFunction::FetchFunctionResults] Fetching function results --");
442254721Semaste
443254721Semaste    Process *process = exe_ctx.GetProcessPtr();
444254721Semaste
445254721Semaste    if (process == NULL)
446254721Semaste        return false;
447254721Semaste
448254721Semaste    lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
449254721Semaste
450254721Semaste    if (process != jit_process_sp.get())
451254721Semaste        return false;
452254721Semaste
453254721Semaste    Error error;
454254721Semaste    ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
455254721Semaste
456254721Semaste    if (error.Fail())
457254721Semaste        return false;
458254721Semaste
459254721Semaste    ret_value.SetClangType(m_function_return_type);
460254721Semaste    ret_value.SetValueType(Value::eValueTypeScalar);
461254721Semaste    return true;
462254721Semaste}
463254721Semaste
464254721Semastevoid
465254721SemasteClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
466254721Semaste{
467254721Semaste    std::list<lldb::addr_t>::iterator pos;
468254721Semaste    pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
469254721Semaste    if (pos != m_wrapper_args_addrs.end())
470254721Semaste        m_wrapper_args_addrs.erase(pos);
471254721Semaste
472254721Semaste    exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
473254721Semaste}
474254721Semaste
475254721SemasteExecutionResults
476254721SemasteClangFunction::ExecuteFunction(
477254721Semaste        ExecutionContext &exe_ctx,
478263367Semaste        lldb::addr_t *args_addr_ptr,
479263367Semaste        const EvaluateExpressionOptions &options,
480254721Semaste        Stream &errors,
481254721Semaste        Value &results)
482254721Semaste{
483263367Semaste    using namespace clang;
484263367Semaste    ExecutionResults return_value = eExecutionSetupError;
485263367Semaste
486263367Semaste    // ClangFunction::ExecuteFunction execution is always just to get the result.  Do make sure we ignore
487263367Semaste    // breakpoints, unwind on error, and don't try to debug it.
488263367Semaste    EvaluateExpressionOptions real_options = options;
489263367Semaste    real_options.SetDebug(false);
490263367Semaste    real_options.SetUnwindOnError(true);
491263367Semaste    real_options.SetIgnoreBreakpoints(true);
492263367Semaste
493263367Semaste    lldb::addr_t args_addr;
494263367Semaste
495263367Semaste    if (args_addr_ptr != NULL)
496263367Semaste        args_addr = *args_addr_ptr;
497263367Semaste    else
498263367Semaste        args_addr = LLDB_INVALID_ADDRESS;
499263367Semaste
500263367Semaste    if (CompileFunction(errors) != 0)
501263367Semaste        return eExecutionSetupError;
502263367Semaste
503263367Semaste    if (args_addr == LLDB_INVALID_ADDRESS)
504263367Semaste    {
505263367Semaste        if (!InsertFunction(exe_ctx, args_addr, errors))
506263367Semaste            return eExecutionSetupError;
507263367Semaste    }
508254721Semaste
509254721Semaste    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
510254721Semaste
511254721Semaste    if (log)
512254721Semaste        log->Printf("== [ClangFunction::ExecuteFunction] Executing function ==");
513254721Semaste
514263367Semaste    lldb::ThreadPlanSP call_plan_sp (GetThreadPlanToCallFunction (exe_ctx,
515263367Semaste                                                                  args_addr,
516263367Semaste                                                                  real_options,
517263367Semaste                                                                  errors));
518254721Semaste    if (!call_plan_sp)
519254721Semaste        return eExecutionSetupError;
520254721Semaste
521254721Semaste    // <rdar://problem/12027563> we need to make sure we record the fact that we are running an expression here
522254721Semaste    // otherwise this fact will fail to be recorded when fetching an Objective-C object description
523254721Semaste    if (exe_ctx.GetProcessPtr())
524254721Semaste        exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
525254721Semaste
526263367Semaste    return_value = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
527263367Semaste                                                          call_plan_sp,
528263367Semaste                                                          real_options,
529263367Semaste                                                          errors);
530254721Semaste
531254721Semaste    if (log)
532254721Semaste    {
533263367Semaste        if (return_value != eExecutionCompleted)
534254721Semaste        {
535254721Semaste            log->Printf("== [ClangFunction::ExecuteFunction] Execution completed abnormally ==");
536254721Semaste        }
537254721Semaste        else
538254721Semaste        {
539254721Semaste            log->Printf("== [ClangFunction::ExecuteFunction] Execution completed normally ==");
540254721Semaste        }
541254721Semaste    }
542254721Semaste
543254721Semaste    if (exe_ctx.GetProcessPtr())
544254721Semaste        exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
545254721Semaste
546254721Semaste    if (args_addr_ptr != NULL)
547254721Semaste        *args_addr_ptr = args_addr;
548254721Semaste
549254721Semaste    if (return_value != eExecutionCompleted)
550254721Semaste        return return_value;
551254721Semaste
552254721Semaste    FetchFunctionResults(exe_ctx, args_addr, results);
553254721Semaste
554254721Semaste    if (args_addr_ptr == NULL)
555254721Semaste        DeallocateFunctionResults(exe_ctx, args_addr);
556254721Semaste
557254721Semaste    return eExecutionCompleted;
558254721Semaste}
559254721Semaste
560254721Semasteclang::ASTConsumer *
561254721SemasteClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
562254721Semaste{
563263363Semaste    m_struct_extractor.reset(new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this));
564263363Semaste
565263363Semaste    return m_struct_extractor.get();
566254721Semaste}
567