blob: ac1b58179558c2ceccdec01646460cbeccefcce1 [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) :
Todd Fialae1cfbc72016-08-11 23:51:28 +000040 Options()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041{
42 // Keep only one place to reset the values to their defaults
Todd Fialae1cfbc72016-08-11 23:51:28 +000043 OptionParsingStarting(nullptr);
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
Todd Fialae1cfbc72016-08-11 23:51:28 +000049CommandObjectArgs::CommandOptions::SetOptionValue(uint32_t option_idx,
50 const char *option_arg,
51 ExecutionContext *execution_context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052{
53 Error error;
54
Greg Clayton3bcdfc02012-12-04 00:32:51 +000055 const int short_option = m_getopt_table[option_idx].val;
Todd Fiala0a70a842014-05-28 16:43:26 +000056 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
58 return error;
59}
60
61void
Todd Fialae1cfbc72016-08-11 23:51:28 +000062CommandObjectArgs::CommandOptions::OptionParsingStarting(
63 ExecutionContext *execution_context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065}
66
Greg Claytone0d378b2011-03-24 21:19:54 +000067const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068CommandObjectArgs::CommandOptions::GetDefinitions ()
69{
70 return g_option_table;
71}
72
Greg Claytona7015092010-09-18 01:14:36 +000073CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +000074 CommandObjectParsed (interpreter,
75 "args",
76 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
77 "args"),
Greg Claytoneb0103f2011-04-07 22:46:35 +000078 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079{
80}
81
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000082CommandObjectArgs::~CommandObjectArgs() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083
84Options *
85CommandObjectArgs::GetOptions ()
86{
87 return &m_options;
88}
89
90bool
Jim Ingham5a988412012-06-08 21:56:10 +000091CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092{
93 ConstString target_triple;
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000094
Greg Claytonf9fc6092013-01-09 19:44:40 +000095 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096 if (!process)
97 {
98 result.AppendError ("Args found no process.");
99 result.SetStatus (eReturnStatusFailed);
100 return false;
101 }
102
Greg Clayton31f1d2f2011-05-11 18:39:18 +0000103 const ABI *abi = process->GetABI().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 if (!abi)
105 {
106 result.AppendError ("The current process has no ABI.");
107 result.SetStatus (eReturnStatusFailed);
108 return false;
109 }
110
Greg Claytonc7bece562013-01-25 18:06:21 +0000111 const size_t num_args = args.GetArgumentCount ();
Andy Gibbsa297a972013-06-19 19:04:53 +0000112 size_t arg_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
114 if (!num_args)
115 {
116 result.AppendError ("args requires at least one argument");
117 result.SetStatus (eReturnStatusFailed);
118 return false;
119 }
120
Greg Claytonf9fc6092013-01-09 19:44:40 +0000121 Thread *thread = m_exe_ctx.GetThreadPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
123 if (!thread)
124 {
125 result.AppendError ("args found no thread.");
126 result.SetStatus (eReturnStatusFailed);
127 return false;
128 }
129
Jason Molendab57e4a12013-11-04 09:33:30 +0000130 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 if (!thread_cur_frame)
132 {
133 result.AppendError ("The current thread has no current frame.");
134 result.SetStatus (eReturnStatusFailed);
135 return false;
136 }
137
Greg Claytone72dfb32012-02-24 01:59:29 +0000138 ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule());
139 if (!thread_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 {
141 result.AppendError ("The PC has no associated module.");
142 result.SetStatus (eReturnStatusFailed);
143 return false;
144 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000145
146 TypeSystem *type_system = thread_module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
147 if (type_system == nullptr)
148 {
149 result.AppendError ("Unable to create C type system.");
150 result.SetStatus (eReturnStatusFailed);
151 return false;
152 }
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 Clayton56939cb2015-09-17 22:23:34 +0000161 CompilerType compiler_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 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000203 compiler_type = type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204
Greg Clayton56939cb2015-09-17 22:23:34 +0000205 if (!compiler_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 {
207 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
208 arg_type_cstr,
209 (encoding == eEncodingSint ? "signed" : "unsigned"),
210 width);
211
212 result.SetStatus (eReturnStatusFailed);
213 return false;
214 }
215 }
216 else if (strchr (arg_type_cstr, '*'))
217 {
218 if (!strcmp (arg_type_cstr, "void*"))
Greg Clayton56939cb2015-09-17 22:23:34 +0000219 compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 else if (!strcmp (arg_type_cstr, "char*"))
Greg Clayton56939cb2015-09-17 22:23:34 +0000221 compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222 else
223 {
224 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
225 result.SetStatus (eReturnStatusFailed);
226 return false;
227 }
228 }
229 else
230 {
231 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
232 result.SetStatus (eReturnStatusFailed);
233 return false;
234 }
235
Greg Clayton56939cb2015-09-17 22:23:34 +0000236 value.SetCompilerType (compiler_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 value_list.PushValue(value);
238 }
239
240 if (!abi->GetArgumentValues (*thread, value_list))
241 {
242 result.AppendError ("Couldn't get argument values");
243 result.SetStatus (eReturnStatusFailed);
244 return false;
245 }
246
247 result.GetOutputStream ().Printf("Arguments : \n");
248
249 for (arg_index = 0; arg_index < num_args; ++arg_index)
250 {
Deepak Panickal99fbc072014-03-03 15:39:47 +0000251 result.GetOutputStream ().Printf ("%" PRIu64 " (%s): ", (uint64_t)arg_index, args.GetArgumentAtIndex (arg_index));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
253 result.GetOutputStream ().Printf("\n");
254 }
255
256 return result.Succeeded();
257}
258
Greg Claytone0d378b2011-03-24 21:19:54 +0000259OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260CommandObjectArgs::CommandOptions::g_option_table[] =
261{
Kate Stoneac9c3a62016-08-26 23:28:47 +0000262 // clang-format off
263 {LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
264 {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}
265 // clang-format on
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266};