blob: 206a26f45e4d1a3b5c3100a686202a0a8e2afd80 [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000014#include "CommandObjectArgs.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000015#include "lldb/Interpreter/Args.h"
Greg Clayton1f746072012-08-29 21:13:06 +000016#include "lldb/Core/Debugger.h"
17#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Value.h"
Sean Callanan30e33972015-09-03 00:48:23 +000019#include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Host/Host.h"
21#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Interpreter/CommandReturnObject.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000023#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Symbol/ObjectFile.h"
25#include "lldb/Symbol/Variable.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000026#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/Process.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000030#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
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 Claytoneb0103f2011-04-07 22:46:35 +000039CommandObjectArgs::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chenf16066e2011-04-08 22:39:17 +000040 Options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041{
42 // Keep only one place to reset the values to their defaults
Greg Claytonf6b8b582011-04-13 00:18:08 +000043 OptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044}
45
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000046CommandObjectArgs::CommandOptions::~CommandOptions() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
48Error
Greg Claytonf6b8b582011-04-13 00:18:08 +000049CommandObjectArgs::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050{
51 Error error;
52
Greg Clayton3bcdfc02012-12-04 00:32:51 +000053 const int short_option = m_getopt_table[option_idx].val;
Todd Fiala0a70a842014-05-28 16:43:26 +000054 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
56 return error;
57}
58
59void
Greg Claytonf6b8b582011-04-13 00:18:08 +000060CommandObjectArgs::CommandOptions::OptionParsingStarting ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062}
63
Greg Claytone0d378b2011-03-24 21:19:54 +000064const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065CommandObjectArgs::CommandOptions::GetDefinitions ()
66{
67 return g_option_table;
68}
69
Greg Claytona7015092010-09-18 01:14:36 +000070CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +000071 CommandObjectParsed (interpreter,
72 "args",
73 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
74 "args"),
Greg Claytoneb0103f2011-04-07 22:46:35 +000075 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076{
77}
78
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000079CommandObjectArgs::~CommandObjectArgs() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080
81Options *
82CommandObjectArgs::GetOptions ()
83{
84 return &m_options;
85}
86
87bool
Jim Ingham5a988412012-06-08 21:56:10 +000088CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089{
90 ConstString target_triple;
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000091
Greg Claytonf9fc6092013-01-09 19:44:40 +000092 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093 if (!process)
94 {
95 result.AppendError ("Args found no process.");
96 result.SetStatus (eReturnStatusFailed);
97 return false;
98 }
99
Greg Clayton31f1d2f2011-05-11 18:39:18 +0000100 const ABI *abi = process->GetABI().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 if (!abi)
102 {
103 result.AppendError ("The current process has no ABI.");
104 result.SetStatus (eReturnStatusFailed);
105 return false;
106 }
107
Greg Claytonc7bece562013-01-25 18:06:21 +0000108 const size_t num_args = args.GetArgumentCount ();
Andy Gibbsa297a972013-06-19 19:04:53 +0000109 size_t arg_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110
111 if (!num_args)
112 {
113 result.AppendError ("args requires at least one argument");
114 result.SetStatus (eReturnStatusFailed);
115 return false;
116 }
117
Greg Claytonf9fc6092013-01-09 19:44:40 +0000118 Thread *thread = m_exe_ctx.GetThreadPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
120 if (!thread)
121 {
122 result.AppendError ("args found no thread.");
123 result.SetStatus (eReturnStatusFailed);
124 return false;
125 }
126
Jason Molendab57e4a12013-11-04 09:33:30 +0000127 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 if (!thread_cur_frame)
129 {
130 result.AppendError ("The current thread has no current frame.");
131 result.SetStatus (eReturnStatusFailed);
132 return false;
133 }
134
Greg Claytone72dfb32012-02-24 01:59:29 +0000135 ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule());
136 if (!thread_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 {
138 result.AppendError ("The PC has no associated module.");
139 result.SetStatus (eReturnStatusFailed);
140 return false;
141 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000142
143 TypeSystem *type_system = thread_module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
144 if (type_system == nullptr)
145 {
146 result.AppendError ("Unable to create C type system.");
147 result.SetStatus (eReturnStatusFailed);
148 return false;
149 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150
151 ValueList value_list;
152
153 for (arg_index = 0; arg_index < num_args; ++arg_index)
154 {
Greg Clayton66111032010-06-23 01:19:29 +0000155 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156 Value value;
157 value.SetValueType(Value::eValueTypeScalar);
Greg Clayton56939cb2015-09-17 22:23:34 +0000158 CompilerType compiler_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159
160 char *int_pos;
Eli Friedman59817b12010-06-09 07:57:51 +0000161 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 {
163 Encoding encoding = eEncodingSint;
164
165 int width = 0;
166
167 if (int_pos > arg_type_cstr + 1)
168 {
169 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
170 result.SetStatus (eReturnStatusFailed);
171 return false;
172 }
173 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u')
174 {
175 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
176 result.SetStatus (eReturnStatusFailed);
177 return false;
178 }
179 if (arg_type_cstr[0] == 'u')
180 {
181 encoding = eEncodingUint;
182 }
183
184 char *width_pos = int_pos + 3;
185
186 if (!strcmp (width_pos, "8_t"))
187 width = 8;
188 else if (!strcmp (width_pos, "16_t"))
189 width = 16;
190 else if (!strcmp (width_pos, "32_t"))
191 width = 32;
192 else if (!strcmp (width_pos, "64_t"))
193 width = 64;
194 else
195 {
196 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
197 result.SetStatus (eReturnStatusFailed);
198 return false;
199 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000200 compiler_type = type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
Greg Clayton56939cb2015-09-17 22:23:34 +0000202 if (!compiler_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 {
204 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
205 arg_type_cstr,
206 (encoding == eEncodingSint ? "signed" : "unsigned"),
207 width);
208
209 result.SetStatus (eReturnStatusFailed);
210 return false;
211 }
212 }
213 else if (strchr (arg_type_cstr, '*'))
214 {
215 if (!strcmp (arg_type_cstr, "void*"))
Greg Clayton56939cb2015-09-17 22:23:34 +0000216 compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217 else if (!strcmp (arg_type_cstr, "char*"))
Greg Clayton56939cb2015-09-17 22:23:34 +0000218 compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219 else
220 {
221 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
222 result.SetStatus (eReturnStatusFailed);
223 return false;
224 }
225 }
226 else
227 {
228 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
229 result.SetStatus (eReturnStatusFailed);
230 return false;
231 }
232
Greg Clayton56939cb2015-09-17 22:23:34 +0000233 value.SetCompilerType (compiler_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 value_list.PushValue(value);
235 }
236
237 if (!abi->GetArgumentValues (*thread, value_list))
238 {
239 result.AppendError ("Couldn't get argument values");
240 result.SetStatus (eReturnStatusFailed);
241 return false;
242 }
243
244 result.GetOutputStream ().Printf("Arguments : \n");
245
246 for (arg_index = 0; arg_index < num_args; ++arg_index)
247 {
Deepak Panickal99fbc072014-03-03 15:39:47 +0000248 result.GetOutputStream ().Printf ("%" PRIu64 " (%s): ", (uint64_t)arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
250 result.GetOutputStream ().Printf("\n");
251 }
252
253 return result.Succeeded();
254}
255
Greg Claytone0d378b2011-03-24 21:19:54 +0000256OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257CommandObjectArgs::CommandOptions::g_option_table[] =
258{
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000259 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
260 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261};