blob: 39dcba07ccab21d0dfa377ea2ba9d8f25a7e2e7b [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Ingham40af72e2010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
Greg Clayton1f746072012-08-29 21:13:06 +000017#include "lldb/Core/Debugger.h"
18#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Value.h"
20#include "lldb/Expression/ClangExpression.h"
Sean Callanan30e33972015-09-03 00:48:23 +000021#include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Expression/ClangFunction.h"
23#include "lldb/Host/Host.h"
24#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Interpreter/CommandReturnObject.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000026#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Symbol/ObjectFile.h"
28#include "lldb/Symbol/Variable.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000029#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Target/Process.h"
31#include "lldb/Target/Target.h"
32#include "lldb/Target/Thread.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000033#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
35using namespace lldb;
36using namespace lldb_private;
37
38// This command is a toy. I'm just using it to have a way to construct the arguments to
39// calling functions.
40//
41
Greg Claytoneb0103f2011-04-07 22:46:35 +000042CommandObjectArgs::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chenf16066e2011-04-08 22:39:17 +000043 Options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044{
45 // Keep only one place to reset the values to their defaults
Greg Claytonf6b8b582011-04-13 00:18:08 +000046 OptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047}
48
49
50CommandObjectArgs::CommandOptions::~CommandOptions ()
51{
52}
53
54Error
Greg Claytonf6b8b582011-04-13 00:18:08 +000055CommandObjectArgs::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056{
57 Error error;
58
Greg Clayton3bcdfc02012-12-04 00:32:51 +000059 const int short_option = m_getopt_table[option_idx].val;
Todd Fiala0a70a842014-05-28 16:43:26 +000060 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
62 return error;
63}
64
65void
Greg Claytonf6b8b582011-04-13 00:18:08 +000066CommandObjectArgs::CommandOptions::OptionParsingStarting ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068}
69
Greg Claytone0d378b2011-03-24 21:19:54 +000070const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071CommandObjectArgs::CommandOptions::GetDefinitions ()
72{
73 return g_option_table;
74}
75
Greg Claytona7015092010-09-18 01:14:36 +000076CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +000077 CommandObjectParsed (interpreter,
78 "args",
79 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
80 "args"),
Greg Claytoneb0103f2011-04-07 22:46:35 +000081 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082{
83}
84
85CommandObjectArgs::~CommandObjectArgs ()
86{
87}
88
89Options *
90CommandObjectArgs::GetOptions ()
91{
92 return &m_options;
93}
94
95bool
Jim Ingham5a988412012-06-08 21:56:10 +000096CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097{
98 ConstString target_triple;
99
Greg Clayton66111032010-06-23 01:19:29 +0000100
Greg Claytonf9fc6092013-01-09 19:44:40 +0000101 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 if (!process)
103 {
104 result.AppendError ("Args found no process.");
105 result.SetStatus (eReturnStatusFailed);
106 return false;
107 }
108
Greg Clayton31f1d2f2011-05-11 18:39:18 +0000109 const ABI *abi = process->GetABI().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 if (!abi)
111 {
112 result.AppendError ("The current process has no ABI.");
113 result.SetStatus (eReturnStatusFailed);
114 return false;
115 }
116
Greg Claytonc7bece562013-01-25 18:06:21 +0000117 const size_t num_args = args.GetArgumentCount ();
Andy Gibbsa297a972013-06-19 19:04:53 +0000118 size_t arg_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
120 if (!num_args)
121 {
122 result.AppendError ("args requires at least one argument");
123 result.SetStatus (eReturnStatusFailed);
124 return false;
125 }
126
Greg Claytonf9fc6092013-01-09 19:44:40 +0000127 Thread *thread = m_exe_ctx.GetThreadPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128
129 if (!thread)
130 {
131 result.AppendError ("args found no thread.");
132 result.SetStatus (eReturnStatusFailed);
133 return false;
134 }
135
Jason Molendab57e4a12013-11-04 09:33:30 +0000136 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 if (!thread_cur_frame)
138 {
139 result.AppendError ("The current thread has no current frame.");
140 result.SetStatus (eReturnStatusFailed);
141 return false;
142 }
143
Greg Claytone72dfb32012-02-24 01:59:29 +0000144 ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule());
145 if (!thread_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 {
147 result.AppendError ("The PC has no associated module.");
148 result.SetStatus (eReturnStatusFailed);
149 return false;
150 }
151
Greg Claytone72dfb32012-02-24 01:59:29 +0000152 ClangASTContext &ast_context = thread_module_sp->GetClangASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153
154 ValueList value_list;
155
156 for (arg_index = 0; arg_index < num_args; ++arg_index)
157 {
Greg Clayton66111032010-06-23 01:19:29 +0000158 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159 Value value;
160 value.SetValueType(Value::eValueTypeScalar);
Greg Claytona1e5dc82015-08-11 22:53:00 +0000161 CompilerType clang_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162
163 char *int_pos;
Eli Friedman59817b12010-06-09 07:57:51 +0000164 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 {
166 Encoding encoding = eEncodingSint;
167
168 int width = 0;
169
170 if (int_pos > arg_type_cstr + 1)
171 {
172 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
173 result.SetStatus (eReturnStatusFailed);
174 return false;
175 }
176 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u')
177 {
178 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
179 result.SetStatus (eReturnStatusFailed);
180 return false;
181 }
182 if (arg_type_cstr[0] == 'u')
183 {
184 encoding = eEncodingUint;
185 }
186
187 char *width_pos = int_pos + 3;
188
189 if (!strcmp (width_pos, "8_t"))
190 width = 8;
191 else if (!strcmp (width_pos, "16_t"))
192 width = 16;
193 else if (!strcmp (width_pos, "32_t"))
194 width = 32;
195 else if (!strcmp (width_pos, "64_t"))
196 width = 64;
197 else
198 {
199 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
200 result.SetStatus (eReturnStatusFailed);
201 return false;
202 }
203
Greg Clayton57ee3062013-07-11 22:46:58 +0000204 clang_type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205
Greg Clayton57ee3062013-07-11 22:46:58 +0000206 if (!clang_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 {
208 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
209 arg_type_cstr,
210 (encoding == eEncodingSint ? "signed" : "unsigned"),
211 width);
212
213 result.SetStatus (eReturnStatusFailed);
214 return false;
215 }
216 }
217 else if (strchr (arg_type_cstr, '*'))
218 {
219 if (!strcmp (arg_type_cstr, "void*"))
Greg Clayton57ee3062013-07-11 22:46:58 +0000220 clang_type = ast_context.GetBasicType(eBasicTypeVoid).GetPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221 else if (!strcmp (arg_type_cstr, "char*"))
Greg Clayton57ee3062013-07-11 22:46:58 +0000222 clang_type = ast_context.GetCStringType (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 else
224 {
225 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
226 result.SetStatus (eReturnStatusFailed);
227 return false;
228 }
229 }
230 else
231 {
232 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
233 result.SetStatus (eReturnStatusFailed);
234 return false;
235 }
236
Greg Clayton99558cc42015-08-24 23:46:31 +0000237 value.SetCompilerType (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 value_list.PushValue(value);
239 }
240
241 if (!abi->GetArgumentValues (*thread, value_list))
242 {
243 result.AppendError ("Couldn't get argument values");
244 result.SetStatus (eReturnStatusFailed);
245 return false;
246 }
247
248 result.GetOutputStream ().Printf("Arguments : \n");
249
250 for (arg_index = 0; arg_index < num_args; ++arg_index)
251 {
Deepak Panickal99fbc072014-03-03 15:39:47 +0000252 result.GetOutputStream ().Printf ("%" PRIu64 " (%s): ", (uint64_t)arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
254 result.GetOutputStream ().Printf("\n");
255 }
256
257 return result.Succeeded();
258}
259
Greg Claytone0d378b2011-03-24 21:19:54 +0000260OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261CommandObjectArgs::CommandOptions::g_option_table[] =
262{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000263 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
264 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265};
266