Skip to content

Commit

Permalink
findrefs: refactor ::PrintRefs template strings (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus Marchini authored and joyeecheung committed Mar 29, 2018
1 parent cec8a7a commit a36f799
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
44 changes: 27 additions & 17 deletions src/llscan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ using lldb::SBValue;
using lldb::eReturnStatusFailed;
using lldb::eReturnStatusSuccessFinishResult;

const char* const
FindReferencesCmd::ObjectScanner::property_reference_template =
"0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n";
const char* const FindReferencesCmd::ObjectScanner::array_reference_template =
"0x%" PRIx64 ": %s[%" PRId64 "]=0x%" PRIx64 "\n";


const char* const
FindReferencesCmd::StringScanner::property_reference_template =
"0x%" PRIx64 ": %s.%s=0x%" PRIx64 " '%s'\n";
const char* const FindReferencesCmd::StringScanner::array_reference_template =
"0x%" PRIx64 ": %s[%" PRId64 "]=0x%" PRIx64 " '%s'\n";

bool FindObjectsCmd::DoExecute(SBDebugger d, char** cmd,
SBCommandReturnObject& result) {
SBTarget target = d.GetSelectedTarget();
Expand Down Expand Up @@ -611,8 +624,8 @@ void FindReferencesCmd::ReferenceScanner::PrintRefs(
if (v.raw() != search_value_.raw()) continue;

std::string type_name = js_obj.GetTypeName(err);
result.Printf("0x%" PRIx64 ": %s[%" PRId64 "]=0x%" PRIx64 "\n",
js_obj.raw(), type_name.c_str(), i, search_value_.raw());
result.Printf(array_reference_template, js_obj.raw(), type_name.c_str(), i,
search_value_.raw());
}

// Walk all the properties in this object.
Expand All @@ -627,7 +640,7 @@ void FindReferencesCmd::ReferenceScanner::PrintRefs(
if (v.raw() == search_value_.raw()) {
std::string key = entry.first.ToString(err);
std::string type_name = js_obj.GetTypeName(err);
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", js_obj.raw(),
result.Printf(property_reference_template, js_obj.raw(),
type_name.c_str(), key.c_str(), search_value_.raw());
}
}
Expand All @@ -642,38 +655,37 @@ void FindReferencesCmd::ReferenceScanner::PrintRefs(

// Concatenated and sliced strings refer to other strings so
// we need to check their references.

if (repr == v8->string()->kSlicedStringTag) {
v8::SlicedString sliced_str(str);
v8::String parent = sliced_str.Parent(err);
if (err.Success() && parent.raw() == search_value_.raw()) {
std::string type_name = sliced_str.GetTypeName(err);
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", str.raw(),
type_name.c_str(), "<Parent>", search_value_.raw());
result.Printf(property_reference_template, str.raw(), type_name.c_str(),
"<Parent>", search_value_.raw());
}
} else if (repr == v8->string()->kConsStringTag) {
v8::ConsString cons_str(str);

v8::String first = cons_str.First(err);
if (err.Success() && first.raw() == search_value_.raw()) {
std::string type_name = cons_str.GetTypeName(err);
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", str.raw(),
type_name.c_str(), "<First>", search_value_.raw());
result.Printf(property_reference_template, str.raw(), type_name.c_str(),
"<First>", search_value_.raw());
}

v8::String second = cons_str.Second(err);
if (err.Success() && second.raw() == search_value_.raw()) {
std::string type_name = cons_str.GetTypeName(err);
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", str.raw(),
type_name.c_str(), "<Second>", search_value_.raw());
result.Printf(property_reference_template, str.raw(), type_name.c_str(),
"<Second>", search_value_.raw());
}
} else if (repr == v8->string()->kThinStringTag) {
v8::ThinString thin_str(str);
v8::String actual = thin_str.Actual(err);
if (err.Success() && actual.raw() == search_value_.raw()) {
std::string type_name = thin_str.GetTypeName(err);
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", str.raw(),
type_name.c_str(), "<Actual>", search_value_.raw());
result.Printf(property_reference_template, str.raw(), type_name.c_str(),
"<Actual>", search_value_.raw());
}
}
// Nothing to do for other kinds of string.
Expand Down Expand Up @@ -794,7 +806,7 @@ void FindReferencesCmd::PropertyScanner::PrintRefs(
}
if (key == search_value_) {
std::string type_name = js_obj.GetTypeName(err);
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", js_obj.raw(),
result.Printf(property_reference_template, js_obj.raw(),
type_name.c_str(), key.c_str(), entry.second.raw());
}
}
Expand Down Expand Up @@ -1280,8 +1292,7 @@ bool LLScan::ScanHeapForObjects(lldb::SBTarget target,
return true;
}

std::string
FindJSObjectsVisitor::MapCacheEntry::GetTypeNameWithProperties(
std::string FindJSObjectsVisitor::MapCacheEntry::GetTypeNameWithProperties(
ShowArrayLength show_array_length, size_t max_properties) {
std::string type_name_with_properties(type_name);

Expand All @@ -1306,8 +1317,7 @@ FindJSObjectsVisitor::MapCacheEntry::GetTypeNameWithProperties(

bool FindJSObjectsVisitor::MapCacheEntry::Load(v8::Map map,
v8::HeapObject heap_object,
v8::LLV8* llv8,
v8::Error& err) {
v8::LLV8* llv8, v8::Error& err) {
// Check type first
is_histogram = FindJSObjectsVisitor::IsAHistogramType(map, err);

Expand Down
10 changes: 8 additions & 2 deletions src/llscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class FindReferencesCmd : public CommandBase {
v8::JSObject& js_obj, v8::Error& err) {}
virtual void PrintRefs(lldb::SBCommandReturnObject& result, v8::String& str,
v8::Error& err) {}

static const char* const property_reference_template;
static const char* const array_reference_template;
};

void PrintReferences(lldb::SBCommandReturnObject& result,
Expand Down Expand Up @@ -153,6 +156,9 @@ class FindReferencesCmd : public CommandBase {
void PrintRefs(lldb::SBCommandReturnObject& result, v8::String& str,
v8::Error& err) override;

static const char* const property_reference_template;
static const char* const array_reference_template;

private:
LLScan* llscan_;
std::string search_value_;
Expand Down Expand Up @@ -257,8 +263,8 @@ class FindJSObjectsVisitor : MemoryVisitor {
ShowArrayLength show_array_length = kShowArrayLength,
size_t max_properties = 0);

bool Load(v8::Map map, v8::HeapObject heap_object,
v8::LLV8* llv8, v8::Error& err);
bool Load(v8::Map map, v8::HeapObject heap_object, v8::LLV8* llv8,
v8::Error& err);
};

static bool IsAHistogramType(v8::Map& map, v8::Error& err);
Expand Down
4 changes: 2 additions & 2 deletions src/llv8-constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ void Module::Assign(SBTarget target, Common* common) {


template <typename T>
T ReadSymbolFromTarget(SBTarget& target, SBAddress& start,
const char* name, Error& err) {
T ReadSymbolFromTarget(SBTarget& target, SBAddress& start, const char* name,
Error& err) {
SBError sberr;
T res = 0;
target.ReadMemory(start, &res, sizeof(T), sberr);
Expand Down

0 comments on commit a36f799

Please sign in to comment.