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