blob: da5496123b19704a417fb1189cff997af05881b0 [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"
Greg Clayton49ce8962012-08-29 21:13:06 +000017#include "lldb/Core/Debugger.h"
18#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Value.h"
20#include "lldb/Expression/ClangExpression.h"
21#include "lldb/Expression/ClangExpressionVariable.h"
22#include "lldb/Expression/ClangFunction.h"
23#include "lldb/Host/Host.h"
24#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Interpreter/CommandReturnObject.h"
26#include "lldb/Symbol/ObjectFile.h"
27#include "lldb/Symbol/Variable.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
31#include "lldb/Target/StackFrame.h"
32
33using namespace lldb;
34using namespace lldb_private;
35
36// This command is a toy. I'm just using it to have a way to construct the arguments to
37// calling functions.
38//
39
Greg Claytonf15996e2011-04-07 22:46:35 +000040CommandObjectArgs::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chen93356432011-04-08 22:39:17 +000041 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43 // Keep only one place to reset the values to their defaults
Greg Clayton143fcc32011-04-13 00:18:08 +000044 OptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +000045}
46
47
48CommandObjectArgs::CommandOptions::~CommandOptions ()
49{
50}
51
52Error
Greg Clayton143fcc32011-04-13 00:18:08 +000053CommandObjectArgs::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000054{
55 Error error;
56
57 char short_option = (char) m_getopt_table[option_idx].val;
58
59 switch (short_option)
60 {
61 default:
Greg Clayton9c236732011-10-26 00:56:27 +000062 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +000063 break;
64 }
65
66 return error;
67}
68
69void
Greg Clayton143fcc32011-04-13 00:18:08 +000070CommandObjectArgs::CommandOptions::OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +000071{
Chris Lattner24943d22010-06-08 16:52:24 +000072}
73
Greg Claytonb3448432011-03-24 21:19:54 +000074const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +000075CommandObjectArgs::CommandOptions::GetDefinitions ()
76{
77 return g_option_table;
78}
79
Greg Clayton238c0a12010-09-18 01:14:36 +000080CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000081 CommandObjectParsed (interpreter,
82 "args",
83 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
84 "args"),
Greg Claytonf15996e2011-04-07 22:46:35 +000085 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000086{
87}
88
89CommandObjectArgs::~CommandObjectArgs ()
90{
91}
92
93Options *
94CommandObjectArgs::GetOptions ()
95{
96 return &m_options;
97}
98
99bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000100CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000101{
102 ConstString target_triple;
103
Greg Clayton63094e02010-06-23 01:19:29 +0000104
Greg Clayton567e7f32011-09-22 04:58:26 +0000105 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000106 if (!process)
107 {
108 result.AppendError ("Args found no process.");
109 result.SetStatus (eReturnStatusFailed);
110 return false;
111 }
112
Greg Clayton75906e42011-05-11 18:39:18 +0000113 const ABI *abi = process->GetABI().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000114 if (!abi)
115 {
116 result.AppendError ("The current process has no ABI.");
117 result.SetStatus (eReturnStatusFailed);
118 return false;
119 }
120
Greg Clayton63094e02010-06-23 01:19:29 +0000121 int num_args = args.GetArgumentCount ();
Chris Lattner24943d22010-06-08 16:52:24 +0000122 int arg_index;
123
124 if (!num_args)
125 {
126 result.AppendError ("args requires at least one argument");
127 result.SetStatus (eReturnStatusFailed);
128 return false;
129 }
130
Greg Clayton567e7f32011-09-22 04:58:26 +0000131 Thread *thread = m_interpreter.GetExecutionContext ().GetThreadPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000132
133 if (!thread)
134 {
135 result.AppendError ("args found no thread.");
136 result.SetStatus (eReturnStatusFailed);
137 return false;
138 }
139
Jim Inghamc8332952010-08-26 21:32:51 +0000140 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
Chris Lattner24943d22010-06-08 16:52:24 +0000141 if (!thread_cur_frame)
142 {
143 result.AppendError ("The current thread has no current frame.");
144 result.SetStatus (eReturnStatusFailed);
145 return false;
146 }
147
Greg Clayton3508c382012-02-24 01:59:29 +0000148 ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule());
149 if (!thread_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000150 {
151 result.AppendError ("The PC has no associated module.");
152 result.SetStatus (eReturnStatusFailed);
153 return false;
154 }
155
Greg Clayton3508c382012-02-24 01:59:29 +0000156 ClangASTContext &ast_context = thread_module_sp->GetClangASTContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000157
158 ValueList value_list;
159
160 for (arg_index = 0; arg_index < num_args; ++arg_index)
161 {
Greg Clayton63094e02010-06-23 01:19:29 +0000162 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
Chris Lattner24943d22010-06-08 16:52:24 +0000163 Value value;
164 value.SetValueType(Value::eValueTypeScalar);
165 void *type;
166
167 char *int_pos;
Eli Friedmanb4a47282010-06-09 07:57:51 +0000168 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
Chris Lattner24943d22010-06-08 16:52:24 +0000169 {
170 Encoding encoding = eEncodingSint;
171
172 int width = 0;
173
174 if (int_pos > arg_type_cstr + 1)
175 {
176 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
177 result.SetStatus (eReturnStatusFailed);
178 return false;
179 }
180 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u')
181 {
182 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
183 result.SetStatus (eReturnStatusFailed);
184 return false;
185 }
186 if (arg_type_cstr[0] == 'u')
187 {
188 encoding = eEncodingUint;
189 }
190
191 char *width_pos = int_pos + 3;
192
193 if (!strcmp (width_pos, "8_t"))
194 width = 8;
195 else if (!strcmp (width_pos, "16_t"))
196 width = 16;
197 else if (!strcmp (width_pos, "32_t"))
198 width = 32;
199 else if (!strcmp (width_pos, "64_t"))
200 width = 64;
201 else
202 {
203 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
204 result.SetStatus (eReturnStatusFailed);
205 return false;
206 }
207
208 type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
209
210 if (!type)
211 {
212 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
213 arg_type_cstr,
214 (encoding == eEncodingSint ? "signed" : "unsigned"),
215 width);
216
217 result.SetStatus (eReturnStatusFailed);
218 return false;
219 }
220 }
221 else if (strchr (arg_type_cstr, '*'))
222 {
223 if (!strcmp (arg_type_cstr, "void*"))
Greg Clayton960d6a42010-08-03 00:35:52 +0000224 type = ast_context.CreatePointerType (ast_context.GetBuiltInType_void ());
Chris Lattner24943d22010-06-08 16:52:24 +0000225 else if (!strcmp (arg_type_cstr, "char*"))
226 type = ast_context.GetCStringType (false);
227 else
228 {
229 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
230 result.SetStatus (eReturnStatusFailed);
231 return false;
232 }
233 }
234 else
235 {
236 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
237 result.SetStatus (eReturnStatusFailed);
238 return false;
239 }
240
Greg Clayton6916e352010-11-13 03:52:47 +0000241 value.SetContext (Value::eContextTypeClangType, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000242
243 value_list.PushValue(value);
244 }
245
246 if (!abi->GetArgumentValues (*thread, value_list))
247 {
248 result.AppendError ("Couldn't get argument values");
249 result.SetStatus (eReturnStatusFailed);
250 return false;
251 }
252
253 result.GetOutputStream ().Printf("Arguments : \n");
254
255 for (arg_index = 0; arg_index < num_args; ++arg_index)
256 {
Greg Clayton63094e02010-06-23 01:19:29 +0000257 result.GetOutputStream ().Printf ("%d (%s): ", arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +0000258 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
259 result.GetOutputStream ().Printf("\n");
260 }
261
262 return result.Succeeded();
263}
264
Greg Claytonb3448432011-03-24 21:19:54 +0000265OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000266CommandObjectArgs::CommandOptions::g_option_table[] =
267{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000268 { LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
269 { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000270};
271