blob: f9d6d02fa0287e2945d5b214dc8fb773a1a8255b [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
Greg Claytonf15996e2011-04-07 22:46:35 +000039CommandObjectArgs::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chen93356432011-04-08 22:39:17 +000040 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000041{
42 // Keep only one place to reset the values to their defaults
Greg Clayton143fcc32011-04-13 00:18:08 +000043 OptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +000044}
45
46
47CommandObjectArgs::CommandOptions::~CommandOptions ()
48{
49}
50
51Error
Greg Clayton143fcc32011-04-13 00:18:08 +000052CommandObjectArgs::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000053{
54 Error error;
55
56 char short_option = (char) m_getopt_table[option_idx].val;
57
58 switch (short_option)
59 {
60 default:
Greg Clayton9c236732011-10-26 00:56:27 +000061 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +000062 break;
63 }
64
65 return error;
66}
67
68void
Greg Clayton143fcc32011-04-13 00:18:08 +000069CommandObjectArgs::CommandOptions::OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +000070{
Chris Lattner24943d22010-06-08 16:52:24 +000071}
72
Greg Claytonb3448432011-03-24 21:19:54 +000073const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +000074CommandObjectArgs::CommandOptions::GetDefinitions ()
75{
76 return g_option_table;
77}
78
Greg Clayton238c0a12010-09-18 01:14:36 +000079CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000080 CommandObjectParsed (interpreter,
81 "args",
82 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
83 "args"),
Greg Claytonf15996e2011-04-07 22:46:35 +000084 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000085{
86}
87
88CommandObjectArgs::~CommandObjectArgs ()
89{
90}
91
92Options *
93CommandObjectArgs::GetOptions ()
94{
95 return &m_options;
96}
97
98bool
Jim Inghamda26bd22012-06-08 21:56:10 +000099CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000100{
101 ConstString target_triple;
102
Greg Clayton63094e02010-06-23 01:19:29 +0000103
Greg Clayton567e7f32011-09-22 04:58:26 +0000104 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000105 if (!process)
106 {
107 result.AppendError ("Args found no process.");
108 result.SetStatus (eReturnStatusFailed);
109 return false;
110 }
111
Greg Clayton75906e42011-05-11 18:39:18 +0000112 const ABI *abi = process->GetABI().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000113 if (!abi)
114 {
115 result.AppendError ("The current process has no ABI.");
116 result.SetStatus (eReturnStatusFailed);
117 return false;
118 }
119
Greg Clayton63094e02010-06-23 01:19:29 +0000120 int num_args = args.GetArgumentCount ();
Chris Lattner24943d22010-06-08 16:52:24 +0000121 int arg_index;
122
123 if (!num_args)
124 {
125 result.AppendError ("args requires at least one argument");
126 result.SetStatus (eReturnStatusFailed);
127 return false;
128 }
129
Greg Clayton567e7f32011-09-22 04:58:26 +0000130 Thread *thread = m_interpreter.GetExecutionContext ().GetThreadPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000131
132 if (!thread)
133 {
134 result.AppendError ("args found no thread.");
135 result.SetStatus (eReturnStatusFailed);
136 return false;
137 }
138
Jim Inghamc8332952010-08-26 21:32:51 +0000139 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
Chris Lattner24943d22010-06-08 16:52:24 +0000140 if (!thread_cur_frame)
141 {
142 result.AppendError ("The current thread has no current frame.");
143 result.SetStatus (eReturnStatusFailed);
144 return false;
145 }
146
Greg Clayton3508c382012-02-24 01:59:29 +0000147 ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule());
148 if (!thread_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000149 {
150 result.AppendError ("The PC has no associated module.");
151 result.SetStatus (eReturnStatusFailed);
152 return false;
153 }
154
Greg Clayton3508c382012-02-24 01:59:29 +0000155 ClangASTContext &ast_context = thread_module_sp->GetClangASTContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000156
157 ValueList value_list;
158
159 for (arg_index = 0; arg_index < num_args; ++arg_index)
160 {
Greg Clayton63094e02010-06-23 01:19:29 +0000161 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
Chris Lattner24943d22010-06-08 16:52:24 +0000162 Value value;
163 value.SetValueType(Value::eValueTypeScalar);
164 void *type;
165
166 char *int_pos;
Eli Friedmanb4a47282010-06-09 07:57:51 +0000167 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
Chris Lattner24943d22010-06-08 16:52:24 +0000168 {
169 Encoding encoding = eEncodingSint;
170
171 int width = 0;
172
173 if (int_pos > arg_type_cstr + 1)
174 {
175 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
176 result.SetStatus (eReturnStatusFailed);
177 return false;
178 }
179 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u')
180 {
181 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
182 result.SetStatus (eReturnStatusFailed);
183 return false;
184 }
185 if (arg_type_cstr[0] == 'u')
186 {
187 encoding = eEncodingUint;
188 }
189
190 char *width_pos = int_pos + 3;
191
192 if (!strcmp (width_pos, "8_t"))
193 width = 8;
194 else if (!strcmp (width_pos, "16_t"))
195 width = 16;
196 else if (!strcmp (width_pos, "32_t"))
197 width = 32;
198 else if (!strcmp (width_pos, "64_t"))
199 width = 64;
200 else
201 {
202 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
203 result.SetStatus (eReturnStatusFailed);
204 return false;
205 }
206
207 type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
208
209 if (!type)
210 {
211 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
212 arg_type_cstr,
213 (encoding == eEncodingSint ? "signed" : "unsigned"),
214 width);
215
216 result.SetStatus (eReturnStatusFailed);
217 return false;
218 }
219 }
220 else if (strchr (arg_type_cstr, '*'))
221 {
222 if (!strcmp (arg_type_cstr, "void*"))
Greg Clayton960d6a42010-08-03 00:35:52 +0000223 type = ast_context.CreatePointerType (ast_context.GetBuiltInType_void ());
Chris Lattner24943d22010-06-08 16:52:24 +0000224 else if (!strcmp (arg_type_cstr, "char*"))
225 type = ast_context.GetCStringType (false);
226 else
227 {
228 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
229 result.SetStatus (eReturnStatusFailed);
230 return false;
231 }
232 }
233 else
234 {
235 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
236 result.SetStatus (eReturnStatusFailed);
237 return false;
238 }
239
Greg Clayton6916e352010-11-13 03:52:47 +0000240 value.SetContext (Value::eContextTypeClangType, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000241
242 value_list.PushValue(value);
243 }
244
245 if (!abi->GetArgumentValues (*thread, value_list))
246 {
247 result.AppendError ("Couldn't get argument values");
248 result.SetStatus (eReturnStatusFailed);
249 return false;
250 }
251
252 result.GetOutputStream ().Printf("Arguments : \n");
253
254 for (arg_index = 0; arg_index < num_args; ++arg_index)
255 {
Greg Clayton63094e02010-06-23 01:19:29 +0000256 result.GetOutputStream ().Printf ("%d (%s): ", arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +0000257 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
258 result.GetOutputStream ().Printf("\n");
259 }
260
261 return result.Succeeded();
262}
263
Greg Claytonb3448432011-03-24 21:19:54 +0000264OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000265CommandObjectArgs::CommandOptions::g_option_table[] =
266{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000267 { LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
268 { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000269};
270