1 //===-- CommandObjectRegister.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "CommandObjectRegister.h"
16 // Other libraries and framework includes
18 #include "lldb/Core/DataExtractor.h"
19 #include "lldb/Core/RegisterValue.h"
20 #include "lldb/Core/Scalar.h"
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/Interpreter/Args.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Interpreter/CommandReturnObject.h"
25 #include "lldb/Interpreter/Options.h"
26 #include "lldb/Interpreter/OptionGroupFormat.h"
27 #include "lldb/Interpreter/OptionValueArray.h"
28 #include "lldb/Interpreter/OptionValueUInt64.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/RegisterContext.h"
32 #include "lldb/Target/Thread.h"
35 using namespace lldb_private;
37 //----------------------------------------------------------------------
39 //----------------------------------------------------------------------
40 class CommandObjectRegisterRead : public CommandObjectParsed
43 CommandObjectRegisterRead (CommandInterpreter &interpreter) :
44 CommandObjectParsed (interpreter,
46 "Dump the contents of one or more register values from the current frame. If no register is specified, dumps them all.",
49 eFlagRequiresRegContext |
50 eFlagProcessMustBeLaunched |
51 eFlagProcessMustBePaused ),
52 m_option_group (interpreter),
53 m_format_options (eFormatDefault),
56 CommandArgumentEntry arg;
57 CommandArgumentData register_arg;
59 // Define the first (and only) variant of this arg.
60 register_arg.arg_type = eArgTypeRegisterName;
61 register_arg.arg_repetition = eArgRepeatStar;
63 // There is only one variant this argument could be; put it into the argument entry.
64 arg.push_back (register_arg);
66 // Push the data for the first argument into the m_arguments vector.
67 m_arguments.push_back (arg);
70 m_option_group.Append (&m_format_options, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_ALL);
71 m_option_group.Append (&m_command_options);
72 m_option_group.Finalize();
77 ~CommandObjectRegisterRead ()
84 return &m_option_group;
88 DumpRegister (const ExecutionContext &exe_ctx,
90 RegisterContext *reg_ctx,
91 const RegisterInfo *reg_info)
95 RegisterValue reg_value;
97 if (reg_ctx->ReadRegister (reg_info, reg_value))
101 bool prefix_with_altname = m_command_options.alternate_name;
102 bool prefix_with_name = !prefix_with_altname;
103 reg_value.Dump(&strm, reg_info, prefix_with_name, prefix_with_altname, m_format_options.GetFormat(), 8);
104 if ((reg_info->encoding == eEncodingUint) || (reg_info->encoding == eEncodingSint))
106 Process *process = exe_ctx.GetProcessPtr();
107 if (process && reg_info->byte_size == process->GetAddressByteSize())
109 addr_t reg_addr = reg_value.GetAsUInt64(LLDB_INVALID_ADDRESS);
110 if (reg_addr != LLDB_INVALID_ADDRESS)
113 if (exe_ctx.GetTargetRef().GetSectionLoadList().ResolveLoadAddress(reg_addr, so_reg_addr))
115 strm.PutCString (" ");
116 so_reg_addr.Dump(&strm, exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
129 DumpRegisterSet (const ExecutionContext &exe_ctx,
131 RegisterContext *reg_ctx,
133 bool primitive_only=false)
135 uint32_t unavailable_count = 0;
136 uint32_t available_count = 0;
139 return false; // thread has no registers (i.e. core files are corrupt, incomplete crash logs...)
141 const RegisterSet * const reg_set = reg_ctx->GetRegisterSet(set_idx);
144 strm.Printf ("%s:\n", reg_set->name);
146 const size_t num_registers = reg_set->num_registers;
147 for (size_t reg_idx = 0; reg_idx < num_registers; ++reg_idx)
149 const uint32_t reg = reg_set->registers[reg_idx];
150 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg);
151 // Skip the dumping of derived register if primitive_only is true.
152 if (primitive_only && reg_info && reg_info->value_regs)
155 if (DumpRegister (exe_ctx, strm, reg_ctx, reg_info))
161 if (unavailable_count)
164 strm.Printf("%u registers were unavailable.\n", unavailable_count);
168 return available_count > 0;
173 DoExecute (Args& command, CommandReturnObject &result)
175 Stream &strm = result.GetOutputStream();
176 RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext ();
178 const RegisterInfo *reg_info = NULL;
179 if (command.GetArgumentCount() == 0)
183 size_t num_register_sets = 1;
184 const size_t set_array_size = m_command_options.set_indexes.GetSize();
185 if (set_array_size > 0)
187 for (size_t i=0; i<set_array_size; ++i)
189 set_idx = m_command_options.set_indexes[i]->GetUInt64Value (UINT32_MAX, NULL);
190 if (set_idx < reg_ctx->GetRegisterSetCount())
192 if (!DumpRegisterSet (m_exe_ctx, strm, reg_ctx, set_idx))
195 result.AppendErrorWithFormat ("register read failed with errno: %d\n", errno);
197 result.AppendError ("unknown error while reading registers.\n");
198 result.SetStatus (eReturnStatusFailed);
204 result.AppendErrorWithFormat ("invalid register set index: %zu\n", set_idx);
205 result.SetStatus (eReturnStatusFailed);
212 if (m_command_options.dump_all_sets)
213 num_register_sets = reg_ctx->GetRegisterSetCount();
215 for (set_idx = 0; set_idx < num_register_sets; ++set_idx)
217 // When dump_all_sets option is set, dump primitive as well as derived registers.
218 DumpRegisterSet (m_exe_ctx, strm, reg_ctx, set_idx, !m_command_options.dump_all_sets.GetCurrentValue());
224 if (m_command_options.dump_all_sets)
226 result.AppendError ("the --all option can't be used when registers names are supplied as arguments\n");
227 result.SetStatus (eReturnStatusFailed);
229 else if (m_command_options.set_indexes.GetSize() > 0)
231 result.AppendError ("the --set <set> option can't be used when registers names are supplied as arguments\n");
232 result.SetStatus (eReturnStatusFailed);
236 const char *arg_cstr;
237 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
239 // in most LLDB commands we accept $rbx as the name for register RBX - and here we would
240 // reject it and non-existant. we should be more consistent towards the user and allow them
241 // to say reg read $rbx - internally, however, we should be strict and not allow ourselves
242 // to call our registers $rbx in our own API
243 if (*arg_cstr == '$')
244 arg_cstr = arg_cstr+1;
245 reg_info = reg_ctx->GetRegisterInfoByName(arg_cstr);
249 if (!DumpRegister (m_exe_ctx, strm, reg_ctx, reg_info))
250 strm.Printf("%-12s = error: unavailable\n", reg_info->name);
254 result.AppendErrorWithFormat ("Invalid register name '%s'.\n", arg_cstr);
259 return result.Succeeded();
262 class CommandOptions : public OptionGroup
267 set_indexes (OptionValue::ConvertTypeToMask (OptionValue::eTypeUInt64)),
268 dump_all_sets (false, false), // Initial and default values are false
269 alternate_name (false, false)
280 GetNumDefinitions ();
282 virtual const OptionDefinition*
285 return g_option_table;
289 OptionParsingStarting (CommandInterpreter &interpreter)
292 dump_all_sets.Clear();
293 alternate_name.Clear();
297 SetOptionValue (CommandInterpreter &interpreter,
299 const char *option_value)
302 const int short_option = g_option_table[option_idx].short_option;
303 switch (short_option)
307 OptionValueSP value_sp (OptionValueUInt64::Create (option_value, error));
309 set_indexes.AppendValue (value_sp);
314 // When we don't use OptionValue::SetValueFromCString(const char *) to
315 // set an option value, it won't be marked as being set in the options
316 // so we make a call to let users know the value was set via option
317 dump_all_sets.SetCurrentValue (true);
318 dump_all_sets.SetOptionWasSet ();
322 // When we don't use OptionValue::SetValueFromCString(const char *) to
323 // set an option value, it won't be marked as being set in the options
324 // so we make a call to let users know the value was set via option
325 alternate_name.SetCurrentValue (true);
326 dump_all_sets.SetOptionWasSet ();
330 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
336 // Options table: Required for subclasses of Options.
338 static const OptionDefinition g_option_table[];
340 // Instance variables to hold the values for command options.
341 OptionValueArray set_indexes;
342 OptionValueBoolean dump_all_sets;
343 OptionValueBoolean alternate_name;
346 OptionGroupOptions m_option_group;
347 OptionGroupFormat m_format_options;
348 CommandOptions m_command_options;
351 const OptionDefinition
352 CommandObjectRegisterRead::CommandOptions::g_option_table[] =
354 { LLDB_OPT_SET_ALL, false, "alternate", 'A', no_argument , NULL, 0, eArgTypeNone , "Display register names using the alternate register name if there is one."},
355 { LLDB_OPT_SET_1 , false, "set" , 's', required_argument, NULL, 0, eArgTypeIndex , "Specify which register sets to dump by index."},
356 { LLDB_OPT_SET_2 , false, "all" , 'a', no_argument , NULL, 0, eArgTypeNone , "Show all register sets."},
360 CommandObjectRegisterRead::CommandOptions::GetNumDefinitions ()
362 return sizeof(g_option_table)/sizeof(OptionDefinition);
366 //----------------------------------------------------------------------
368 //----------------------------------------------------------------------
369 class CommandObjectRegisterWrite : public CommandObjectParsed
372 CommandObjectRegisterWrite (CommandInterpreter &interpreter) :
373 CommandObjectParsed (interpreter,
375 "Modify a single register value.",
378 eFlagRequiresRegContext |
379 eFlagProcessMustBeLaunched |
380 eFlagProcessMustBePaused)
382 CommandArgumentEntry arg1;
383 CommandArgumentEntry arg2;
384 CommandArgumentData register_arg;
385 CommandArgumentData value_arg;
387 // Define the first (and only) variant of this arg.
388 register_arg.arg_type = eArgTypeRegisterName;
389 register_arg.arg_repetition = eArgRepeatPlain;
391 // There is only one variant this argument could be; put it into the argument entry.
392 arg1.push_back (register_arg);
394 // Define the first (and only) variant of this arg.
395 value_arg.arg_type = eArgTypeValue;
396 value_arg.arg_repetition = eArgRepeatPlain;
398 // There is only one variant this argument could be; put it into the argument entry.
399 arg2.push_back (value_arg);
401 // Push the data for the first argument into the m_arguments vector.
402 m_arguments.push_back (arg1);
403 m_arguments.push_back (arg2);
407 ~CommandObjectRegisterWrite ()
413 DoExecute(Args& command, CommandReturnObject &result)
415 DataExtractor reg_data;
416 RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext ();
418 if (command.GetArgumentCount() != 2)
420 result.AppendError ("register write takes exactly 2 arguments: <reg-name> <value>");
421 result.SetStatus (eReturnStatusFailed);
425 const char *reg_name = command.GetArgumentAtIndex(0);
426 const char *value_str = command.GetArgumentAtIndex(1);
429 // in most LLDB commands we accept $rbx as the name for register RBX - and here we would
430 // reject it and non-existant. we should be more consistent towards the user and allow them
431 // to say reg write $rbx - internally, however, we should be strict and not allow ourselves
432 // to call our registers $rbx in our own API
433 if (reg_name && *reg_name == '$')
434 reg_name = reg_name+1;
436 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name);
440 RegisterValue reg_value;
442 Error error (reg_value.SetValueFromCString (reg_info, value_str));
445 if (reg_ctx->WriteRegister (reg_info, reg_value))
447 // Toss all frames and anything else in the thread
448 // after a register has been written.
449 m_exe_ctx.GetThreadRef().Flush();
450 result.SetStatus (eReturnStatusSuccessFinishNoResult);
454 if (error.AsCString())
456 result.AppendErrorWithFormat ("Failed to write register '%s' with value '%s': %s\n",
463 result.AppendErrorWithFormat ("Failed to write register '%s' with value '%s'",
467 result.SetStatus (eReturnStatusFailed);
471 result.AppendErrorWithFormat ("Register not found for '%s'.\n", reg_name);
472 result.SetStatus (eReturnStatusFailed);
475 return result.Succeeded();
480 //----------------------------------------------------------------------
481 // CommandObjectRegister constructor
482 //----------------------------------------------------------------------
483 CommandObjectRegister::CommandObjectRegister(CommandInterpreter &interpreter) :
484 CommandObjectMultiword (interpreter,
486 "A set of commands to access thread registers.",
487 "register [read|write] ...")
489 LoadSubCommand ("read", CommandObjectSP (new CommandObjectRegisterRead (interpreter)));
490 LoadSubCommand ("write", CommandObjectSP (new CommandObjectRegisterWrite (interpreter)));
494 //----------------------------------------------------------------------
496 //----------------------------------------------------------------------
497 CommandObjectRegister::~CommandObjectRegister()