blob: f75ac0b17ab10d47b67ee3798673d4935653b02e [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 () :
Greg Claytonc12b6b42010-10-10 22:28:11 +000040 Options()
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)*",
83 "args")
84{
85}
86
87CommandObjectArgs::~CommandObjectArgs ()
88{
89}
90
91Options *
92CommandObjectArgs::GetOptions ()
93{
94 return &m_options;
95}
96
97bool
Greg Clayton63094e02010-06-23 01:19:29 +000098CommandObjectArgs::Execute
99(
Greg Clayton63094e02010-06-23 01:19:29 +0000100 Args& args,
101 CommandReturnObject &result
102)
Chris Lattner24943d22010-06-08 16:52:24 +0000103{
104 ConstString target_triple;
105
Greg Clayton63094e02010-06-23 01:19:29 +0000106
Greg Clayton238c0a12010-09-18 01:14:36 +0000107 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
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
115 const ABI *abi = process->GetABI ();
116 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 Clayton238c0a12010-09-18 01:14:36 +0000133 Thread *thread = m_interpreter.GetDebugger().GetExecutionContext ().thread;
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 Claytonb04e7a82010-08-24 21:05:24 +0000150 Module *thread_module = thread_cur_frame->GetFrameCodeAddress ().GetModule ();
Chris Lattner24943d22010-06-08 16:52:24 +0000151 if (!thread_module)
152 {
153 result.AppendError ("The PC has no associated module.");
154 result.SetStatus (eReturnStatusFailed);
155 return false;
156 }
157
Greg Claytonb01000f2011-01-17 03:46:26 +0000158 ClangASTContext &ast_context = thread_module->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."},
271 { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000272};
273