blob: 4450aeb915c3bf01785d5c06c5b555460e6c9107 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectArgs.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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "CommandObjectArgs.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000018#include "lldb/Interpreter/Args.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000019#include "lldb/Core/Debugger.h"
20#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Value.h"
22#include "lldb/Expression/ClangExpression.h"
23#include "lldb/Expression/ClangExpressionVariable.h"
24#include "lldb/Expression/ClangFunction.h"
25#include "lldb/Host/Host.h"
26#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Interpreter/CommandReturnObject.h"
28#include "lldb/Symbol/ObjectFile.h"
29#include "lldb/Symbol/Variable.h"
30#include "lldb/Target/Process.h"
31#include "lldb/Target/Target.h"
32#include "lldb/Target/Thread.h"
33#include "lldb/Target/StackFrame.h"
34
35using namespace lldb;
36using namespace lldb_private;
37
38// This command is a toy. I'm just using it to have a way to construct the arguments to
39// calling functions.
40//
41
Greg Claytonf15996e2011-04-07 22:46:35 +000042CommandObjectArgs::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chen93356432011-04-08 22:39:17 +000043 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000044{
45 // Keep only one place to reset the values to their defaults
Greg Clayton143fcc32011-04-13 00:18:08 +000046 OptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +000047}
48
49
50CommandObjectArgs::CommandOptions::~CommandOptions ()
51{
52}
53
54Error
Greg Clayton143fcc32011-04-13 00:18:08 +000055CommandObjectArgs::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000056{
57 Error error;
58
Greg Clayton6475c422012-12-04 00:32:51 +000059 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner24943d22010-06-08 16:52:24 +000060
61 switch (short_option)
62 {
63 default:
Greg Clayton9c236732011-10-26 00:56:27 +000064 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +000065 break;
66 }
67
68 return error;
69}
70
71void
Greg Clayton143fcc32011-04-13 00:18:08 +000072CommandObjectArgs::CommandOptions::OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +000073{
Chris Lattner24943d22010-06-08 16:52:24 +000074}
75
Greg Claytonb3448432011-03-24 21:19:54 +000076const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +000077CommandObjectArgs::CommandOptions::GetDefinitions ()
78{
79 return g_option_table;
80}
81
Greg Clayton238c0a12010-09-18 01:14:36 +000082CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000083 CommandObjectParsed (interpreter,
84 "args",
85 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
86 "args"),
Greg Claytonf15996e2011-04-07 22:46:35 +000087 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000088{
89}
90
91CommandObjectArgs::~CommandObjectArgs ()
92{
93}
94
95Options *
96CommandObjectArgs::GetOptions ()
97{
98 return &m_options;
99}
100
101bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000102CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000103{
104 ConstString target_triple;
105
Greg Clayton63094e02010-06-23 01:19:29 +0000106
Greg Clayton567e7f32011-09-22 04:58:26 +0000107 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000108 if (!process)
109 {
110 result.AppendError ("Args found no process.");
111 result.SetStatus (eReturnStatusFailed);
112 return false;
113 }
114
Greg Clayton75906e42011-05-11 18:39:18 +0000115 const ABI *abi = process->GetABI().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000116 if (!abi)
117 {
118 result.AppendError ("The current process has no ABI.");
119 result.SetStatus (eReturnStatusFailed);
120 return false;
121 }
122
Greg Clayton63094e02010-06-23 01:19:29 +0000123 int num_args = args.GetArgumentCount ();
Chris Lattner24943d22010-06-08 16:52:24 +0000124 int arg_index;
125
126 if (!num_args)
127 {
128 result.AppendError ("args requires at least one argument");
129 result.SetStatus (eReturnStatusFailed);
130 return false;
131 }
132
Greg Clayton567e7f32011-09-22 04:58:26 +0000133 Thread *thread = m_interpreter.GetExecutionContext ().GetThreadPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000134
135 if (!thread)
136 {
137 result.AppendError ("args found no thread.");
138 result.SetStatus (eReturnStatusFailed);
139 return false;
140 }
141
Jim Inghamc8332952010-08-26 21:32:51 +0000142 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
Chris Lattner24943d22010-06-08 16:52:24 +0000143 if (!thread_cur_frame)
144 {
145 result.AppendError ("The current thread has no current frame.");
146 result.SetStatus (eReturnStatusFailed);
147 return false;
148 }
149
Greg Clayton3508c382012-02-24 01:59:29 +0000150 ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule());
151 if (!thread_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000152 {
153 result.AppendError ("The PC has no associated module.");
154 result.SetStatus (eReturnStatusFailed);
155 return false;
156 }
157
Greg Clayton3508c382012-02-24 01:59:29 +0000158 ClangASTContext &ast_context = thread_module_sp->GetClangASTContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000159
160 ValueList value_list;
161
162 for (arg_index = 0; arg_index < num_args; ++arg_index)
163 {
Greg Clayton63094e02010-06-23 01:19:29 +0000164 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
Chris Lattner24943d22010-06-08 16:52:24 +0000165 Value value;
166 value.SetValueType(Value::eValueTypeScalar);
167 void *type;
168
169 char *int_pos;
Eli Friedmanb4a47282010-06-09 07:57:51 +0000170 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
Chris Lattner24943d22010-06-08 16:52:24 +0000171 {
172 Encoding encoding = eEncodingSint;
173
174 int width = 0;
175
176 if (int_pos > arg_type_cstr + 1)
177 {
178 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
179 result.SetStatus (eReturnStatusFailed);
180 return false;
181 }
182 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u')
183 {
184 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
185 result.SetStatus (eReturnStatusFailed);
186 return false;
187 }
188 if (arg_type_cstr[0] == 'u')
189 {
190 encoding = eEncodingUint;
191 }
192
193 char *width_pos = int_pos + 3;
194
195 if (!strcmp (width_pos, "8_t"))
196 width = 8;
197 else if (!strcmp (width_pos, "16_t"))
198 width = 16;
199 else if (!strcmp (width_pos, "32_t"))
200 width = 32;
201 else if (!strcmp (width_pos, "64_t"))
202 width = 64;
203 else
204 {
205 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
206 result.SetStatus (eReturnStatusFailed);
207 return false;
208 }
209
210 type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
211
212 if (!type)
213 {
214 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
215 arg_type_cstr,
216 (encoding == eEncodingSint ? "signed" : "unsigned"),
217 width);
218
219 result.SetStatus (eReturnStatusFailed);
220 return false;
221 }
222 }
223 else if (strchr (arg_type_cstr, '*'))
224 {
225 if (!strcmp (arg_type_cstr, "void*"))
Greg Clayton960d6a42010-08-03 00:35:52 +0000226 type = ast_context.CreatePointerType (ast_context.GetBuiltInType_void ());
Chris Lattner24943d22010-06-08 16:52:24 +0000227 else if (!strcmp (arg_type_cstr, "char*"))
228 type = ast_context.GetCStringType (false);
229 else
230 {
231 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
232 result.SetStatus (eReturnStatusFailed);
233 return false;
234 }
235 }
236 else
237 {
238 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
239 result.SetStatus (eReturnStatusFailed);
240 return false;
241 }
242
Greg Clayton6916e352010-11-13 03:52:47 +0000243 value.SetContext (Value::eContextTypeClangType, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000244
245 value_list.PushValue(value);
246 }
247
248 if (!abi->GetArgumentValues (*thread, value_list))
249 {
250 result.AppendError ("Couldn't get argument values");
251 result.SetStatus (eReturnStatusFailed);
252 return false;
253 }
254
255 result.GetOutputStream ().Printf("Arguments : \n");
256
257 for (arg_index = 0; arg_index < num_args; ++arg_index)
258 {
Greg Clayton63094e02010-06-23 01:19:29 +0000259 result.GetOutputStream ().Printf ("%d (%s): ", arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +0000260 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
261 result.GetOutputStream ().Printf("\n");
262 }
263
264 return result.Succeeded();
265}
266
Greg Claytonb3448432011-03-24 21:19:54 +0000267OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000268CommandObjectArgs::CommandOptions::g_option_table[] =
269{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000270 { LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +0000271 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000272};
273