1 //===-- CommandObjectMemory.cpp -------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "CommandObjectMemory.h"
10 #include "CommandObjectMemoryTag.h"
11 #include "lldb/Core/DumpDataExtractor.h"
12 #include "lldb/Core/Section.h"
13 #include "lldb/Expression/ExpressionVariable.h"
14 #include "lldb/Host/OptionParser.h"
15 #include "lldb/Interpreter/CommandOptionArgumentTable.h"
16 #include "lldb/Interpreter/CommandReturnObject.h"
17 #include "lldb/Interpreter/OptionArgParser.h"
18 #include "lldb/Interpreter/OptionGroupFormat.h"
19 #include "lldb/Interpreter/OptionGroupMemoryTag.h"
20 #include "lldb/Interpreter/OptionGroupOutputFile.h"
21 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
22 #include "lldb/Interpreter/OptionValueLanguage.h"
23 #include "lldb/Interpreter/OptionValueString.h"
24 #include "lldb/Interpreter/Options.h"
25 #include "lldb/Symbol/SymbolFile.h"
26 #include "lldb/Symbol/TypeList.h"
27 #include "lldb/Target/ABI.h"
28 #include "lldb/Target/Language.h"
29 #include "lldb/Target/MemoryHistory.h"
30 #include "lldb/Target/MemoryRegionInfo.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/StackFrame.h"
33 #include "lldb/Target/Target.h"
34 #include "lldb/Target/Thread.h"
35 #include "lldb/Utility/Args.h"
36 #include "lldb/Utility/DataBufferHeap.h"
37 #include "lldb/Utility/StreamString.h"
38 #include "lldb/ValueObject/ValueObjectMemory.h"
39 #include "llvm/Support/MathExtras.h"
45 using namespace lldb_private
;
47 #define LLDB_OPTIONS_memory_read
48 #include "CommandOptions.inc"
50 class OptionGroupReadMemory
: public OptionGroup
{
52 OptionGroupReadMemory()
53 : m_num_per_line(1, 1), m_offset(0, 0),
54 m_language_for_type(eLanguageTypeUnknown
) {}
56 ~OptionGroupReadMemory() override
= default;
58 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
59 return llvm::ArrayRef(g_memory_read_options
);
62 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_value
,
63 ExecutionContext
*execution_context
) override
{
65 const int short_option
= g_memory_read_options
[option_idx
].short_option
;
67 switch (short_option
) {
69 error
= m_num_per_line
.SetValueFromString(option_value
);
70 if (m_num_per_line
.GetCurrentValue() == 0)
71 error
= Status::FromErrorStringWithFormat(
72 "invalid value for --num-per-line option '%s'",
73 option_value
.str().c_str());
77 m_output_as_binary
= true;
81 error
= m_view_as_type
.SetValueFromString(option_value
);
89 error
= m_language_for_type
.SetValueFromString(option_value
);
93 error
= m_offset
.SetValueFromString(option_value
);
97 llvm_unreachable("Unimplemented option");
102 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
103 m_num_per_line
.Clear();
104 m_output_as_binary
= false;
105 m_view_as_type
.Clear();
108 m_language_for_type
.Clear();
111 Status
FinalizeSettings(Target
*target
, OptionGroupFormat
&format_options
) {
113 OptionValueUInt64
&byte_size_value
= format_options
.GetByteSizeValue();
114 OptionValueUInt64
&count_value
= format_options
.GetCountValue();
115 const bool byte_size_option_set
= byte_size_value
.OptionWasSet();
116 const bool num_per_line_option_set
= m_num_per_line
.OptionWasSet();
117 const bool count_option_set
= format_options
.GetCountValue().OptionWasSet();
119 switch (format_options
.GetFormat()) {
124 if (!byte_size_option_set
)
126 if (!num_per_line_option_set
)
128 if (!count_option_set
)
129 format_options
.GetCountValue() = 8;
135 case eFormatInstruction
:
136 if (count_option_set
)
137 byte_size_value
= target
->GetArchitecture().GetMaximumOpcodeByteSize();
141 case eFormatAddressInfo
:
142 if (!byte_size_option_set
)
143 byte_size_value
= target
->GetArchitecture().GetAddressByteSize();
145 if (!count_option_set
)
146 format_options
.GetCountValue() = 8;
150 byte_size_value
= target
->GetArchitecture().GetAddressByteSize();
151 if (!num_per_line_option_set
)
153 if (!count_option_set
)
154 format_options
.GetCountValue() = 8;
162 case eFormatUnicode8
:
163 case eFormatUnicode16
:
164 case eFormatUnicode32
:
165 case eFormatUnsigned
:
166 case eFormatHexFloat
:
167 if (!byte_size_option_set
)
169 if (!num_per_line_option_set
)
171 if (!count_option_set
)
172 format_options
.GetCountValue() = 8;
176 case eFormatBytesWithASCII
:
177 if (byte_size_option_set
) {
178 if (byte_size_value
> 1)
179 error
= Status::FromErrorStringWithFormat(
180 "display format (bytes/bytes with ASCII) conflicts with the "
181 "specified byte size %" PRIu64
"\n"
182 "\tconsider using a different display format or don't specify "
184 byte_size_value
.GetCurrentValue());
187 if (!num_per_line_option_set
)
189 if (!count_option_set
)
190 format_options
.GetCountValue() = 32;
193 case eFormatCharArray
:
195 case eFormatCharPrintable
:
196 if (!byte_size_option_set
)
198 if (!num_per_line_option_set
)
200 if (!count_option_set
)
201 format_options
.GetCountValue() = 64;
205 if (!byte_size_option_set
)
207 if (!num_per_line_option_set
)
209 if (!count_option_set
)
210 format_options
.GetCountValue() = 8;
213 case eFormatComplexInteger
:
214 if (!byte_size_option_set
)
216 if (!num_per_line_option_set
)
218 if (!count_option_set
)
219 format_options
.GetCountValue() = 8;
223 if (!byte_size_option_set
)
225 if (!num_per_line_option_set
) {
226 switch (byte_size_value
) {
242 if (!count_option_set
)
246 case eFormatVectorOfChar
:
247 case eFormatVectorOfSInt8
:
248 case eFormatVectorOfUInt8
:
249 case eFormatVectorOfSInt16
:
250 case eFormatVectorOfUInt16
:
251 case eFormatVectorOfSInt32
:
252 case eFormatVectorOfUInt32
:
253 case eFormatVectorOfSInt64
:
254 case eFormatVectorOfUInt64
:
255 case eFormatVectorOfFloat16
:
256 case eFormatVectorOfFloat32
:
257 case eFormatVectorOfFloat64
:
258 case eFormatVectorOfUInt128
:
259 if (!byte_size_option_set
)
260 byte_size_value
= 128;
261 if (!num_per_line_option_set
)
263 if (!count_option_set
)
270 bool AnyOptionWasSet() const {
271 return m_num_per_line
.OptionWasSet() || m_output_as_binary
||
272 m_view_as_type
.OptionWasSet() || m_offset
.OptionWasSet() ||
273 m_language_for_type
.OptionWasSet();
276 OptionValueUInt64 m_num_per_line
;
277 bool m_output_as_binary
= false;
278 OptionValueString m_view_as_type
;
279 bool m_force
= false;
280 OptionValueUInt64 m_offset
;
281 OptionValueLanguage m_language_for_type
;
284 // Read memory from the inferior process
285 class CommandObjectMemoryRead
: public CommandObjectParsed
{
287 CommandObjectMemoryRead(CommandInterpreter
&interpreter
)
288 : CommandObjectParsed(
289 interpreter
, "memory read",
290 "Read from the memory of the current target process.", nullptr,
291 eCommandRequiresTarget
| eCommandProcessMustBePaused
),
292 m_format_options(eFormatBytesWithASCII
, 1, 8),
293 m_memory_tag_options(/*note_binary=*/true),
294 m_prev_format_options(eFormatBytesWithASCII
, 1, 8) {
295 CommandArgumentEntry arg1
;
296 CommandArgumentEntry arg2
;
297 CommandArgumentData start_addr_arg
;
298 CommandArgumentData end_addr_arg
;
300 // Define the first (and only) variant of this arg.
301 start_addr_arg
.arg_type
= eArgTypeAddressOrExpression
;
302 start_addr_arg
.arg_repetition
= eArgRepeatPlain
;
304 // There is only one variant this argument could be; put it into the
306 arg1
.push_back(start_addr_arg
);
308 // Define the first (and only) variant of this arg.
309 end_addr_arg
.arg_type
= eArgTypeAddressOrExpression
;
310 end_addr_arg
.arg_repetition
= eArgRepeatOptional
;
312 // There is only one variant this argument could be; put it into the
314 arg2
.push_back(end_addr_arg
);
316 // Push the data for the first argument into the m_arguments vector.
317 m_arguments
.push_back(arg1
);
318 m_arguments
.push_back(arg2
);
320 // Add the "--format" and "--count" options to group 1 and 3
321 m_option_group
.Append(&m_format_options
,
322 OptionGroupFormat::OPTION_GROUP_FORMAT
|
323 OptionGroupFormat::OPTION_GROUP_COUNT
,
324 LLDB_OPT_SET_1
| LLDB_OPT_SET_2
| LLDB_OPT_SET_3
);
325 m_option_group
.Append(&m_format_options
,
326 OptionGroupFormat::OPTION_GROUP_GDB_FMT
,
327 LLDB_OPT_SET_1
| LLDB_OPT_SET_3
);
328 // Add the "--size" option to group 1 and 2
329 m_option_group
.Append(&m_format_options
,
330 OptionGroupFormat::OPTION_GROUP_SIZE
,
331 LLDB_OPT_SET_1
| LLDB_OPT_SET_2
);
332 m_option_group
.Append(&m_memory_options
);
333 m_option_group
.Append(&m_outfile_options
, LLDB_OPT_SET_ALL
,
334 LLDB_OPT_SET_1
| LLDB_OPT_SET_2
| LLDB_OPT_SET_3
);
335 m_option_group
.Append(&m_varobj_options
, LLDB_OPT_SET_ALL
, LLDB_OPT_SET_3
);
336 m_option_group
.Append(&m_memory_tag_options
, LLDB_OPT_SET_ALL
,
338 m_option_group
.Finalize();
341 ~CommandObjectMemoryRead() override
= default;
343 Options
*GetOptions() override
{ return &m_option_group
; }
345 std::optional
<std::string
> GetRepeatCommand(Args
¤t_command_args
,
346 uint32_t index
) override
{
351 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
352 // No need to check "target" for validity as eCommandRequiresTarget ensures
354 Target
*target
= m_exe_ctx
.GetTargetPtr();
356 const size_t argc
= command
.GetArgumentCount();
358 if ((argc
== 0 && m_next_addr
== LLDB_INVALID_ADDRESS
) || argc
> 2) {
359 result
.AppendErrorWithFormat("%s takes a start address expression with "
360 "an optional end address expression.\n",
362 result
.AppendWarning("Expressions should be quoted if they contain "
363 "spaces or other special characters.");
367 CompilerType compiler_type
;
370 const char *view_as_type_cstr
=
371 m_memory_options
.m_view_as_type
.GetCurrentValue();
372 if (view_as_type_cstr
&& view_as_type_cstr
[0]) {
373 // We are viewing memory as a type
375 uint32_t reference_count
= 0;
376 uint32_t pointer_count
= 0;
379 #define ALL_KEYWORDS \
381 KEYWORD("volatile") \
382 KEYWORD("restrict") \
387 #define KEYWORD(s) s,
388 static const char *g_keywords
[] = {ALL_KEYWORDS
};
391 #define KEYWORD(s) (sizeof(s) - 1),
392 static const int g_keyword_lengths
[] = {ALL_KEYWORDS
};
397 static size_t g_num_keywords
= sizeof(g_keywords
) / sizeof(const char *);
398 std::string
type_str(view_as_type_cstr
);
400 // Remove all instances of g_keywords that are followed by spaces
401 for (size_t i
= 0; i
< g_num_keywords
; ++i
) {
402 const char *keyword
= g_keywords
[i
];
403 int keyword_len
= g_keyword_lengths
[i
];
406 while ((idx
= type_str
.find(keyword
, idx
)) != std::string::npos
) {
407 if (type_str
[idx
+ keyword_len
] == ' ' ||
408 type_str
[idx
+ keyword_len
] == '\t') {
409 type_str
.erase(idx
, keyword_len
+ 1);
416 bool done
= type_str
.empty();
418 idx
= type_str
.find_first_not_of(" \t");
419 if (idx
> 0 && idx
!= std::string::npos
)
420 type_str
.erase(0, idx
);
422 // Strip trailing spaces
423 if (type_str
.empty())
426 switch (type_str
[type_str
.size() - 1]) {
432 type_str
.erase(type_str
.size() - 1);
436 if (reference_count
== 0) {
438 type_str
.erase(type_str
.size() - 1);
440 result
.AppendErrorWithFormat("invalid type string: '%s'\n",
453 ConstString
lookup_type_name(type_str
.c_str());
454 StackFrame
*frame
= m_exe_ctx
.GetFramePtr();
455 ModuleSP search_first
;
457 search_first
= frame
->GetSymbolContext(eSymbolContextModule
).module_sp
;
458 TypeQuery
query(lookup_type_name
.GetStringRef(),
459 TypeQueryOptions::e_find_one
);
461 target
->GetImages().FindTypes(search_first
.get(), query
, results
);
462 TypeSP type_sp
= results
.GetFirstType();
464 if (!type_sp
&& lookup_type_name
.GetCString()) {
465 LanguageType language_for_type
=
466 m_memory_options
.m_language_for_type
.GetCurrentValue();
467 std::set
<LanguageType
> languages_to_check
;
468 if (language_for_type
!= eLanguageTypeUnknown
) {
469 languages_to_check
.insert(language_for_type
);
471 languages_to_check
= Language::GetSupportedLanguages();
474 std::set
<CompilerType
> user_defined_types
;
475 for (auto lang
: languages_to_check
) {
476 if (auto *persistent_vars
=
477 target
->GetPersistentExpressionStateForLanguage(lang
)) {
478 if (std::optional
<CompilerType
> type
=
479 persistent_vars
->GetCompilerTypeFromPersistentDecl(
481 user_defined_types
.emplace(*type
);
486 if (user_defined_types
.size() > 1) {
487 result
.AppendErrorWithFormat(
488 "Mutiple types found matching raw type '%s', please disambiguate "
489 "by specifying the language with -x",
490 lookup_type_name
.GetCString());
494 if (user_defined_types
.size() == 1) {
495 compiler_type
= *user_defined_types
.begin();
499 if (!compiler_type
.IsValid()) {
501 compiler_type
= type_sp
->GetFullCompilerType();
503 result
.AppendErrorWithFormat("unable to find any types that match "
504 "the raw type '%s' for full type '%s'\n",
505 lookup_type_name
.GetCString(),
511 while (pointer_count
> 0) {
512 CompilerType pointer_type
= compiler_type
.GetPointerType();
513 if (pointer_type
.IsValid())
514 compiler_type
= pointer_type
;
516 result
.AppendError("unable make a pointer type\n");
522 std::optional
<uint64_t> size
= compiler_type
.GetByteSize(nullptr);
524 result
.AppendErrorWithFormat(
525 "unable to get the byte size of the type '%s'\n",
529 m_format_options
.GetByteSizeValue() = *size
;
531 if (!m_format_options
.GetCountValue().OptionWasSet())
532 m_format_options
.GetCountValue() = 1;
534 error
= m_memory_options
.FinalizeSettings(target
, m_format_options
);
537 // Look for invalid combinations of settings
539 result
.AppendError(error
.AsCString());
544 size_t total_byte_size
= 0;
546 // Use the last address and byte size and all options as they were if no
547 // options have been set
549 total_byte_size
= m_prev_byte_size
;
550 compiler_type
= m_prev_compiler_type
;
551 if (!m_format_options
.AnyOptionWasSet() &&
552 !m_memory_options
.AnyOptionWasSet() &&
553 !m_outfile_options
.AnyOptionWasSet() &&
554 !m_varobj_options
.AnyOptionWasSet() &&
555 !m_memory_tag_options
.AnyOptionWasSet()) {
556 m_format_options
= m_prev_format_options
;
557 m_memory_options
= m_prev_memory_options
;
558 m_outfile_options
= m_prev_outfile_options
;
559 m_varobj_options
= m_prev_varobj_options
;
560 m_memory_tag_options
= m_prev_memory_tag_options
;
564 size_t item_count
= m_format_options
.GetCountValue().GetCurrentValue();
566 // TODO For non-8-bit byte addressable architectures this needs to be
567 // revisited to fully support all lldb's range of formatting options.
568 // Furthermore code memory reads (for those architectures) will not be
569 // correctly formatted even w/o formatting options.
570 size_t item_byte_size
=
571 target
->GetArchitecture().GetDataByteSize() > 1
572 ? target
->GetArchitecture().GetDataByteSize()
573 : m_format_options
.GetByteSizeValue().GetCurrentValue();
575 const size_t num_per_line
=
576 m_memory_options
.m_num_per_line
.GetCurrentValue();
578 if (total_byte_size
== 0) {
579 total_byte_size
= item_count
* item_byte_size
;
580 if (total_byte_size
== 0)
581 total_byte_size
= 32;
585 addr
= OptionArgParser::ToAddress(&m_exe_ctx
, command
[0].ref(),
586 LLDB_INVALID_ADDRESS
, &error
);
588 if (addr
== LLDB_INVALID_ADDRESS
) {
589 result
.AppendError("invalid start address expression.");
590 result
.AppendError(error
.AsCString());
595 lldb::addr_t end_addr
= OptionArgParser::ToAddress(
596 &m_exe_ctx
, command
[1].ref(), LLDB_INVALID_ADDRESS
, nullptr);
598 if (end_addr
== LLDB_INVALID_ADDRESS
) {
599 result
.AppendError("invalid end address expression.");
600 result
.AppendError(error
.AsCString());
602 } else if (end_addr
<= addr
) {
603 result
.AppendErrorWithFormat(
604 "end address (0x%" PRIx64
605 ") must be greater than the start address (0x%" PRIx64
").\n",
608 } else if (m_format_options
.GetCountValue().OptionWasSet()) {
609 result
.AppendErrorWithFormat(
610 "specify either the end address (0x%" PRIx64
611 ") or the count (--count %" PRIu64
"), not both.\n",
612 end_addr
, (uint64_t)item_count
);
616 total_byte_size
= end_addr
- addr
;
617 item_count
= total_byte_size
/ item_byte_size
;
620 uint32_t max_unforced_size
= target
->GetMaximumMemReadSize();
622 if (total_byte_size
> max_unforced_size
&& !m_memory_options
.m_force
) {
623 result
.AppendErrorWithFormat(
624 "Normally, \'memory read\' will not read over %" PRIu32
627 result
.AppendErrorWithFormat(
628 "Please use --force to override this restriction just once.\n");
629 result
.AppendErrorWithFormat("or set target.max-memory-read-size if you "
630 "will often need a larger limit.\n");
634 WritableDataBufferSP data_sp
;
635 size_t bytes_read
= 0;
636 if (compiler_type
.GetOpaqueQualType()) {
637 // Make sure we don't display our type as ASCII bytes like the default
639 if (!m_format_options
.GetFormatValue().OptionWasSet())
640 m_format_options
.GetFormatValue().SetCurrentValue(eFormatDefault
);
642 std::optional
<uint64_t> size
= compiler_type
.GetByteSize(nullptr);
644 result
.AppendError("can't get size of type");
647 bytes_read
= *size
* m_format_options
.GetCountValue().GetCurrentValue();
650 addr
= addr
+ (*size
* m_memory_options
.m_offset
.GetCurrentValue());
651 } else if (m_format_options
.GetFormatValue().GetCurrentValue() !=
653 data_sp
= std::make_shared
<DataBufferHeap
>(total_byte_size
, '\0');
654 if (data_sp
->GetBytes() == nullptr) {
655 result
.AppendErrorWithFormat(
656 "can't allocate 0x%" PRIx32
657 " bytes for the memory read buffer, specify a smaller size to read",
658 (uint32_t)total_byte_size
);
662 Address
address(addr
, nullptr);
663 bytes_read
= target
->ReadMemory(address
, data_sp
->GetBytes(),
664 data_sp
->GetByteSize(), error
, true);
665 if (bytes_read
== 0) {
666 const char *error_cstr
= error
.AsCString();
667 if (error_cstr
&& error_cstr
[0]) {
668 result
.AppendError(error_cstr
);
670 result
.AppendErrorWithFormat(
671 "failed to read memory from 0x%" PRIx64
".\n", addr
);
676 if (bytes_read
< total_byte_size
)
677 result
.AppendWarningWithFormat(
678 "Not all bytes (%" PRIu64
"/%" PRIu64
679 ") were able to be read from 0x%" PRIx64
".\n",
680 (uint64_t)bytes_read
, (uint64_t)total_byte_size
, addr
);
682 // we treat c-strings as a special case because they do not have a fixed
684 if (m_format_options
.GetByteSizeValue().OptionWasSet() &&
685 !m_format_options
.HasGDBFormat())
686 item_byte_size
= m_format_options
.GetByteSizeValue().GetCurrentValue();
688 item_byte_size
= target
->GetMaximumSizeOfStringSummary();
689 if (!m_format_options
.GetCountValue().OptionWasSet())
691 data_sp
= std::make_shared
<DataBufferHeap
>(
692 (item_byte_size
+ 1) * item_count
,
693 '\0'); // account for NULLs as necessary
694 if (data_sp
->GetBytes() == nullptr) {
695 result
.AppendErrorWithFormat(
696 "can't allocate 0x%" PRIx64
697 " bytes for the memory read buffer, specify a smaller size to read",
698 (uint64_t)((item_byte_size
+ 1) * item_count
));
701 uint8_t *data_ptr
= data_sp
->GetBytes();
702 auto data_addr
= addr
;
703 auto count
= item_count
;
705 bool break_on_no_NULL
= false;
706 while (item_count
< count
) {
708 buffer
.resize(item_byte_size
+ 1, 0);
710 size_t read
= target
->ReadCStringFromMemory(data_addr
, &buffer
[0],
711 item_byte_size
+ 1, error
);
713 result
.AppendErrorWithFormat(
714 "failed to read memory from 0x%" PRIx64
".\n", addr
);
718 if (item_byte_size
== read
) {
719 result
.AppendWarningWithFormat(
720 "unable to find a NULL terminated string at 0x%" PRIx64
721 ". Consider increasing the maximum read length.\n",
724 break_on_no_NULL
= true;
726 ++read
; // account for final NULL byte
728 memcpy(data_ptr
, &buffer
[0], read
);
732 item_count
++; // if we break early we know we only read item_count
735 if (break_on_no_NULL
)
739 std::make_shared
<DataBufferHeap
>(data_sp
->GetBytes(), bytes_read
+ 1);
742 m_next_addr
= addr
+ bytes_read
;
743 m_prev_byte_size
= bytes_read
;
744 m_prev_format_options
= m_format_options
;
745 m_prev_memory_options
= m_memory_options
;
746 m_prev_outfile_options
= m_outfile_options
;
747 m_prev_varobj_options
= m_varobj_options
;
748 m_prev_memory_tag_options
= m_memory_tag_options
;
749 m_prev_compiler_type
= compiler_type
;
751 std::unique_ptr
<Stream
> output_stream_storage
;
752 Stream
*output_stream_p
= nullptr;
753 const FileSpec
&outfile_spec
=
754 m_outfile_options
.GetFile().GetCurrentValue();
756 std::string path
= outfile_spec
.GetPath();
759 File::OpenOptions open_options
=
760 File::eOpenOptionWriteOnly
| File::eOpenOptionCanCreate
;
761 const bool append
= m_outfile_options
.GetAppend().GetCurrentValue();
763 append
? File::eOpenOptionAppend
: File::eOpenOptionTruncate
;
765 auto outfile
= FileSystem::Instance().Open(outfile_spec
, open_options
);
768 auto outfile_stream_up
=
769 std::make_unique
<StreamFile
>(std::move(outfile
.get()));
770 if (m_memory_options
.m_output_as_binary
) {
771 const size_t bytes_written
=
772 outfile_stream_up
->Write(data_sp
->GetBytes(), bytes_read
);
773 if (bytes_written
> 0) {
774 result
.GetOutputStream().Printf(
775 "%zi bytes %s to '%s'\n", bytes_written
,
776 append
? "appended" : "written", path
.c_str());
779 result
.AppendErrorWithFormat("Failed to write %" PRIu64
781 (uint64_t)bytes_read
, path
.c_str());
785 // We are going to write ASCII to the file just point the
786 // output_stream to our outfile_stream...
787 output_stream_storage
= std::move(outfile_stream_up
);
788 output_stream_p
= output_stream_storage
.get();
791 result
.AppendErrorWithFormat("Failed to open file '%s' for %s:\n",
792 path
.c_str(), append
? "append" : "write");
794 result
.AppendError(llvm::toString(outfile
.takeError()));
798 output_stream_p
= &result
.GetOutputStream();
801 ExecutionContextScope
*exe_scope
= m_exe_ctx
.GetBestExecutionContextScope();
802 if (compiler_type
.GetOpaqueQualType()) {
803 for (uint32_t i
= 0; i
< item_count
; ++i
) {
804 addr_t item_addr
= addr
+ (i
* item_byte_size
);
805 Address
address(item_addr
);
806 StreamString name_strm
;
807 name_strm
.Printf("0x%" PRIx64
, item_addr
);
808 ValueObjectSP
valobj_sp(ValueObjectMemory::Create(
809 exe_scope
, name_strm
.GetString(), address
, compiler_type
));
811 Format format
= m_format_options
.GetFormat();
812 if (format
!= eFormatDefault
)
813 valobj_sp
->SetFormat(format
);
815 DumpValueObjectOptions
options(m_varobj_options
.GetAsDumpOptions(
816 eLanguageRuntimeDescriptionDisplayVerbosityFull
, format
));
818 if (llvm::Error error
= valobj_sp
->Dump(*output_stream_p
, options
)) {
819 result
.AppendError(toString(std::move(error
)));
823 result
.AppendErrorWithFormat(
824 "failed to create a value object for: (%s) %s\n",
825 view_as_type_cstr
, name_strm
.GetData());
832 result
.SetStatus(eReturnStatusSuccessFinishResult
);
833 DataExtractor
data(data_sp
, target
->GetArchitecture().GetByteOrder(),
834 target
->GetArchitecture().GetAddressByteSize(),
835 target
->GetArchitecture().GetDataByteSize());
837 Format format
= m_format_options
.GetFormat();
838 if (((format
== eFormatChar
) || (format
== eFormatCharPrintable
)) &&
839 (item_byte_size
!= 1)) {
840 // if a count was not passed, or it is 1
841 if (!m_format_options
.GetCountValue().OptionWasSet() || item_count
== 1) {
842 // this turns requests such as
843 // memory read -fc -s10 -c1 *charPtrPtr
844 // which make no sense (what is a char of size 10?) into a request for
845 // fetching 10 chars of size 1 from the same memory location
846 format
= eFormatCharArray
;
847 item_count
= item_byte_size
;
850 // here we passed a count, and it was not 1 so we have a byte_size and
851 // a count we could well multiply those, but instead let's just fail
852 result
.AppendErrorWithFormat(
853 "reading memory as characters of size %" PRIu64
" is not supported",
854 (uint64_t)item_byte_size
);
859 assert(output_stream_p
);
860 size_t bytes_dumped
= DumpDataExtractor(
861 data
, output_stream_p
, 0, format
, item_byte_size
, item_count
,
862 num_per_line
/ target
->GetArchitecture().GetDataByteSize(), addr
, 0, 0,
863 exe_scope
, m_memory_tag_options
.GetShowTags().GetCurrentValue());
864 m_next_addr
= addr
+ bytes_dumped
;
865 output_stream_p
->EOL();
868 OptionGroupOptions m_option_group
;
869 OptionGroupFormat m_format_options
;
870 OptionGroupReadMemory m_memory_options
;
871 OptionGroupOutputFile m_outfile_options
;
872 OptionGroupValueObjectDisplay m_varobj_options
;
873 OptionGroupMemoryTag m_memory_tag_options
;
874 lldb::addr_t m_next_addr
= LLDB_INVALID_ADDRESS
;
875 lldb::addr_t m_prev_byte_size
= 0;
876 OptionGroupFormat m_prev_format_options
;
877 OptionGroupReadMemory m_prev_memory_options
;
878 OptionGroupOutputFile m_prev_outfile_options
;
879 OptionGroupValueObjectDisplay m_prev_varobj_options
;
880 OptionGroupMemoryTag m_prev_memory_tag_options
;
881 CompilerType m_prev_compiler_type
;
884 #define LLDB_OPTIONS_memory_find
885 #include "CommandOptions.inc"
887 // Find the specified data in memory
888 class CommandObjectMemoryFind
: public CommandObjectParsed
{
890 class OptionGroupFindMemory
: public OptionGroup
{
892 OptionGroupFindMemory() : m_count(1), m_offset(0) {}
894 ~OptionGroupFindMemory() override
= default;
896 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
897 return llvm::ArrayRef(g_memory_find_options
);
900 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_value
,
901 ExecutionContext
*execution_context
) override
{
903 const int short_option
= g_memory_find_options
[option_idx
].short_option
;
905 switch (short_option
) {
907 m_expr
.SetValueFromString(option_value
);
911 m_string
.SetValueFromString(option_value
);
915 if (m_count
.SetValueFromString(option_value
).Fail())
916 error
= Status::FromErrorString("unrecognized value for count");
920 if (m_offset
.SetValueFromString(option_value
).Fail())
921 error
= Status::FromErrorString("unrecognized value for dump-offset");
925 llvm_unreachable("Unimplemented option");
930 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
936 OptionValueString m_expr
;
937 OptionValueString m_string
;
938 OptionValueUInt64 m_count
;
939 OptionValueUInt64 m_offset
;
942 CommandObjectMemoryFind(CommandInterpreter
&interpreter
)
943 : CommandObjectParsed(
944 interpreter
, "memory find",
945 "Find a value in the memory of the current target process.",
946 nullptr, eCommandRequiresProcess
| eCommandProcessMustBeLaunched
) {
947 CommandArgumentEntry arg1
;
948 CommandArgumentEntry arg2
;
949 CommandArgumentData addr_arg
;
950 CommandArgumentData value_arg
;
952 // Define the first (and only) variant of this arg.
953 addr_arg
.arg_type
= eArgTypeAddressOrExpression
;
954 addr_arg
.arg_repetition
= eArgRepeatPlain
;
956 // There is only one variant this argument could be; put it into the
958 arg1
.push_back(addr_arg
);
960 // Define the first (and only) variant of this arg.
961 value_arg
.arg_type
= eArgTypeAddressOrExpression
;
962 value_arg
.arg_repetition
= eArgRepeatPlain
;
964 // There is only one variant this argument could be; put it into the
966 arg2
.push_back(value_arg
);
968 // Push the data for the first argument into the m_arguments vector.
969 m_arguments
.push_back(arg1
);
970 m_arguments
.push_back(arg2
);
972 m_option_group
.Append(&m_memory_options
);
973 m_option_group
.Append(&m_memory_tag_options
, LLDB_OPT_SET_ALL
,
975 m_option_group
.Finalize();
978 ~CommandObjectMemoryFind() override
= default;
980 Options
*GetOptions() override
{ return &m_option_group
; }
983 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
984 // No need to check "process" for validity as eCommandRequiresProcess
985 // ensures it is valid
986 Process
*process
= m_exe_ctx
.GetProcessPtr();
988 const size_t argc
= command
.GetArgumentCount();
991 result
.AppendError("two addresses needed for memory find");
996 lldb::addr_t low_addr
= OptionArgParser::ToAddress(
997 &m_exe_ctx
, command
[0].ref(), LLDB_INVALID_ADDRESS
, &error
);
998 if (low_addr
== LLDB_INVALID_ADDRESS
|| error
.Fail()) {
999 result
.AppendError("invalid low address");
1002 lldb::addr_t high_addr
= OptionArgParser::ToAddress(
1003 &m_exe_ctx
, command
[1].ref(), LLDB_INVALID_ADDRESS
, &error
);
1004 if (high_addr
== LLDB_INVALID_ADDRESS
|| error
.Fail()) {
1005 result
.AppendError("invalid high address");
1009 if (high_addr
<= low_addr
) {
1011 "starting address must be smaller than ending address");
1015 lldb::addr_t found_location
= LLDB_INVALID_ADDRESS
;
1017 DataBufferHeap buffer
;
1019 if (m_memory_options
.m_string
.OptionWasSet()) {
1020 llvm::StringRef str
=
1021 m_memory_options
.m_string
.GetValueAs
<llvm::StringRef
>().value_or("");
1023 result
.AppendError("search string must have non-zero length.");
1026 buffer
.CopyData(str
);
1027 } else if (m_memory_options
.m_expr
.OptionWasSet()) {
1028 StackFrame
*frame
= m_exe_ctx
.GetFramePtr();
1029 ValueObjectSP result_sp
;
1030 if ((eExpressionCompleted
==
1031 process
->GetTarget().EvaluateExpression(
1032 m_memory_options
.m_expr
.GetValueAs
<llvm::StringRef
>().value_or(
1034 frame
, result_sp
)) &&
1036 uint64_t value
= result_sp
->GetValueAsUnsigned(0);
1037 std::optional
<uint64_t> size
=
1038 result_sp
->GetCompilerType().GetByteSize(nullptr);
1043 uint8_t byte
= (uint8_t)value
;
1044 buffer
.CopyData(&byte
, 1);
1047 uint16_t word
= (uint16_t)value
;
1048 buffer
.CopyData(&word
, 2);
1051 uint32_t lword
= (uint32_t)value
;
1052 buffer
.CopyData(&lword
, 4);
1055 buffer
.CopyData(&value
, 8);
1061 result
.AppendError("unknown type. pass a string instead");
1065 "result size larger than 8 bytes. pass a string instead");
1070 "expression evaluation failed. pass a string instead");
1075 "please pass either a block of text, or an expression to evaluate.");
1079 size_t count
= m_memory_options
.m_count
.GetCurrentValue();
1080 found_location
= low_addr
;
1081 bool ever_found
= false;
1083 found_location
= process
->FindInMemory(
1084 found_location
, high_addr
, buffer
.GetBytes(), buffer
.GetByteSize());
1085 if (found_location
== LLDB_INVALID_ADDRESS
) {
1087 result
.AppendMessage("data not found within the range.\n");
1088 result
.SetStatus(lldb::eReturnStatusSuccessFinishNoResult
);
1090 result
.AppendMessage("no more matches within the range.\n");
1093 result
.AppendMessageWithFormat("data found at location: 0x%" PRIx64
"\n",
1096 DataBufferHeap
dumpbuffer(32, 0);
1097 process
->ReadMemory(
1098 found_location
+ m_memory_options
.m_offset
.GetCurrentValue(),
1099 dumpbuffer
.GetBytes(), dumpbuffer
.GetByteSize(), error
);
1100 if (!error
.Fail()) {
1101 DataExtractor
data(dumpbuffer
.GetBytes(), dumpbuffer
.GetByteSize(),
1102 process
->GetByteOrder(),
1103 process
->GetAddressByteSize());
1105 data
, &result
.GetOutputStream(), 0, lldb::eFormatBytesWithASCII
, 1,
1106 dumpbuffer
.GetByteSize(), 16,
1107 found_location
+ m_memory_options
.m_offset
.GetCurrentValue(), 0, 0,
1108 m_exe_ctx
.GetBestExecutionContextScope(),
1109 m_memory_tag_options
.GetShowTags().GetCurrentValue());
1110 result
.GetOutputStream().EOL();
1118 result
.SetStatus(lldb::eReturnStatusSuccessFinishResult
);
1121 OptionGroupOptions m_option_group
;
1122 OptionGroupFindMemory m_memory_options
;
1123 OptionGroupMemoryTag m_memory_tag_options
;
1126 #define LLDB_OPTIONS_memory_write
1127 #include "CommandOptions.inc"
1129 // Write memory to the inferior process
1130 class CommandObjectMemoryWrite
: public CommandObjectParsed
{
1132 class OptionGroupWriteMemory
: public OptionGroup
{
1134 OptionGroupWriteMemory() = default;
1136 ~OptionGroupWriteMemory() override
= default;
1138 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
1139 return llvm::ArrayRef(g_memory_write_options
);
1142 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_value
,
1143 ExecutionContext
*execution_context
) override
{
1145 const int short_option
= g_memory_write_options
[option_idx
].short_option
;
1147 switch (short_option
) {
1149 m_infile
.SetFile(option_value
, FileSpec::Style::native
);
1150 FileSystem::Instance().Resolve(m_infile
);
1151 if (!FileSystem::Instance().Exists(m_infile
)) {
1153 error
= Status::FromErrorStringWithFormat(
1154 "input file does not exist: '%s'", option_value
.str().c_str());
1159 if (option_value
.getAsInteger(0, m_infile_offset
)) {
1160 m_infile_offset
= 0;
1161 error
= Status::FromErrorStringWithFormat(
1162 "invalid offset string '%s'", option_value
.str().c_str());
1167 llvm_unreachable("Unimplemented option");
1172 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
1174 m_infile_offset
= 0;
1178 off_t m_infile_offset
;
1181 CommandObjectMemoryWrite(CommandInterpreter
&interpreter
)
1182 : CommandObjectParsed(
1183 interpreter
, "memory write",
1184 "Write to the memory of the current target process.", nullptr,
1185 eCommandRequiresProcess
| eCommandProcessMustBeLaunched
),
1187 eFormatBytes
, 1, UINT64_MAX
,
1190 "The format to use for each of the value to be written."),
1191 std::make_tuple(eArgTypeByteSize
,
1192 "The size in bytes to write from input file or "
1194 CommandArgumentEntry arg1
;
1195 CommandArgumentEntry arg2
;
1196 CommandArgumentData addr_arg
;
1197 CommandArgumentData value_arg
;
1199 // Define the first (and only) variant of this arg.
1200 addr_arg
.arg_type
= eArgTypeAddress
;
1201 addr_arg
.arg_repetition
= eArgRepeatPlain
;
1203 // There is only one variant this argument could be; put it into the
1205 arg1
.push_back(addr_arg
);
1207 // Define the first (and only) variant of this arg.
1208 value_arg
.arg_type
= eArgTypeValue
;
1209 value_arg
.arg_repetition
= eArgRepeatPlus
;
1210 value_arg
.arg_opt_set_association
= LLDB_OPT_SET_1
;
1212 // There is only one variant this argument could be; put it into the
1214 arg2
.push_back(value_arg
);
1216 // Push the data for the first argument into the m_arguments vector.
1217 m_arguments
.push_back(arg1
);
1218 m_arguments
.push_back(arg2
);
1220 m_option_group
.Append(&m_format_options
,
1221 OptionGroupFormat::OPTION_GROUP_FORMAT
,
1223 m_option_group
.Append(&m_format_options
,
1224 OptionGroupFormat::OPTION_GROUP_SIZE
,
1225 LLDB_OPT_SET_1
| LLDB_OPT_SET_2
);
1226 m_option_group
.Append(&m_memory_options
, LLDB_OPT_SET_ALL
, LLDB_OPT_SET_2
);
1227 m_option_group
.Finalize();
1230 ~CommandObjectMemoryWrite() override
= default;
1232 Options
*GetOptions() override
{ return &m_option_group
; }
1235 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
1236 // No need to check "process" for validity as eCommandRequiresProcess
1237 // ensures it is valid
1238 Process
*process
= m_exe_ctx
.GetProcessPtr();
1240 const size_t argc
= command
.GetArgumentCount();
1242 if (m_memory_options
.m_infile
) {
1244 result
.AppendErrorWithFormat(
1245 "%s takes a destination address when writing file contents.\n",
1246 m_cmd_name
.c_str());
1250 result
.AppendErrorWithFormat(
1251 "%s takes only a destination address when writing file contents.\n",
1252 m_cmd_name
.c_str());
1255 } else if (argc
< 2) {
1256 result
.AppendErrorWithFormat(
1257 "%s takes a destination address and at least one value.\n",
1258 m_cmd_name
.c_str());
1262 StreamString
buffer(
1264 process
->GetTarget().GetArchitecture().GetAddressByteSize(),
1265 process
->GetTarget().GetArchitecture().GetByteOrder());
1267 OptionValueUInt64
&byte_size_value
= m_format_options
.GetByteSizeValue();
1268 size_t item_byte_size
= byte_size_value
.GetCurrentValue();
1271 lldb::addr_t addr
= OptionArgParser::ToAddress(
1272 &m_exe_ctx
, command
[0].ref(), LLDB_INVALID_ADDRESS
, &error
);
1274 if (addr
== LLDB_INVALID_ADDRESS
) {
1275 result
.AppendError("invalid address expression\n");
1276 result
.AppendError(error
.AsCString());
1280 if (m_memory_options
.m_infile
) {
1281 size_t length
= SIZE_MAX
;
1282 if (item_byte_size
> 1)
1283 length
= item_byte_size
;
1284 auto data_sp
= FileSystem::Instance().CreateDataBuffer(
1285 m_memory_options
.m_infile
.GetPath(), length
,
1286 m_memory_options
.m_infile_offset
);
1288 length
= data_sp
->GetByteSize();
1291 size_t bytes_written
=
1292 process
->WriteMemory(addr
, data_sp
->GetBytes(), length
, error
);
1294 if (bytes_written
== length
) {
1295 // All bytes written
1296 result
.GetOutputStream().Printf(
1297 "%" PRIu64
" bytes were written to 0x%" PRIx64
"\n",
1298 (uint64_t)bytes_written
, addr
);
1299 result
.SetStatus(eReturnStatusSuccessFinishResult
);
1300 } else if (bytes_written
> 0) {
1301 // Some byte written
1302 result
.GetOutputStream().Printf(
1303 "%" PRIu64
" bytes of %" PRIu64
1304 " requested were written to 0x%" PRIx64
"\n",
1305 (uint64_t)bytes_written
, (uint64_t)length
, addr
);
1306 result
.SetStatus(eReturnStatusSuccessFinishResult
);
1308 result
.AppendErrorWithFormat("Memory write to 0x%" PRIx64
1310 addr
, error
.AsCString());
1314 result
.AppendErrorWithFormat("Unable to read contents of file.\n");
1317 } else if (item_byte_size
== 0) {
1318 if (m_format_options
.GetFormat() == eFormatPointer
)
1319 item_byte_size
= buffer
.GetAddressByteSize();
1324 command
.Shift(); // shift off the address argument
1327 bool success
= false;
1328 for (auto &entry
: command
) {
1329 switch (m_format_options
.GetFormat()) {
1331 case eFormatFloat
: // TODO: add support for floats soon
1332 case eFormatCharPrintable
:
1333 case eFormatBytesWithASCII
:
1334 case eFormatComplex
:
1336 case eFormatUnicode8
:
1337 case eFormatUnicode16
:
1338 case eFormatUnicode32
:
1339 case eFormatVectorOfChar
:
1340 case eFormatVectorOfSInt8
:
1341 case eFormatVectorOfUInt8
:
1342 case eFormatVectorOfSInt16
:
1343 case eFormatVectorOfUInt16
:
1344 case eFormatVectorOfSInt32
:
1345 case eFormatVectorOfUInt32
:
1346 case eFormatVectorOfSInt64
:
1347 case eFormatVectorOfUInt64
:
1348 case eFormatVectorOfFloat16
:
1349 case eFormatVectorOfFloat32
:
1350 case eFormatVectorOfFloat64
:
1351 case eFormatVectorOfUInt128
:
1353 case eFormatComplexInteger
:
1354 case eFormatAddressInfo
:
1355 case eFormatHexFloat
:
1356 case eFormatInstruction
:
1358 result
.AppendError("unsupported format for writing memory");
1361 case eFormatDefault
:
1364 case eFormatHexUppercase
:
1365 case eFormatPointer
: {
1367 // Be careful, getAsInteger with a radix of 16 rejects "0xab" so we
1368 // have to special case that:
1369 bool success
= false;
1370 if (entry
.ref().starts_with("0x"))
1371 success
= !entry
.ref().getAsInteger(0, uval64
);
1373 success
= !entry
.ref().getAsInteger(16, uval64
);
1375 result
.AppendErrorWithFormat(
1376 "'%s' is not a valid hex string value.\n", entry
.c_str());
1378 } else if (!llvm::isUIntN(item_byte_size
* 8, uval64
)) {
1379 result
.AppendErrorWithFormat("Value 0x%" PRIx64
1380 " is too large to fit in a %" PRIu64
1381 " byte unsigned integer value.\n",
1382 uval64
, (uint64_t)item_byte_size
);
1385 buffer
.PutMaxHex64(uval64
, item_byte_size
);
1388 case eFormatBoolean
:
1389 uval64
= OptionArgParser::ToBoolean(entry
.ref(), false, &success
);
1391 result
.AppendErrorWithFormat(
1392 "'%s' is not a valid boolean string value.\n", entry
.c_str());
1395 buffer
.PutMaxHex64(uval64
, item_byte_size
);
1399 if (entry
.ref().getAsInteger(2, uval64
)) {
1400 result
.AppendErrorWithFormat(
1401 "'%s' is not a valid binary string value.\n", entry
.c_str());
1403 } else if (!llvm::isUIntN(item_byte_size
* 8, uval64
)) {
1404 result
.AppendErrorWithFormat("Value 0x%" PRIx64
1405 " is too large to fit in a %" PRIu64
1406 " byte unsigned integer value.\n",
1407 uval64
, (uint64_t)item_byte_size
);
1410 buffer
.PutMaxHex64(uval64
, item_byte_size
);
1413 case eFormatCharArray
:
1415 case eFormatCString
: {
1416 if (entry
.ref().empty())
1419 size_t len
= entry
.ref().size();
1420 // Include the NULL for C strings...
1421 if (m_format_options
.GetFormat() == eFormatCString
)
1424 if (process
->WriteMemory(addr
, entry
.c_str(), len
, error
) == len
) {
1427 result
.AppendErrorWithFormat("Memory write to 0x%" PRIx64
1429 addr
, error
.AsCString());
1434 case eFormatDecimal
:
1435 if (entry
.ref().getAsInteger(0, sval64
)) {
1436 result
.AppendErrorWithFormat(
1437 "'%s' is not a valid signed decimal value.\n", entry
.c_str());
1439 } else if (!llvm::isIntN(item_byte_size
* 8, sval64
)) {
1440 result
.AppendErrorWithFormat(
1441 "Value %" PRIi64
" is too large or small to fit in a %" PRIu64
1442 " byte signed integer value.\n",
1443 sval64
, (uint64_t)item_byte_size
);
1446 buffer
.PutMaxHex64(sval64
, item_byte_size
);
1449 case eFormatUnsigned
:
1451 if (entry
.ref().getAsInteger(0, uval64
)) {
1452 result
.AppendErrorWithFormat(
1453 "'%s' is not a valid unsigned decimal string value.\n",
1456 } else if (!llvm::isUIntN(item_byte_size
* 8, uval64
)) {
1457 result
.AppendErrorWithFormat("Value %" PRIu64
1458 " is too large to fit in a %" PRIu64
1459 " byte unsigned integer value.\n",
1460 uval64
, (uint64_t)item_byte_size
);
1463 buffer
.PutMaxHex64(uval64
, item_byte_size
);
1467 if (entry
.ref().getAsInteger(8, uval64
)) {
1468 result
.AppendErrorWithFormat(
1469 "'%s' is not a valid octal string value.\n", entry
.c_str());
1471 } else if (!llvm::isUIntN(item_byte_size
* 8, uval64
)) {
1472 result
.AppendErrorWithFormat("Value %" PRIo64
1473 " is too large to fit in a %" PRIu64
1474 " byte unsigned integer value.\n",
1475 uval64
, (uint64_t)item_byte_size
);
1478 buffer
.PutMaxHex64(uval64
, item_byte_size
);
1483 if (!buffer
.GetString().empty()) {
1485 const char *buffer_data
= buffer
.GetString().data();
1486 const size_t buffer_size
= buffer
.GetString().size();
1487 const size_t write_size
=
1488 process
->WriteMemory(addr
, buffer_data
, buffer_size
, error
);
1490 if (write_size
!= buffer_size
) {
1491 result
.AppendErrorWithFormat("Memory write to 0x%" PRIx64
1493 addr
, error
.AsCString());
1499 OptionGroupOptions m_option_group
;
1500 OptionGroupFormat m_format_options
;
1501 OptionGroupWriteMemory m_memory_options
;
1504 // Get malloc/free history of a memory address.
1505 class CommandObjectMemoryHistory
: public CommandObjectParsed
{
1507 CommandObjectMemoryHistory(CommandInterpreter
&interpreter
)
1508 : CommandObjectParsed(interpreter
, "memory history",
1509 "Print recorded stack traces for "
1510 "allocation/deallocation events "
1511 "associated with an address.",
1513 eCommandRequiresTarget
| eCommandRequiresProcess
|
1514 eCommandProcessMustBePaused
|
1515 eCommandProcessMustBeLaunched
) {
1516 CommandArgumentEntry arg1
;
1517 CommandArgumentData addr_arg
;
1519 // Define the first (and only) variant of this arg.
1520 addr_arg
.arg_type
= eArgTypeAddress
;
1521 addr_arg
.arg_repetition
= eArgRepeatPlain
;
1523 // There is only one variant this argument could be; put it into the
1525 arg1
.push_back(addr_arg
);
1527 // Push the data for the first argument into the m_arguments vector.
1528 m_arguments
.push_back(arg1
);
1531 ~CommandObjectMemoryHistory() override
= default;
1533 std::optional
<std::string
> GetRepeatCommand(Args
¤t_command_args
,
1534 uint32_t index
) override
{
1539 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
1540 const size_t argc
= command
.GetArgumentCount();
1542 if (argc
== 0 || argc
> 1) {
1543 result
.AppendErrorWithFormat("%s takes an address expression",
1544 m_cmd_name
.c_str());
1549 lldb::addr_t addr
= OptionArgParser::ToAddress(
1550 &m_exe_ctx
, command
[0].ref(), LLDB_INVALID_ADDRESS
, &error
);
1552 if (addr
== LLDB_INVALID_ADDRESS
) {
1553 result
.AppendError("invalid address expression");
1554 result
.AppendError(error
.AsCString());
1558 Stream
*output_stream
= &result
.GetOutputStream();
1560 const ProcessSP
&process_sp
= m_exe_ctx
.GetProcessSP();
1561 const MemoryHistorySP
&memory_history
=
1562 MemoryHistory::FindPlugin(process_sp
);
1564 if (!memory_history
) {
1565 result
.AppendError("no available memory history provider");
1569 HistoryThreads thread_list
= memory_history
->GetHistoryThreads(addr
);
1571 const bool stop_format
= false;
1572 for (auto thread
: thread_list
) {
1573 thread
->GetStatus(*output_stream
, 0, UINT32_MAX
, 0, stop_format
,
1574 /*should_filter*/ false);
1577 result
.SetStatus(eReturnStatusSuccessFinishResult
);
1581 // CommandObjectMemoryRegion
1582 #pragma mark CommandObjectMemoryRegion
1584 #define LLDB_OPTIONS_memory_region
1585 #include "CommandOptions.inc"
1587 class CommandObjectMemoryRegion
: public CommandObjectParsed
{
1589 class OptionGroupMemoryRegion
: public OptionGroup
{
1591 OptionGroupMemoryRegion() : m_all(false, false) {}
1593 ~OptionGroupMemoryRegion() override
= default;
1595 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
1596 return llvm::ArrayRef(g_memory_region_options
);
1599 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_value
,
1600 ExecutionContext
*execution_context
) override
{
1602 const int short_option
= g_memory_region_options
[option_idx
].short_option
;
1604 switch (short_option
) {
1606 m_all
.SetCurrentValue(true);
1607 m_all
.SetOptionWasSet();
1610 llvm_unreachable("Unimplemented option");
1616 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
1620 OptionValueBoolean m_all
;
1623 CommandObjectMemoryRegion(CommandInterpreter
&interpreter
)
1624 : CommandObjectParsed(interpreter
, "memory region",
1625 "Get information on the memory region containing "
1626 "an address in the current target process.",
1627 "memory region <address-expression> (or --all)",
1628 eCommandRequiresProcess
| eCommandTryTargetAPILock
|
1629 eCommandProcessMustBeLaunched
) {
1630 // Address in option set 1.
1631 m_arguments
.push_back(CommandArgumentEntry
{CommandArgumentData(
1632 eArgTypeAddressOrExpression
, eArgRepeatPlain
, LLDB_OPT_SET_1
)});
1633 // "--all" will go in option set 2.
1634 m_option_group
.Append(&m_memory_region_options
);
1635 m_option_group
.Finalize();
1638 ~CommandObjectMemoryRegion() override
= default;
1640 Options
*GetOptions() override
{ return &m_option_group
; }
1643 void DumpRegion(CommandReturnObject
&result
, Target
&target
,
1644 const MemoryRegionInfo
&range_info
, lldb::addr_t load_addr
) {
1645 lldb_private::Address addr
;
1646 ConstString section_name
;
1647 if (target
.ResolveLoadAddress(load_addr
, addr
)) {
1648 SectionSP
section_sp(addr
.GetSection());
1650 // Got the top most section, not the deepest section
1651 while (section_sp
->GetParent())
1652 section_sp
= section_sp
->GetParent();
1653 section_name
= section_sp
->GetName();
1657 ConstString name
= range_info
.GetName();
1658 result
.AppendMessageWithFormatv(
1659 "[{0:x16}-{1:x16}) {2:r}{3:w}{4:x}{5}{6}{7}{8}",
1660 range_info
.GetRange().GetRangeBase(),
1661 range_info
.GetRange().GetRangeEnd(), range_info
.GetReadable(),
1662 range_info
.GetWritable(), range_info
.GetExecutable(), name
? " " : "",
1663 name
, section_name
? " " : "", section_name
);
1664 MemoryRegionInfo::OptionalBool memory_tagged
= range_info
.GetMemoryTagged();
1665 if (memory_tagged
== MemoryRegionInfo::OptionalBool::eYes
)
1666 result
.AppendMessage("memory tagging: enabled");
1668 const std::optional
<std::vector
<addr_t
>> &dirty_page_list
=
1669 range_info
.GetDirtyPageList();
1670 if (dirty_page_list
) {
1671 const size_t page_count
= dirty_page_list
->size();
1672 result
.AppendMessageWithFormat(
1673 "Modified memory (dirty) page list provided, %zu entries.\n",
1675 if (page_count
> 0) {
1676 bool print_comma
= false;
1677 result
.AppendMessageWithFormat("Dirty pages: ");
1678 for (size_t i
= 0; i
< page_count
; i
++) {
1680 result
.AppendMessageWithFormat(", ");
1683 result
.AppendMessageWithFormat("0x%" PRIx64
, (*dirty_page_list
)[i
]);
1685 result
.AppendMessageWithFormat(".\n");
1690 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
1691 ProcessSP process_sp
= m_exe_ctx
.GetProcessSP();
1693 m_prev_end_addr
= LLDB_INVALID_ADDRESS
;
1694 result
.AppendError("invalid process");
1699 lldb::addr_t load_addr
= m_prev_end_addr
;
1700 m_prev_end_addr
= LLDB_INVALID_ADDRESS
;
1702 const size_t argc
= command
.GetArgumentCount();
1703 const lldb::ABISP
&abi
= process_sp
->GetABI();
1706 if (m_memory_region_options
.m_all
) {
1708 "The \"--all\" option cannot be used when an address "
1709 "argument is given");
1713 auto load_addr_str
= command
[0].ref();
1714 load_addr
= OptionArgParser::ToAddress(&m_exe_ctx
, load_addr_str
,
1715 LLDB_INVALID_ADDRESS
, &error
);
1716 if (error
.Fail() || load_addr
== LLDB_INVALID_ADDRESS
) {
1717 result
.AppendErrorWithFormat("invalid address argument \"%s\": %s\n",
1718 command
[0].c_str(), error
.AsCString());
1721 } else if (argc
> 1 ||
1722 // When we're repeating the command, the previous end address is
1723 // used for load_addr. If that was 0xF...F then we must have
1724 // reached the end of memory.
1725 (argc
== 0 && !m_memory_region_options
.m_all
&&
1726 load_addr
== LLDB_INVALID_ADDRESS
) ||
1727 // If the target has non-address bits (tags, limited virtual
1728 // address size, etc.), the end of mappable memory will be lower
1729 // than that. So if we find any non-address bit set, we must be
1730 // at the end of the mappable range.
1731 (abi
&& (abi
->FixAnyAddress(load_addr
) != load_addr
))) {
1732 result
.AppendErrorWithFormat(
1733 "'%s' takes one argument or \"--all\" option:\nUsage: %s\n",
1734 m_cmd_name
.c_str(), m_cmd_syntax
.c_str());
1738 // It is important that we track the address used to request the region as
1739 // this will give the correct section name in the case that regions overlap.
1740 // On Windows we get mutliple regions that start at the same place but are
1741 // different sizes and refer to different sections.
1742 std::vector
<std::pair
<lldb_private::MemoryRegionInfo
, lldb::addr_t
>>
1744 if (m_memory_region_options
.m_all
) {
1745 // We don't use GetMemoryRegions here because it doesn't include unmapped
1746 // areas like repeating the command would. So instead, emulate doing that.
1747 lldb::addr_t addr
= 0;
1748 while (error
.Success() && addr
!= LLDB_INVALID_ADDRESS
&&
1749 // When there are non-address bits the last range will not extend
1750 // to LLDB_INVALID_ADDRESS but to the max virtual address.
1751 // This prevents us looping forever if that is the case.
1752 (!abi
|| (abi
->FixAnyAddress(addr
) == addr
))) {
1753 lldb_private::MemoryRegionInfo region_info
;
1754 error
= process_sp
->GetMemoryRegionInfo(addr
, region_info
);
1756 if (error
.Success()) {
1757 region_list
.push_back({region_info
, addr
});
1758 addr
= region_info
.GetRange().GetRangeEnd();
1762 lldb_private::MemoryRegionInfo region_info
;
1763 error
= process_sp
->GetMemoryRegionInfo(load_addr
, region_info
);
1764 if (error
.Success())
1765 region_list
.push_back({region_info
, load_addr
});
1768 if (error
.Success()) {
1769 for (std::pair
<MemoryRegionInfo
, addr_t
> &range
: region_list
) {
1770 DumpRegion(result
, process_sp
->GetTarget(), range
.first
, range
.second
);
1771 m_prev_end_addr
= range
.first
.GetRange().GetRangeEnd();
1774 result
.SetStatus(eReturnStatusSuccessFinishResult
);
1778 result
.AppendErrorWithFormat("%s\n", error
.AsCString());
1781 std::optional
<std::string
> GetRepeatCommand(Args
¤t_command_args
,
1782 uint32_t index
) override
{
1783 // If we repeat this command, repeat it without any arguments so we can
1784 // show the next memory range
1788 lldb::addr_t m_prev_end_addr
= LLDB_INVALID_ADDRESS
;
1790 OptionGroupOptions m_option_group
;
1791 OptionGroupMemoryRegion m_memory_region_options
;
1794 // CommandObjectMemory
1796 CommandObjectMemory::CommandObjectMemory(CommandInterpreter
&interpreter
)
1797 : CommandObjectMultiword(
1798 interpreter
, "memory",
1799 "Commands for operating on memory in the current target process.",
1800 "memory <subcommand> [<subcommand-options>]") {
1801 LoadSubCommand("find",
1802 CommandObjectSP(new CommandObjectMemoryFind(interpreter
)));
1803 LoadSubCommand("read",
1804 CommandObjectSP(new CommandObjectMemoryRead(interpreter
)));
1805 LoadSubCommand("write",
1806 CommandObjectSP(new CommandObjectMemoryWrite(interpreter
)));
1807 LoadSubCommand("history",
1808 CommandObjectSP(new CommandObjectMemoryHistory(interpreter
)));
1809 LoadSubCommand("region",
1810 CommandObjectSP(new CommandObjectMemoryRegion(interpreter
)));
1811 LoadSubCommand("tag",
1812 CommandObjectSP(new CommandObjectMemoryTag(interpreter
)));
1815 CommandObjectMemory::~CommandObjectMemory() = default;