2 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2002-2010, Axel Dörfler, axeld@pinc-software.de.
4 * Copyright 2012-2016, Rene Gollent, rene@gollent.com.
5 * Distributed under the terms of the MIT License.
7 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
8 * Distributed under the terms of the NewOS License.
12 #include "CliDumpMemoryCommand.h"
17 #include <AutoLocker.h>
19 #include "CliContext.h"
20 #include "CppLanguage.h"
22 #include "TeamMemoryBlock.h"
24 #include "UserInterface.h"
29 CliDumpMemoryCommand::CliDumpMemoryCommand()
31 CliCommand("dump contents of debugged team's memory",
32 "%s [\"]address|expression[\"] [num]\n"
33 "Reads and displays the contents of memory at the target address.")
35 // TODO: this should be retrieved via some indirect helper rather
36 // than instantiating the specific language directly.
37 fLanguage
= new(std::nothrow
) CppLanguage();
41 CliDumpMemoryCommand::~CliDumpMemoryCommand()
43 if (fLanguage
!= NULL
)
44 fLanguage
->ReleaseReference();
49 CliDumpMemoryCommand::Execute(int argc
, const char* const* argv
,
57 if (fLanguage
== NULL
) {
58 printf("Unable to evaluate expression: %s\n", strerror(B_NO_MEMORY
));
62 ExpressionInfo
* info
= context
.GetExpressionInfo();
64 target_addr_t address
= 0;
67 context
.GetUserInterfaceListener()->ExpressionEvaluationRequested(
69 context
.WaitForEvents(CliContext::EVENT_EXPRESSION_EVALUATED
);
70 if (context
.IsTerminating())
74 ExpressionResult
* result
= context
.GetExpressionValue();
76 if (result
->Kind() == EXPRESSION_RESULT_KIND_PRIMITIVE
) {
77 Value
* value
= result
->PrimitiveValue();
78 BVariant variantValue
;
79 value
->ToVariant(variantValue
);
80 if (variantValue
.Type() == B_STRING_TYPE
)
81 errorMessage
.SetTo(variantValue
.ToString());
83 address
= variantValue
.ToUInt64();
86 errorMessage
= strerror(context
.GetExpressionResult());
88 if (!errorMessage
.IsEmpty()) {
89 printf("Unable to evaluate expression: %s\n",
90 errorMessage
.String());
95 int32 displayWidth
= 0;
97 // build the format string
98 if (strcmp(argv
[0], "db") == 0) {
101 } else if (strcmp(argv
[0], "ds") == 0) {
104 } else if (strcmp(argv
[0], "dw") == 0) {
107 } else if (strcmp(argv
[0], "dl") == 0) {
110 } else if (strcmp(argv
[0], "string") == 0) {
114 printf("dump called in an invalid way!\n");
121 num
= strtol(argv
[2], &remainder
, 0);
122 if (*remainder
!= '\0') {
123 printf("Error: invalid parameter \"%s\"\n", argv
[2]);
130 TeamMemoryBlock
* block
= context
.CurrentBlock();
131 if (block
== NULL
|| !block
->Contains(address
)) {
132 context
.GetUserInterfaceListener()->InspectRequested(address
,
134 context
.WaitForEvents(CliContext::EVENT_TEAM_MEMORY_BLOCK_RETRIEVED
);
135 if (context
.IsTerminating())
137 block
= context
.CurrentBlock();
140 if (!strcmp(argv
[0], "string")) {
141 printf("%p \"", (char*)address
);
143 target_addr_t offset
= address
;
145 while (block
->Contains(offset
)) {
146 c
= *(block
->Data() + offset
- block
->BaseAddress());
166 UiUtils::DumpMemory(output
, 0, block
, address
, itemSize
, displayWidth
,
168 printf("%s\n", output
.String());