blob: 2df78a4957fe032f40a7868509292494494c21c6 [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
10#include "CommandObjectArgs.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Value.h"
18#include "lldb/Expression/ClangExpression.h"
19#include "lldb/Expression/ClangExpressionVariable.h"
20#include "lldb/Expression/ClangFunction.h"
21#include "lldb/Host/Host.h"
22#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton63094e02010-06-23 01:19:29 +000023#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Interpreter/CommandReturnObject.h"
25#include "lldb/Symbol/ObjectFile.h"
26#include "lldb/Symbol/Variable.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.h"
30#include "lldb/Target/StackFrame.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
35// This command is a toy. I'm just using it to have a way to construct the arguments to
36// calling functions.
37//
38
39CommandObjectArgs::CommandOptions::CommandOptions () :
40Options()
41{
42 // Keep only one place to reset the values to their defaults
43 ResetOptionValues();
44}
45
46
47CommandObjectArgs::CommandOptions::~CommandOptions ()
48{
49}
50
51Error
52CommandObjectArgs::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
53{
54 Error error;
55
56 char short_option = (char) m_getopt_table[option_idx].val;
57
58 switch (short_option)
59 {
60 default:
61 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
62 break;
63 }
64
65 return error;
66}
67
68void
69CommandObjectArgs::CommandOptions::ResetOptionValues ()
70{
71 Options::ResetOptionValues();
72}
73
74const lldb::OptionDefinition*
75CommandObjectArgs::CommandOptions::GetDefinitions ()
76{
77 return g_option_table;
78}
79
Greg Clayton238c0a12010-09-18 01:14:36 +000080CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
81 CommandObject (interpreter,
82 "args",
Chris Lattner24943d22010-06-08 16:52:24 +000083 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
84 "args")
85{
86}
87
88CommandObjectArgs::~CommandObjectArgs ()
89{
90}
91
92Options *
93CommandObjectArgs::GetOptions ()
94{
95 return &m_options;
96}
97
98bool
Greg Clayton63094e02010-06-23 01:19:29 +000099CommandObjectArgs::Execute
100(
Greg Clayton63094e02010-06-23 01:19:29 +0000101 Args& args,
102 CommandReturnObject &result
103)
Chris Lattner24943d22010-06-08 16:52:24 +0000104{
105 ConstString target_triple;
106
Greg Clayton63094e02010-06-23 01:19:29 +0000107
Greg Clayton238c0a12010-09-18 01:14:36 +0000108 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000109 if (!process)
110 {
111 result.AppendError ("Args found no process.");
112 result.SetStatus (eReturnStatusFailed);
113 return false;
114 }
115
116 const ABI *abi = process->GetABI ();
117 if (!abi)
118 {
119 result.AppendError ("The current process has no ABI.");
120 result.SetStatus (eReturnStatusFailed);
121 return false;
122 }
123
Greg Clayton63094e02010-06-23 01:19:29 +0000124 int num_args = args.GetArgumentCount ();
Chris Lattner24943d22010-06-08 16:52:24 +0000125 int arg_index;
126
127 if (!num_args)
128 {
129 result.AppendError ("args requires at least one argument");
130 result.SetStatus (eReturnStatusFailed);
131 return false;
132 }
133
Greg Clayton238c0a12010-09-18 01:14:36 +0000134 Thread *thread = m_interpreter.GetDebugger().GetExecutionContext ().thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000135
136 if (!thread)
137 {
138 result.AppendError ("args found no thread.");
139 result.SetStatus (eReturnStatusFailed);
140 return false;
141 }
142
Jim Inghamc8332952010-08-26 21:32:51 +0000143 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
Chris Lattner24943d22010-06-08 16:52:24 +0000144 if (!thread_cur_frame)
145 {
146 result.AppendError ("The current thread has no current frame.");
147 result.SetStatus (eReturnStatusFailed);
148 return false;
149 }
150
Greg Claytonb04e7a82010-08-24 21:05:24 +0000151 Module *thread_module = thread_cur_frame->GetFrameCodeAddress ().GetModule ();
Chris Lattner24943d22010-06-08 16:52:24 +0000152 if (!thread_module)
153 {
154 result.AppendError ("The PC has no associated module.");
155 result.SetStatus (eReturnStatusFailed);
156 return false;
157 }
158
159 TypeList *thread_type_list = thread_module->GetTypeList ();
160 if (!thread_type_list)
161 {
162 result.AppendError ("The module has no type list.");
163 result.SetStatus (eReturnStatusFailed);
164 return false;
165 }
166
167 ClangASTContext &ast_context = thread_type_list->GetClangASTContext();
168
169 ValueList value_list;
170
171 for (arg_index = 0; arg_index < num_args; ++arg_index)
172 {
Greg Clayton63094e02010-06-23 01:19:29 +0000173 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
Chris Lattner24943d22010-06-08 16:52:24 +0000174 Value value;
175 value.SetValueType(Value::eValueTypeScalar);
176 void *type;
177
178 char *int_pos;
Eli Friedmanb4a47282010-06-09 07:57:51 +0000179 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
Chris Lattner24943d22010-06-08 16:52:24 +0000180 {
181 Encoding encoding = eEncodingSint;
182
183 int width = 0;
184
185 if (int_pos > arg_type_cstr + 1)
186 {
187 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
188 result.SetStatus (eReturnStatusFailed);
189 return false;
190 }
191 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u')
192 {
193 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
194 result.SetStatus (eReturnStatusFailed);
195 return false;
196 }
197 if (arg_type_cstr[0] == 'u')
198 {
199 encoding = eEncodingUint;
200 }
201
202 char *width_pos = int_pos + 3;
203
204 if (!strcmp (width_pos, "8_t"))
205 width = 8;
206 else if (!strcmp (width_pos, "16_t"))
207 width = 16;
208 else if (!strcmp (width_pos, "32_t"))
209 width = 32;
210 else if (!strcmp (width_pos, "64_t"))
211 width = 64;
212 else
213 {
214 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
215 result.SetStatus (eReturnStatusFailed);
216 return false;
217 }
218
219 type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
220
221 if (!type)
222 {
223 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
224 arg_type_cstr,
225 (encoding == eEncodingSint ? "signed" : "unsigned"),
226 width);
227
228 result.SetStatus (eReturnStatusFailed);
229 return false;
230 }
231 }
232 else if (strchr (arg_type_cstr, '*'))
233 {
234 if (!strcmp (arg_type_cstr, "void*"))
Greg Clayton960d6a42010-08-03 00:35:52 +0000235 type = ast_context.CreatePointerType (ast_context.GetBuiltInType_void ());
Chris Lattner24943d22010-06-08 16:52:24 +0000236 else if (!strcmp (arg_type_cstr, "char*"))
237 type = ast_context.GetCStringType (false);
238 else
239 {
240 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
241 result.SetStatus (eReturnStatusFailed);
242 return false;
243 }
244 }
245 else
246 {
247 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
248 result.SetStatus (eReturnStatusFailed);
249 return false;
250 }
251
252 value.SetContext (Value::eContextTypeOpaqueClangQualType, type);
253
254 value_list.PushValue(value);
255 }
256
257 if (!abi->GetArgumentValues (*thread, value_list))
258 {
259 result.AppendError ("Couldn't get argument values");
260 result.SetStatus (eReturnStatusFailed);
261 return false;
262 }
263
264 result.GetOutputStream ().Printf("Arguments : \n");
265
266 for (arg_index = 0; arg_index < num_args; ++arg_index)
267 {
Greg Clayton63094e02010-06-23 01:19:29 +0000268 result.GetOutputStream ().Printf ("%d (%s): ", arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +0000269 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
270 result.GetOutputStream ().Printf("\n");
271 }
272
273 return result.Succeeded();
274}
275
276lldb::OptionDefinition
277CommandObjectArgs::CommandOptions::g_option_table[] =
278{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000279 { LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
280 { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000281};
282