]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Commands/CommandObjectArgs.cpp
Merge ^/head r327169 through r327340.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Commands / CommandObjectArgs.cpp
1 //===-- 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
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "CommandObjectArgs.h"
15 #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
16 #include "lldb/Core/Debugger.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/Value.h"
19 #include "lldb/Host/Host.h"
20 #include "lldb/Host/OptionParser.h"
21 #include "lldb/Interpreter/Args.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #include "lldb/Symbol/ClangASTContext.h"
25 #include "lldb/Symbol/ObjectFile.h"
26 #include "lldb/Symbol/Variable.h"
27 #include "lldb/Target/ABI.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32
33 #include "llvm/ADT/StringSwitch.h"
34
35 using namespace lldb;
36 using namespace lldb_private;
37
38 // This command is a toy.  I'm just using it to have a way to construct the
39 // arguments to
40 // calling functions.
41 //
42
43 static OptionDefinition g_arg_options[] = {
44     // clang-format off
45   { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation." },
46     // clang-format on
47 };
48
49 CommandObjectArgs::CommandOptions::CommandOptions(
50     CommandInterpreter &interpreter)
51     : Options() {
52   // Keep only one place to reset the values to their defaults
53   OptionParsingStarting(nullptr);
54 }
55
56 CommandObjectArgs::CommandOptions::~CommandOptions() = default;
57
58 Status CommandObjectArgs::CommandOptions::SetOptionValue(
59     uint32_t option_idx, llvm::StringRef option_arg,
60     ExecutionContext *execution_context) {
61   Status error;
62
63   const int short_option = m_getopt_table[option_idx].val;
64   error.SetErrorStringWithFormat("invalid short option character '%c'",
65                                  short_option);
66
67   return error;
68 }
69
70 void CommandObjectArgs::CommandOptions::OptionParsingStarting(
71     ExecutionContext *execution_context) {}
72
73 llvm::ArrayRef<OptionDefinition>
74 CommandObjectArgs::CommandOptions::GetDefinitions() {
75   return llvm::makeArrayRef(g_arg_options);
76 }
77
78 CommandObjectArgs::CommandObjectArgs(CommandInterpreter &interpreter)
79     : CommandObjectParsed(interpreter, "args",
80                           "When stopped at the start of a function, reads "
81                           "function arguments of type (u?)int(8|16|32|64)_t, "
82                           "(void|char)*",
83                           "args"),
84       m_options(interpreter) {}
85
86 CommandObjectArgs::~CommandObjectArgs() = default;
87
88 Options *CommandObjectArgs::GetOptions() { return &m_options; }
89
90 bool CommandObjectArgs::DoExecute(Args &args, CommandReturnObject &result) {
91   ConstString target_triple;
92
93   Process *process = m_exe_ctx.GetProcessPtr();
94   if (!process) {
95     result.AppendError("Args found no process.");
96     result.SetStatus(eReturnStatusFailed);
97     return false;
98   }
99
100   const ABI *abi = process->GetABI().get();
101   if (!abi) {
102     result.AppendError("The current process has no ABI.");
103     result.SetStatus(eReturnStatusFailed);
104     return false;
105   }
106
107   if (args.empty()) {
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 (auto &arg_entry : args.entries()) {
147     llvm::StringRef arg_type = arg_entry.ref;
148     Value value;
149     value.SetValueType(Value::eValueTypeScalar);
150     CompilerType compiler_type;
151
152     std::size_t int_pos = arg_type.find("int");
153     if (int_pos != llvm::StringRef::npos) {
154       Encoding encoding = eEncodingSint;
155
156       int width = 0;
157
158       if (int_pos > 1) {
159         result.AppendErrorWithFormat("Invalid format: %s.\n",
160                                      arg_entry.c_str());
161         result.SetStatus(eReturnStatusFailed);
162         return false;
163       }
164       if (int_pos == 1 && arg_type[0] != 'u') {
165         result.AppendErrorWithFormat("Invalid format: %s.\n",
166                                      arg_entry.c_str());
167         result.SetStatus(eReturnStatusFailed);
168         return false;
169       }
170       if (arg_type[0] == 'u') {
171         encoding = eEncodingUint;
172       }
173
174       llvm::StringRef width_spec = arg_type.drop_front(int_pos + 3);
175
176       auto exp_result = llvm::StringSwitch<llvm::Optional<int>>(width_spec)
177                             .Case("8_t", 8)
178                             .Case("16_t", 16)
179                             .Case("32_t", 32)
180                             .Case("64_t", 64)
181                             .Default(llvm::None);
182       if (!exp_result.hasValue()) {
183         result.AppendErrorWithFormat("Invalid format: %s.\n",
184                                      arg_entry.c_str());
185         result.SetStatus(eReturnStatusFailed);
186         return false;
187       }
188       width = *exp_result;
189
190       compiler_type =
191           type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width);
192
193       if (!compiler_type.IsValid()) {
194         result.AppendErrorWithFormat(
195             "Couldn't get Clang type for format %s (%s integer, width %d).\n",
196             arg_entry.c_str(),
197             (encoding == eEncodingSint ? "signed" : "unsigned"), width);
198
199         result.SetStatus(eReturnStatusFailed);
200         return false;
201       }
202     } else if (arg_type == "void*") {
203       compiler_type =
204           type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
205     } else if (arg_type == "char*") {
206       compiler_type =
207           type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType();
208     } else {
209       result.AppendErrorWithFormat("Invalid format: %s.\n", arg_entry.c_str());
210       result.SetStatus(eReturnStatusFailed);
211       return false;
212     }
213
214     value.SetCompilerType(compiler_type);
215     value_list.PushValue(value);
216   }
217
218   if (!abi->GetArgumentValues(*thread, value_list)) {
219     result.AppendError("Couldn't get argument values");
220     result.SetStatus(eReturnStatusFailed);
221     return false;
222   }
223
224   result.GetOutputStream().Printf("Arguments : \n");
225
226   for (auto entry : llvm::enumerate(args.entries())) {
227     result.GetOutputStream().Printf(
228         "%" PRIu64 " (%s): ", (uint64_t)entry.index(), entry.value().c_str());
229     value_list.GetValueAtIndex(entry.index())->Dump(&result.GetOutputStream());
230     result.GetOutputStream().Printf("\n");
231   }
232
233   return result.Succeeded();
234 }