blob: 0ed4a06e5648b5b869a1c0f3d81ed551b4a53b23 [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
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{
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) :
80 CommandObject (interpreter,
81 "args",
Chris Lattner24943d22010-06-08 16:52:24 +000082 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
Greg Claytonf15996e2011-04-07 22:46:35 +000083 "args"),
84 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
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 Claytonb72d0f02011-04-12 05:54:46 +0000108 Process *process = m_interpreter.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 Claytonb72d0f02011-04-12 05:54:46 +0000134 Thread *thread = m_interpreter.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
Greg Claytonb01000f2011-01-17 03:46:26 +0000159 ClangASTContext &ast_context = thread_module->GetClangASTContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000160
161 ValueList value_list;
162
163 for (arg_index = 0; arg_index < num_args; ++arg_index)
164 {
Greg Clayton63094e02010-06-23 01:19:29 +0000165 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
Chris Lattner24943d22010-06-08 16:52:24 +0000166 Value value;
167 value.SetValueType(Value::eValueTypeScalar);
168 void *type;
169
170 char *int_pos;
Eli Friedmanb4a47282010-06-09 07:57:51 +0000171 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
Chris Lattner24943d22010-06-08 16:52:24 +0000172 {
173 Encoding encoding = eEncodingSint;
174
175 int width = 0;
176
177 if (int_pos > arg_type_cstr + 1)
178 {
179 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
180 result.SetStatus (eReturnStatusFailed);
181 return false;
182 }
183 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u')
184 {
185 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
186 result.SetStatus (eReturnStatusFailed);
187 return false;
188 }
189 if (arg_type_cstr[0] == 'u')
190 {
191 encoding = eEncodingUint;
192 }
193
194 char *width_pos = int_pos + 3;
195
196 if (!strcmp (width_pos, "8_t"))
197 width = 8;
198 else if (!strcmp (width_pos, "16_t"))
199 width = 16;
200 else if (!strcmp (width_pos, "32_t"))
201 width = 32;
202 else if (!strcmp (width_pos, "64_t"))
203 width = 64;
204 else
205 {
206 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
207 result.SetStatus (eReturnStatusFailed);
208 return false;
209 }
210
211 type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
212
213 if (!type)
214 {
215 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
216 arg_type_cstr,
217 (encoding == eEncodingSint ? "signed" : "unsigned"),
218 width);
219
220 result.SetStatus (eReturnStatusFailed);
221 return false;
222 }
223 }
224 else if (strchr (arg_type_cstr, '*'))
225 {
226 if (!strcmp (arg_type_cstr, "void*"))
Greg Clayton960d6a42010-08-03 00:35:52 +0000227 type = ast_context.CreatePointerType (ast_context.GetBuiltInType_void ());
Chris Lattner24943d22010-06-08 16:52:24 +0000228 else if (!strcmp (arg_type_cstr, "char*"))
229 type = ast_context.GetCStringType (false);
230 else
231 {
232 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
233 result.SetStatus (eReturnStatusFailed);
234 return false;
235 }
236 }
237 else
238 {
239 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
240 result.SetStatus (eReturnStatusFailed);
241 return false;
242 }
243
Greg Clayton6916e352010-11-13 03:52:47 +0000244 value.SetContext (Value::eContextTypeClangType, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000245
246 value_list.PushValue(value);
247 }
248
249 if (!abi->GetArgumentValues (*thread, value_list))
250 {
251 result.AppendError ("Couldn't get argument values");
252 result.SetStatus (eReturnStatusFailed);
253 return false;
254 }
255
256 result.GetOutputStream ().Printf("Arguments : \n");
257
258 for (arg_index = 0; arg_index < num_args; ++arg_index)
259 {
Greg Clayton63094e02010-06-23 01:19:29 +0000260 result.GetOutputStream ().Printf ("%d (%s): ", arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +0000261 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
262 result.GetOutputStream ().Printf("\n");
263 }
264
265 return result.Succeeded();
266}
267
Greg Claytonb3448432011-03-24 21:19:54 +0000268OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000269CommandObjectArgs::CommandOptions::g_option_table[] =
270{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000271 { LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
272 { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000273};
274