xref: /haiku/src/apps/debugger/user_interface/cli/commands/CliDumpStringCommand.cpp (revision 445d4fd926c569e7b9ae28017da86280aaecbae2)
1 /*
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.
6  *
7  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
8  * Distributed under the terms of the NewOS License.
9  */
10 
11 
12 #include "CliDumpStringCommand.h"
13 
14 #include <ctype.h>
15 #include <stdio.h>
16 
17 #include <AutoLocker.h>
18 
19 #include "CliContext.h"
20 #include "CppLanguage.h"
21 #include "Team.h"
22 #include "TeamMemoryBlock.h"
23 #include "UiUtils.h"
24 #include "UserInterface.h"
25 #include "Value.h"
26 #include "Variable.h"
27 
28 
29 CliDumpStringCommand::CliDumpStringCommand()
30 	:
31 	CliCommand("dump contents of a string in the debugged team's memory",
32 			"%s [\"]address|expression[\"]\n"
33 			"Reads and displays the contents of a null-terminated string at the target address.")
34 {
35 	// TODO: this should be retrieved via some indirect helper rather
36 	// than instantiating the specific language directly.
37 	fLanguage = new(std::nothrow) CppLanguage();
38 }
39 
40 
41 CliDumpStringCommand::~CliDumpStringCommand()
42 {
43 	if (fLanguage != NULL)
44 		fLanguage->ReleaseReference();
45 }
46 
47 
48 void
49 CliDumpStringCommand::Execute(int argc, const char* const* argv,
50 	CliContext& context)
51 {
52 	if (argc < 2) {
53 		PrintUsage(argv[0]);
54 		return;
55 	}
56 
57 	if (fLanguage == NULL) {
58 		printf("Unable to evaluate expression: %s\n", strerror(B_NO_MEMORY));
59 		return;
60 	}
61 
62 	target_addr_t address;
63 	if (context.EvaluateExpression(argv[1], fLanguage, address) != B_OK)
64 		return;
65 
66 	TeamMemoryBlock* block = NULL;
67 	if (context.GetMemoryBlock(address, block) != B_OK)
68 		return;
69 
70 	printf("%p \"", (char*)address);
71 
72 	target_addr_t offset = address;
73 	char c;
74 	while (block->Contains(offset)) {
75 		c = *(block->Data() + offset - block->BaseAddress());
76 
77 		if (c == '\0')
78 			break;
79 		if (c == '\n')
80 			printf("\\n");
81 		else if (c == '\t')
82 			printf("\\t");
83 		else {
84 			if (!isprint(c))
85 				c = '.';
86 
87 			printf("%c", c);
88 		}
89 		++offset;
90 	}
91 
92 	printf("\"\n");
93 }
94