blob: 6cd75292ee7afa7d54a8f48e3cccbd0313ed78d2 [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"
Kate Stoneb9c1b512016-09-06 20:57:50 +000015#include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Host/Host.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#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"
Kate Stoneb9c1b512016-09-06 20:57:50 +000028#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
32using namespace lldb;
33using namespace lldb_private;
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035// This command is a toy. I'm just using it to have a way to construct the
36// arguments to
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037// calling functions.
38//
39
Zachary Turner1f0f5b52016-09-22 20:22:55 +000040static OptionDefinition g_arg_options[] = {
41 // clang-format off
42 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation." },
43 // clang-format on
44};
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046CommandObjectArgs::CommandOptions::CommandOptions(
47 CommandInterpreter &interpreter)
48 : Options() {
49 // Keep only one place to reset the values to their defaults
50 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000053CommandObjectArgs::CommandOptions::~CommandOptions() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055Error CommandObjectArgs::CommandOptions::SetOptionValue(
56 uint32_t option_idx, const char *option_arg,
57 ExecutionContext *execution_context) {
58 Error error;
59
60 const int short_option = m_getopt_table[option_idx].val;
61 error.SetErrorStringWithFormat("invalid short option character '%c'",
62 short_option);
63
64 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067void CommandObjectArgs::CommandOptions::OptionParsingStarting(
68 ExecutionContext *execution_context) {}
69
Zachary Turner1f0f5b52016-09-22 20:22:55 +000070llvm::ArrayRef<OptionDefinition>
71CommandObjectArgs::CommandOptions::GetDefinitions() {
72 return g_arg_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075CommandObjectArgs::CommandObjectArgs(CommandInterpreter &interpreter)
76 : CommandObjectParsed(interpreter, "args",
77 "When stopped at the start of a function, reads "
78 "function arguments of type (u?)int(8|16|32|64)_t, "
79 "(void|char)*",
80 "args"),
81 m_options(interpreter) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000083CommandObjectArgs::~CommandObjectArgs() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085Options *CommandObjectArgs::GetOptions() { return &m_options; }
86
87bool CommandObjectArgs::DoExecute(Args &args, CommandReturnObject &result) {
88 ConstString target_triple;
89
90 Process *process = m_exe_ctx.GetProcessPtr();
91 if (!process) {
92 result.AppendError("Args found no process.");
93 result.SetStatus(eReturnStatusFailed);
94 return false;
95 }
96
97 const ABI *abi = process->GetABI().get();
98 if (!abi) {
99 result.AppendError("The current process has no ABI.");
100 result.SetStatus(eReturnStatusFailed);
101 return false;
102 }
103
104 const size_t num_args = args.GetArgumentCount();
105 size_t arg_index;
106
107 if (!num_args) {
108 result.AppendError("args requires at least one argument");
109 result.SetStatus(eReturnStatusFailed);
110 return false;
111 }
112
113 Thread *thread = m_exe_ctx.GetThreadPtr();
114
115 if (!thread) {
116 result.AppendError("args found no thread.");
117 result.SetStatus(eReturnStatusFailed);
118 return false;
119 }
120
121 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame();
122 if (!thread_cur_frame) {
123 result.AppendError("The current thread has no current frame.");
124 result.SetStatus(eReturnStatusFailed);
125 return false;
126 }
127
128 ModuleSP thread_module_sp(
129 thread_cur_frame->GetFrameCodeAddress().GetModule());
130 if (!thread_module_sp) {
131 result.AppendError("The PC has no associated module.");
132 result.SetStatus(eReturnStatusFailed);
133 return false;
134 }
135
136 TypeSystem *type_system =
137 thread_module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
138 if (type_system == nullptr) {
139 result.AppendError("Unable to create C type system.");
140 result.SetStatus(eReturnStatusFailed);
141 return false;
142 }
143
144 ValueList value_list;
145
146 for (arg_index = 0; arg_index < num_args; ++arg_index) {
147 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
148 Value value;
149 value.SetValueType(Value::eValueTypeScalar);
150 CompilerType compiler_type;
151
152 char *int_pos;
153 if ((int_pos = strstr(const_cast<char *>(arg_type_cstr), "int"))) {
154 Encoding encoding = eEncodingSint;
155
156 int width = 0;
157
158 if (int_pos > arg_type_cstr + 1) {
159 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
160 result.SetStatus(eReturnStatusFailed);
161 return false;
162 }
163 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u') {
164 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
165 result.SetStatus(eReturnStatusFailed);
166 return false;
167 }
168 if (arg_type_cstr[0] == 'u') {
169 encoding = eEncodingUint;
170 }
171
172 char *width_pos = int_pos + 3;
173
174 if (!strcmp(width_pos, "8_t"))
175 width = 8;
176 else if (!strcmp(width_pos, "16_t"))
177 width = 16;
178 else if (!strcmp(width_pos, "32_t"))
179 width = 32;
180 else if (!strcmp(width_pos, "64_t"))
181 width = 64;
182 else {
183 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
184 result.SetStatus(eReturnStatusFailed);
185 return false;
186 }
187 compiler_type =
188 type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width);
189
190 if (!compiler_type.IsValid()) {
191 result.AppendErrorWithFormat(
192 "Couldn't get Clang type for format %s (%s integer, width %d).\n",
193 arg_type_cstr, (encoding == eEncodingSint ? "signed" : "unsigned"),
194 width);
195
196 result.SetStatus(eReturnStatusFailed);
197 return false;
198 }
199 } else if (strchr(arg_type_cstr, '*')) {
200 if (!strcmp(arg_type_cstr, "void*"))
201 compiler_type =
202 type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
203 else if (!strcmp(arg_type_cstr, "char*"))
204 compiler_type =
205 type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType();
206 else {
207 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
208 result.SetStatus(eReturnStatusFailed);
209 return false;
210 }
211 } else {
212 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
213 result.SetStatus(eReturnStatusFailed);
214 return false;
215 }
216
217 value.SetCompilerType(compiler_type);
218 value_list.PushValue(value);
219 }
220
221 if (!abi->GetArgumentValues(*thread, value_list)) {
222 result.AppendError("Couldn't get argument values");
223 result.SetStatus(eReturnStatusFailed);
224 return false;
225 }
226
227 result.GetOutputStream().Printf("Arguments : \n");
228
229 for (arg_index = 0; arg_index < num_args; ++arg_index) {
230 result.GetOutputStream().Printf("%" PRIu64 " (%s): ", (uint64_t)arg_index,
231 args.GetArgumentAtIndex(arg_index));
232 value_list.GetValueAtIndex(arg_index)->Dump(&result.GetOutputStream());
233 result.GetOutputStream().Printf("\n");
234 }
235
236 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237}