blob: 9f22bba78c55149a75d28892828958a1a413a7b4 [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"
Sean Callanan30e33972015-09-03 00:48:23 +000020#include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Host/Host.h"
22#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Interpreter/CommandReturnObject.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000024#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Symbol/ObjectFile.h"
26#include "lldb/Symbol/Variable.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000027#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Target/Process.h"
29#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000031#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
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 Claytoneb0103f2011-04-07 22:46:35 +000040CommandObjectArgs::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chenf16066e2011-04-08 22:39:17 +000041 Options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042{
43 // Keep only one place to reset the values to their defaults
Greg Claytonf6b8b582011-04-13 00:18:08 +000044 OptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045}
46
47
48CommandObjectArgs::CommandOptions::~CommandOptions ()
49{
50}
51
52Error
Greg Claytonf6b8b582011-04-13 00:18:08 +000053CommandObjectArgs::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054{
55 Error error;
56
Greg Clayton3bcdfc02012-12-04 00:32:51 +000057 const int short_option = m_getopt_table[option_idx].val;
Todd Fiala0a70a842014-05-28 16:43:26 +000058 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
60 return error;
61}
62
63void
Greg Claytonf6b8b582011-04-13 00:18:08 +000064CommandObjectArgs::CommandOptions::OptionParsingStarting ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066}
67
Greg Claytone0d378b2011-03-24 21:19:54 +000068const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069CommandObjectArgs::CommandOptions::GetDefinitions ()
70{
71 return g_option_table;
72}
73
Greg Claytona7015092010-09-18 01:14:36 +000074CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +000075 CommandObjectParsed (interpreter,
76 "args",
77 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
78 "args"),
Greg Claytoneb0103f2011-04-07 22:46:35 +000079 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080{
81}
82
83CommandObjectArgs::~CommandObjectArgs ()
84{
85}
86
87Options *
88CommandObjectArgs::GetOptions ()
89{
90 return &m_options;
91}
92
93bool
Jim Ingham5a988412012-06-08 21:56:10 +000094CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095{
96 ConstString target_triple;
97
Greg Clayton66111032010-06-23 01:19:29 +000098
Greg Claytonf9fc6092013-01-09 19:44:40 +000099 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 if (!process)
101 {
102 result.AppendError ("Args found no process.");
103 result.SetStatus (eReturnStatusFailed);
104 return false;
105 }
106
Greg Clayton31f1d2f2011-05-11 18:39:18 +0000107 const ABI *abi = process->GetABI().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 if (!abi)
109 {
110 result.AppendError ("The current process has no ABI.");
111 result.SetStatus (eReturnStatusFailed);
112 return false;
113 }
114
Greg Claytonc7bece562013-01-25 18:06:21 +0000115 const size_t num_args = args.GetArgumentCount ();
Andy Gibbsa297a972013-06-19 19:04:53 +0000116 size_t arg_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
118 if (!num_args)
119 {
120 result.AppendError ("args requires at least one argument");
121 result.SetStatus (eReturnStatusFailed);
122 return false;
123 }
124
Greg Claytonf9fc6092013-01-09 19:44:40 +0000125 Thread *thread = m_exe_ctx.GetThreadPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126
127 if (!thread)
128 {
129 result.AppendError ("args found no thread.");
130 result.SetStatus (eReturnStatusFailed);
131 return false;
132 }
133
Jason Molendab57e4a12013-11-04 09:33:30 +0000134 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 if (!thread_cur_frame)
136 {
137 result.AppendError ("The current thread has no current frame.");
138 result.SetStatus (eReturnStatusFailed);
139 return false;
140 }
141
Greg Claytone72dfb32012-02-24 01:59:29 +0000142 ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule());
143 if (!thread_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 {
145 result.AppendError ("The PC has no associated module.");
146 result.SetStatus (eReturnStatusFailed);
147 return false;
148 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000149
150 TypeSystem *type_system = thread_module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
151 if (type_system == nullptr)
152 {
153 result.AppendError ("Unable to create C type system.");
154 result.SetStatus (eReturnStatusFailed);
155 return false;
156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157
158 ValueList value_list;
159
160 for (arg_index = 0; arg_index < num_args; ++arg_index)
161 {
Greg Clayton66111032010-06-23 01:19:29 +0000162 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163 Value value;
164 value.SetValueType(Value::eValueTypeScalar);
Greg Clayton56939cb2015-09-17 22:23:34 +0000165 CompilerType compiler_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166
167 char *int_pos;
Eli Friedman59817b12010-06-09 07:57:51 +0000168 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
Chris Lattner30fdc8d2010-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 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000207 compiler_type = type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208
Greg Clayton56939cb2015-09-17 22:23:34 +0000209 if (!compiler_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 {
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 Clayton56939cb2015-09-17 22:23:34 +0000223 compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224 else if (!strcmp (arg_type_cstr, "char*"))
Greg Clayton56939cb2015-09-17 22:23:34 +0000225 compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 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 Clayton56939cb2015-09-17 22:23:34 +0000240 value.SetCompilerType (compiler_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 value_list.PushValue(value);
242 }
243
244 if (!abi->GetArgumentValues (*thread, value_list))
245 {
246 result.AppendError ("Couldn't get argument values");
247 result.SetStatus (eReturnStatusFailed);
248 return false;
249 }
250
251 result.GetOutputStream ().Printf("Arguments : \n");
252
253 for (arg_index = 0; arg_index < num_args; ++arg_index)
254 {
Deepak Panickal99fbc072014-03-03 15:39:47 +0000255 result.GetOutputStream ().Printf ("%" PRIu64 " (%s): ", (uint64_t)arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
257 result.GetOutputStream ().Printf("\n");
258 }
259
260 return result.Succeeded();
261}
262
Greg Claytone0d378b2011-03-24 21:19:54 +0000263OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264CommandObjectArgs::CommandOptions::g_option_table[] =
265{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000266 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
267 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268};
269