blob: 694913943bf216aee0ec747a20a052d1508b76b0 [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
Zachary Turner11eb9c62016-10-05 20:03:37 +000032#include "llvm/ADT/StringSwitch.h"
33
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034using namespace lldb;
35using namespace lldb_private;
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037// This command is a toy. I'm just using it to have a way to construct the
38// arguments to
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039// calling functions.
40//
41
Zachary Turner1f0f5b52016-09-22 20:22:55 +000042static OptionDefinition g_arg_options[] = {
43 // clang-format off
44 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation." },
45 // clang-format on
46};
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048CommandObjectArgs::CommandOptions::CommandOptions(
49 CommandInterpreter &interpreter)
50 : Options() {
51 // Keep only one place to reset the values to their defaults
52 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053}
54
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000055CommandObjectArgs::CommandOptions::~CommandOptions() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057Error CommandObjectArgs::CommandOptions::SetOptionValue(
58 uint32_t option_idx, const char *option_arg,
59 ExecutionContext *execution_context) {
60 Error error;
61
62 const int short_option = m_getopt_table[option_idx].val;
63 error.SetErrorStringWithFormat("invalid short option character '%c'",
64 short_option);
65
66 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067}
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069void CommandObjectArgs::CommandOptions::OptionParsingStarting(
70 ExecutionContext *execution_context) {}
71
Zachary Turner1f0f5b52016-09-22 20:22:55 +000072llvm::ArrayRef<OptionDefinition>
73CommandObjectArgs::CommandOptions::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +000074 return llvm::makeArrayRef(g_arg_options);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077CommandObjectArgs::CommandObjectArgs(CommandInterpreter &interpreter)
78 : CommandObjectParsed(interpreter, "args",
79 "When stopped at the start of a function, reads "
80 "function arguments of type (u?)int(8|16|32|64)_t, "
81 "(void|char)*",
82 "args"),
83 m_options(interpreter) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000085CommandObjectArgs::~CommandObjectArgs() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087Options *CommandObjectArgs::GetOptions() { return &m_options; }
88
89bool CommandObjectArgs::DoExecute(Args &args, CommandReturnObject &result) {
90 ConstString target_triple;
91
92 Process *process = m_exe_ctx.GetProcessPtr();
93 if (!process) {
94 result.AppendError("Args found no process.");
95 result.SetStatus(eReturnStatusFailed);
96 return false;
97 }
98
99 const ABI *abi = process->GetABI().get();
100 if (!abi) {
101 result.AppendError("The current process has no ABI.");
102 result.SetStatus(eReturnStatusFailed);
103 return false;
104 }
105
Zachary Turner11eb9c62016-10-05 20:03:37 +0000106 if (args.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 result.AppendError("args requires at least one argument");
108 result.SetStatus(eReturnStatusFailed);
109 return false;
110 }
111
112 Thread *thread = m_exe_ctx.GetThreadPtr();
113
114 if (!thread) {
115 result.AppendError("args found no thread.");
116 result.SetStatus(eReturnStatusFailed);
117 return false;
118 }
119
120 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame();
121 if (!thread_cur_frame) {
122 result.AppendError("The current thread has no current frame.");
123 result.SetStatus(eReturnStatusFailed);
124 return false;
125 }
126
127 ModuleSP thread_module_sp(
128 thread_cur_frame->GetFrameCodeAddress().GetModule());
129 if (!thread_module_sp) {
130 result.AppendError("The PC has no associated module.");
131 result.SetStatus(eReturnStatusFailed);
132 return false;
133 }
134
135 TypeSystem *type_system =
136 thread_module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
137 if (type_system == nullptr) {
138 result.AppendError("Unable to create C type system.");
139 result.SetStatus(eReturnStatusFailed);
140 return false;
141 }
142
143 ValueList value_list;
144
Zachary Turner11eb9c62016-10-05 20:03:37 +0000145 for (auto &arg_entry : args.entries()) {
146 llvm::StringRef arg_type = arg_entry.ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 Value value;
148 value.SetValueType(Value::eValueTypeScalar);
149 CompilerType compiler_type;
150
Zachary Turner11eb9c62016-10-05 20:03:37 +0000151 std::size_t int_pos = arg_type.find("int");
152 if (int_pos != llvm::StringRef::npos) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 Encoding encoding = eEncodingSint;
154
155 int width = 0;
156
Zachary Turner11eb9c62016-10-05 20:03:37 +0000157 if (int_pos > 1) {
158 result.AppendErrorWithFormat("Invalid format: %s.\n",
159 arg_entry.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 result.SetStatus(eReturnStatusFailed);
161 return false;
162 }
Zachary Turner11eb9c62016-10-05 20:03:37 +0000163 if (int_pos == 1 && arg_type[0] != 'u') {
164 result.AppendErrorWithFormat("Invalid format: %s.\n",
165 arg_entry.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 result.SetStatus(eReturnStatusFailed);
167 return false;
168 }
Zachary Turner11eb9c62016-10-05 20:03:37 +0000169 if (arg_type[0] == 'u') {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 encoding = eEncodingUint;
171 }
172
Zachary Turner11eb9c62016-10-05 20:03:37 +0000173 llvm::StringRef width_spec = arg_type.drop_front(int_pos + 3);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174
Zachary Turner11eb9c62016-10-05 20:03:37 +0000175 auto exp_result = llvm::StringSwitch<llvm::Optional<int>>(width_spec)
176 .Case("8_t", 8)
177 .Case("16_t", 16)
178 .Case("32_t", 32)
179 .Case("64_t", 64)
180 .Default(llvm::None);
181 if (!exp_result.hasValue()) {
182 result.AppendErrorWithFormat("Invalid format: %s.\n",
183 arg_entry.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 result.SetStatus(eReturnStatusFailed);
185 return false;
186 }
Zachary Turner11eb9c62016-10-05 20:03:37 +0000187 width = *exp_result;
188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 compiler_type =
190 type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width);
191
192 if (!compiler_type.IsValid()) {
193 result.AppendErrorWithFormat(
194 "Couldn't get Clang type for format %s (%s integer, width %d).\n",
Zachary Turner11eb9c62016-10-05 20:03:37 +0000195 arg_entry.c_str(),
196 (encoding == eEncodingSint ? "signed" : "unsigned"), width);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197
198 result.SetStatus(eReturnStatusFailed);
199 return false;
200 }
Zachary Turner11eb9c62016-10-05 20:03:37 +0000201 } else if (arg_type == "void*") {
202 compiler_type =
203 type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
204 } else if (arg_type == "char*") {
205 compiler_type =
206 type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 } else {
Zachary Turner11eb9c62016-10-05 20:03:37 +0000208 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_entry.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 result.SetStatus(eReturnStatusFailed);
210 return false;
211 }
212
213 value.SetCompilerType(compiler_type);
214 value_list.PushValue(value);
215 }
216
217 if (!abi->GetArgumentValues(*thread, value_list)) {
218 result.AppendError("Couldn't get argument values");
219 result.SetStatus(eReturnStatusFailed);
220 return false;
221 }
222
223 result.GetOutputStream().Printf("Arguments : \n");
224
Zachary Turner11eb9c62016-10-05 20:03:37 +0000225 for (auto entry : llvm::enumerate(args.entries())) {
226 result.GetOutputStream().Printf("%" PRIu64 " (%s): ", (uint64_t)entry.Index,
227 entry.Value.c_str());
228 value_list.GetValueAtIndex(entry.Index)->Dump(&result.GetOutputStream());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 result.GetOutputStream().Printf("\n");
230 }
231
232 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233}