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"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/Stream.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Expression/FunctionCaller.h"
24 #include "lldb/Expression/UtilityFunction.h"
25 #include "lldb/Expression/ExpressionSourceCode.h"
26 #include "lldb/Expression/IRExecutionUnit.h"
27 #include "lldb/Host/Host.h"
28 #include "lldb/Target/ExecutionContext.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/Target.h"
31 
32 using namespace lldb_private;
33 using namespace lldb;
34 
35 //------------------------------------------------------------------
36 /// Constructor
37 ///
38 /// @param[in] text
39 ///     The text of the function.  Must be a full translation unit.
40 ///
41 /// @param[in] name
42 ///     The name of the function, as used in the text.
43 //------------------------------------------------------------------
44 UtilityFunction::UtilityFunction (ExecutionContextScope &exe_scope,
45                                   const char *text,
46                                   const char *name) :
47     Expression (exe_scope),
48     m_execution_unit_sp (),
49     m_jit_module_wp (),
50     m_function_text (ExpressionSourceCode::g_expression_prefix),
51     m_function_name (name)
52 {
53     if (text && text[0])
54         m_function_text.append (text);
55 }
56 
57 UtilityFunction::~UtilityFunction ()
58 {
59     lldb::ProcessSP process_sp (m_jit_process_wp.lock());
60     if (process_sp)
61     {
62         lldb::ModuleSP jit_module_sp (m_jit_module_wp.lock());
63         if (jit_module_sp)
64             process_sp->GetTarget().GetImages().Remove(jit_module_sp);
65     }
66 
67 }
68 
69 // FIXME: We should check that every time this is called it is called with the same return type & arguments...
70 
71 FunctionCaller *
72 UtilityFunction::MakeFunctionCaller (const CompilerType &return_type, const ValueList &arg_value_list, Error &error)
73 {
74     if (m_caller_up)
75         return m_caller_up.get();
76 
77     ProcessSP process_sp = m_jit_process_wp.lock();
78     if (!process_sp)
79         return nullptr;
80 
81     Address impl_code_address;
82     impl_code_address.SetOffset(StartAddress());
83     std::string name(m_function_name);
84     name.append("-caller");
85 
86     m_caller_up.reset (process_sp->GetTarget().GetFunctionCallerForLanguage (Language(),
87                                                                              return_type,
88                                                                              impl_code_address,
89                                                                              arg_value_list,
90                                                                              name.c_str(),
91                                                                              error));
92     if (error.Fail())
93     {
94 
95         return nullptr;
96     }
97     if (m_caller_up)
98     {
99         StreamString errors;
100         errors.Clear();
101         unsigned num_errors = m_caller_up->CompileFunction(errors);
102         if (num_errors)
103         {
104             error.SetErrorStringWithFormat ("Error compiling %s caller function: \"%s\".",
105                                                 m_function_name.c_str(),
106                                                 errors.GetData());
107             m_caller_up.reset();
108             return nullptr;
109         }
110 
111         errors.Clear();
112         ExecutionContext exe_ctx(process_sp);
113 
114         if (!m_caller_up->WriteFunctionWrapper(exe_ctx, errors))
115         {
116             error.SetErrorStringWithFormat ("Error inserting caller function for %s: \"%s\".",
117                                                 m_function_name.c_str(),
118                                                 errors.GetData());
119             m_caller_up.reset();
120             return nullptr;
121         }
122     }
123     return m_caller_up.get();
124 }
125