blob: eec83b7db43c02d3fe40fbd7af265378992120cd [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
Kate Stoneb9c1b512016-09-06 20:57:50 +000040CommandObjectArgs::CommandOptions::CommandOptions(
41 CommandInterpreter &interpreter)
42 : Options() {
43 // Keep only one place to reset the values to their defaults
44 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045}
46
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000047CommandObjectArgs::CommandOptions::~CommandOptions() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049Error CommandObjectArgs::CommandOptions::SetOptionValue(
50 uint32_t option_idx, const char *option_arg,
51 ExecutionContext *execution_context) {
52 Error error;
53
54 const int short_option = m_getopt_table[option_idx].val;
55 error.SetErrorStringWithFormat("invalid short option character '%c'",
56 short_option);
57
58 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059}
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061void CommandObjectArgs::CommandOptions::OptionParsingStarting(
62 ExecutionContext *execution_context) {}
63
64const OptionDefinition *CommandObjectArgs::CommandOptions::GetDefinitions() {
65 return g_option_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068CommandObjectArgs::CommandObjectArgs(CommandInterpreter &interpreter)
69 : CommandObjectParsed(interpreter, "args",
70 "When stopped at the start of a function, reads "
71 "function arguments of type (u?)int(8|16|32|64)_t, "
72 "(void|char)*",
73 "args"),
74 m_options(interpreter) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000076CommandObjectArgs::~CommandObjectArgs() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078Options *CommandObjectArgs::GetOptions() { return &m_options; }
79
80bool CommandObjectArgs::DoExecute(Args &args, CommandReturnObject &result) {
81 ConstString target_triple;
82
83 Process *process = m_exe_ctx.GetProcessPtr();
84 if (!process) {
85 result.AppendError("Args found no process.");
86 result.SetStatus(eReturnStatusFailed);
87 return false;
88 }
89
90 const ABI *abi = process->GetABI().get();
91 if (!abi) {
92 result.AppendError("The current process has no ABI.");
93 result.SetStatus(eReturnStatusFailed);
94 return false;
95 }
96
97 const size_t num_args = args.GetArgumentCount();
98 size_t arg_index;
99
100 if (!num_args) {
101 result.AppendError("args requires at least one argument");
102 result.SetStatus(eReturnStatusFailed);
103 return false;
104 }
105
106 Thread *thread = m_exe_ctx.GetThreadPtr();
107
108 if (!thread) {
109 result.AppendError("args found no thread.");
110 result.SetStatus(eReturnStatusFailed);
111 return false;
112 }
113
114 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame();
115 if (!thread_cur_frame) {
116 result.AppendError("The current thread has no current frame.");
117 result.SetStatus(eReturnStatusFailed);
118 return false;
119 }
120
121 ModuleSP thread_module_sp(
122 thread_cur_frame->GetFrameCodeAddress().GetModule());
123 if (!thread_module_sp) {
124 result.AppendError("The PC has no associated module.");
125 result.SetStatus(eReturnStatusFailed);
126 return false;
127 }
128
129 TypeSystem *type_system =
130 thread_module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
131 if (type_system == nullptr) {
132 result.AppendError("Unable to create C type system.");
133 result.SetStatus(eReturnStatusFailed);
134 return false;
135 }
136
137 ValueList value_list;
138
139 for (arg_index = 0; arg_index < num_args; ++arg_index) {
140 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
141 Value value;
142 value.SetValueType(Value::eValueTypeScalar);
143 CompilerType compiler_type;
144
145 char *int_pos;
146 if ((int_pos = strstr(const_cast<char *>(arg_type_cstr), "int"))) {
147 Encoding encoding = eEncodingSint;
148
149 int width = 0;
150
151 if (int_pos > arg_type_cstr + 1) {
152 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
153 result.SetStatus(eReturnStatusFailed);
154 return false;
155 }
156 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u') {
157 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
158 result.SetStatus(eReturnStatusFailed);
159 return false;
160 }
161 if (arg_type_cstr[0] == 'u') {
162 encoding = eEncodingUint;
163 }
164
165 char *width_pos = int_pos + 3;
166
167 if (!strcmp(width_pos, "8_t"))
168 width = 8;
169 else if (!strcmp(width_pos, "16_t"))
170 width = 16;
171 else if (!strcmp(width_pos, "32_t"))
172 width = 32;
173 else if (!strcmp(width_pos, "64_t"))
174 width = 64;
175 else {
176 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
177 result.SetStatus(eReturnStatusFailed);
178 return false;
179 }
180 compiler_type =
181 type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width);
182
183 if (!compiler_type.IsValid()) {
184 result.AppendErrorWithFormat(
185 "Couldn't get Clang type for format %s (%s integer, width %d).\n",
186 arg_type_cstr, (encoding == eEncodingSint ? "signed" : "unsigned"),
187 width);
188
189 result.SetStatus(eReturnStatusFailed);
190 return false;
191 }
192 } else if (strchr(arg_type_cstr, '*')) {
193 if (!strcmp(arg_type_cstr, "void*"))
194 compiler_type =
195 type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
196 else if (!strcmp(arg_type_cstr, "char*"))
197 compiler_type =
198 type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType();
199 else {
200 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
201 result.SetStatus(eReturnStatusFailed);
202 return false;
203 }
204 } else {
205 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_type_cstr);
206 result.SetStatus(eReturnStatusFailed);
207 return false;
208 }
209
210 value.SetCompilerType(compiler_type);
211 value_list.PushValue(value);
212 }
213
214 if (!abi->GetArgumentValues(*thread, value_list)) {
215 result.AppendError("Couldn't get argument values");
216 result.SetStatus(eReturnStatusFailed);
217 return false;
218 }
219
220 result.GetOutputStream().Printf("Arguments : \n");
221
222 for (arg_index = 0; arg_index < num_args; ++arg_index) {
223 result.GetOutputStream().Printf("%" PRIu64 " (%s): ", (uint64_t)arg_index,
224 args.GetArgumentAtIndex(arg_index));
225 value_list.GetValueAtIndex(arg_index)->Dump(&result.GetOutputStream());
226 result.GetOutputStream().Printf("\n");
227 }
228
229 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230}
231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232OptionDefinition CommandObjectArgs::CommandOptions::g_option_table[] = {
233 // clang-format off
Kate Stoneac9c3a62016-08-26 23:28:47 +0000234 {LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
235 {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 // clang-format on
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237};