Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminal-safe redexdump output #163

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ AM_CPPFLAGS = \
-I$(top_srcdir)/opt/synth \
-I$(top_srcdir)/opt/unterface \
-I$(top_srcdir)/util \
-I$(top_srcdir)/tools/common \
-I$(top_srcdir)/tools/redexdump \
-I/usr/include/jsoncpp

#
Expand Down Expand Up @@ -127,14 +129,23 @@ libredex_la_LIBADD = \
#
# redex-all: the main executable
#
bin_PROGRAMS = redex-all
bin_PROGRAMS = redex-all redexdump

redex_all_SOURCES = \
tools/redex-all/main.cpp \
tools/redex-all/Passes.cpp

redex_all_LDADD = libredex.la

redexdump_SOURCES = \
tools/redexdump/DumpTables.cpp \
tools/redexdump/PrintUtil.cpp \
tools/redexdump/RedexDump.cpp \
tools/common/DexCommon.cpp \
tools/common/Formatters.cpp

redexdump_LDADD = libredex.la

#
# redex: Python driver script
#
Expand Down
33 changes: 30 additions & 3 deletions tools/redexdump/DumpTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include "DexDebugInstruction.h"
#include "Formatters.h"
#include "PrintUtil.h"
#include "Unicode.h"
#include <RedexDump.h>
#include <sstream>
#include <vector>
#include <string.h>
#include <string>

Expand Down Expand Up @@ -432,14 +434,39 @@ static std::string get_debug_item(const uint8_t** pdebug_item) {
}

// Dump a string_data_item (i.e., an entry in the string data
// section), advancing POS_INOUT over the item. BUG: we dump the
// MUTF8 string as if it were standard UTF8. Shame on us.
// section), advancing POS_INOUT over the item.
static const char string_data_header[] = "u16len [contents]";
static void dump_string_data_item(const uint8_t** pos_inout) {
const uint8_t* pos = *pos_inout;
uint32_t utf16_code_point_count = read_uleb128(&pos); // Not byte count!
size_t utf8_length = strlen((char*) pos);
redump("%03u [%s]\n", (unsigned) utf16_code_point_count, pos);
std::string cleansed_data;
const char* string_to_print;
if (raw) { // Output whatever bytes we have
string_to_print = (char*) pos;
} else { // Translate to UTF-8; strip control characters
std::vector<char32_t> code_points;
const char* enc_pos = (char*) pos;
uint32_t cp;
while ((cp = mutf8_next_code_point(enc_pos))) {
if (cp < ' ' || cp == 255 /* DEL */) {
cp = '.';
}
code_points.push_back(cp);
}
ssize_t nr_utf8_bytes = utf32_to_utf8_length(
&code_points[0], code_points.size());
if (nr_utf8_bytes < 0 && utf8_length == 0) {
cleansed_data = "";
} else if (nr_utf8_bytes < 0) {
cleansed_data = "{invalid encoding?}";
} else {
cleansed_data.resize(nr_utf8_bytes);
utf32_to_utf8(&code_points[0], code_points.size(), &cleansed_data[0]);
}
string_to_print = cleansed_data.c_str();
}
redump("%03u [%s]\n", (unsigned) utf16_code_point_count, string_to_print);
*pos_inout = pos + utf8_length + 1;
}

Expand Down
1 change: 1 addition & 0 deletions tools/redexdump/PrintUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cstdarg>

bool clean = false;
bool raw = false;

void redump(const char* format, ...) {
va_list va;
Expand Down
1 change: 1 addition & 0 deletions tools/redexdump/PrintUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdint.h>

extern bool clean;
extern bool raw;

void redump(const char* format, ...);
void redump(uint32_t off, const char* format, ...);
Expand Down
2 changes: 2 additions & 0 deletions tools/redexdump/RedexDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static const char ddump_usage_string[] =
"-D, --ddebug=<addr>: disassemble debug info item at <addr>\n"
"\nprinting options:\n"
"--clean: does not print indexes or offsets making the output "
"--raw: print all bytes, even control characters "
"usable for a diff\n";

int main(int argc, char* argv[]) {
Expand Down Expand Up @@ -82,6 +83,7 @@ int main(int argc, char* argv[]) {
{ "debug", no_argument, nullptr, 'd' },
{ "ddebug", required_argument, nullptr, 'D' },
{ "clean", no_argument, (int*)&clean, 1 },
{ "raw", no_argument, (int*)&raw, 1 },
{ "no-dump-map", no_argument, &no_dump_map, 1 },
{ "help", no_argument, nullptr, 'h' },
{ nullptr, 0, nullptr, 0 },
Expand Down