]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/source/Expression/ClangUtilityFunction.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / lldb / source / Expression / ClangUtilityFunction.cpp
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/Stream.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Expression/ClangExpressionDeclMap.h"
23 #include "lldb/Expression/ClangExpressionParser.h"
24 #include "lldb/Expression/ClangUtilityFunction.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
33 //------------------------------------------------------------------
34 /// Constructor
35 ///
36 /// @param[in] text
37 ///     The text of the function.  Must be a full translation unit.
38 ///
39 /// @param[in] name
40 ///     The name of the function, as used in the text.
41 //------------------------------------------------------------------
42 ClangUtilityFunction::ClangUtilityFunction (const char *text, 
43                                             const char *name) :
44     ClangExpression (),
45     m_function_text (ExpressionSourceCode::g_expression_prefix),
46     m_function_name (name)
47 {
48     if (text && text[0])
49         m_function_text.append (text);
50 }
51
52 ClangUtilityFunction::~ClangUtilityFunction ()
53 {
54 }
55
56 //------------------------------------------------------------------
57 /// Install the utility function into a process
58 ///
59 /// @param[in] error_stream
60 ///     A stream to print parse errors and warnings to.
61 ///
62 /// @param[in] exe_ctx
63 ///     The execution context to install the utility function to.
64 ///
65 /// @return
66 ///     True on success (no errors); false otherwise.
67 //------------------------------------------------------------------
68 bool
69 ClangUtilityFunction::Install (Stream &error_stream,
70                                ExecutionContext &exe_ctx)
71 {
72     if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
73     {
74         error_stream.PutCString("error: already installed\n");
75         return false;
76     }
77     
78     ////////////////////////////////////
79     // Set up the target and compiler
80     //
81     
82     Target *target = exe_ctx.GetTargetPtr();
83     
84     if (!target)
85     {
86         error_stream.PutCString ("error: invalid target\n");
87         return false;
88     }
89     
90     Process *process = exe_ctx.GetProcessPtr();
91     
92     if (!process)
93     {
94         error_stream.PutCString ("error: invalid process\n");
95         return false;
96     }
97         
98     //////////////////////////
99     // Parse the expression
100     //
101     
102     bool keep_result_in_memory = false;
103     
104     m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
105     
106     if (!m_expr_decl_map->WillParse(exe_ctx, NULL))
107     {
108         error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
109         return false;
110     }
111         
112     ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this);
113     
114     unsigned num_errors = parser.Parse (error_stream);
115     
116     if (num_errors)
117     {
118         error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
119         
120         m_expr_decl_map.reset();
121         
122         return false;
123     }
124     
125     //////////////////////////////////
126     // JIT the output of the parser
127     //
128         
129     bool can_interpret = false; // should stay that way
130     
131     Error jit_error = parser.PrepareForExecution (m_jit_start_addr, 
132                                                   m_jit_end_addr,
133                                                   m_execution_unit_ap,
134                                                   exe_ctx,
135                                                   can_interpret,
136                                                   eExecutionPolicyAlways);
137     
138     if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
139         m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
140     
141 #if 0
142         // jingham: look here
143     StreamFile logfile ("/tmp/exprs.txt", "a");
144     logfile.Printf ("0x%16.16" PRIx64 ": func = %s, source =\n%s\n",
145                     m_jit_start_addr, 
146                     m_function_name.c_str(), 
147                     m_function_text.c_str());
148 #endif
149
150     m_expr_decl_map->DidParse();
151     
152     m_expr_decl_map.reset();
153     
154     if (jit_error.Success())
155     {
156         return true;
157     }
158     else
159     {
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");
165         return false;
166     }
167 }
168
169