Skip to content

Commit a36f799

Browse files
Matheus Marchinijoyeecheung
Matheus Marchini
authored andcommitted
findrefs: refactor ::PrintRefs template strings (#178)
PR-URL: nodejs/node#178
1 parent cec8a7a commit a36f799

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

src/llscan.cc

+27-17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ using lldb::SBValue;
2727
using lldb::eReturnStatusFailed;
2828
using lldb::eReturnStatusSuccessFinishResult;
2929

30+
const char* const
31+
FindReferencesCmd::ObjectScanner::property_reference_template =
32+
"0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n";
33+
const char* const FindReferencesCmd::ObjectScanner::array_reference_template =
34+
"0x%" PRIx64 ": %s[%" PRId64 "]=0x%" PRIx64 "\n";
35+
36+
37+
const char* const
38+
FindReferencesCmd::StringScanner::property_reference_template =
39+
"0x%" PRIx64 ": %s.%s=0x%" PRIx64 " '%s'\n";
40+
const char* const FindReferencesCmd::StringScanner::array_reference_template =
41+
"0x%" PRIx64 ": %s[%" PRId64 "]=0x%" PRIx64 " '%s'\n";
42+
3043
bool FindObjectsCmd::DoExecute(SBDebugger d, char** cmd,
3144
SBCommandReturnObject& result) {
3245
SBTarget target = d.GetSelectedTarget();
@@ -611,8 +624,8 @@ void FindReferencesCmd::ReferenceScanner::PrintRefs(
611624
if (v.raw() != search_value_.raw()) continue;
612625

613626
std::string type_name = js_obj.GetTypeName(err);
614-
result.Printf("0x%" PRIx64 ": %s[%" PRId64 "]=0x%" PRIx64 "\n",
615-
js_obj.raw(), type_name.c_str(), i, search_value_.raw());
627+
result.Printf(array_reference_template, js_obj.raw(), type_name.c_str(), i,
628+
search_value_.raw());
616629
}
617630

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

643656
// Concatenated and sliced strings refer to other strings so
644657
// we need to check their references.
645-
646658
if (repr == v8->string()->kSlicedStringTag) {
647659
v8::SlicedString sliced_str(str);
648660
v8::String parent = sliced_str.Parent(err);
649661
if (err.Success() && parent.raw() == search_value_.raw()) {
650662
std::string type_name = sliced_str.GetTypeName(err);
651-
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", str.raw(),
652-
type_name.c_str(), "<Parent>", search_value_.raw());
663+
result.Printf(property_reference_template, str.raw(), type_name.c_str(),
664+
"<Parent>", search_value_.raw());
653665
}
654666
} else if (repr == v8->string()->kConsStringTag) {
655667
v8::ConsString cons_str(str);
656668

657669
v8::String first = cons_str.First(err);
658670
if (err.Success() && first.raw() == search_value_.raw()) {
659671
std::string type_name = cons_str.GetTypeName(err);
660-
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", str.raw(),
661-
type_name.c_str(), "<First>", search_value_.raw());
672+
result.Printf(property_reference_template, str.raw(), type_name.c_str(),
673+
"<First>", search_value_.raw());
662674
}
663675

664676
v8::String second = cons_str.Second(err);
665677
if (err.Success() && second.raw() == search_value_.raw()) {
666678
std::string type_name = cons_str.GetTypeName(err);
667-
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", str.raw(),
668-
type_name.c_str(), "<Second>", search_value_.raw());
679+
result.Printf(property_reference_template, str.raw(), type_name.c_str(),
680+
"<Second>", search_value_.raw());
669681
}
670682
} else if (repr == v8->string()->kThinStringTag) {
671683
v8::ThinString thin_str(str);
672684
v8::String actual = thin_str.Actual(err);
673685
if (err.Success() && actual.raw() == search_value_.raw()) {
674686
std::string type_name = thin_str.GetTypeName(err);
675-
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", str.raw(),
676-
type_name.c_str(), "<Actual>", search_value_.raw());
687+
result.Printf(property_reference_template, str.raw(), type_name.c_str(),
688+
"<Actual>", search_value_.raw());
677689
}
678690
}
679691
// Nothing to do for other kinds of string.
@@ -794,7 +806,7 @@ void FindReferencesCmd::PropertyScanner::PrintRefs(
794806
}
795807
if (key == search_value_) {
796808
std::string type_name = js_obj.GetTypeName(err);
797-
result.Printf("0x%" PRIx64 ": %s.%s=0x%" PRIx64 "\n", js_obj.raw(),
809+
result.Printf(property_reference_template, js_obj.raw(),
798810
type_name.c_str(), key.c_str(), entry.second.raw());
799811
}
800812
}
@@ -1280,8 +1292,7 @@ bool LLScan::ScanHeapForObjects(lldb::SBTarget target,
12801292
return true;
12811293
}
12821294

1283-
std::string
1284-
FindJSObjectsVisitor::MapCacheEntry::GetTypeNameWithProperties(
1295+
std::string FindJSObjectsVisitor::MapCacheEntry::GetTypeNameWithProperties(
12851296
ShowArrayLength show_array_length, size_t max_properties) {
12861297
std::string type_name_with_properties(type_name);
12871298

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

13071318
bool FindJSObjectsVisitor::MapCacheEntry::Load(v8::Map map,
13081319
v8::HeapObject heap_object,
1309-
v8::LLV8* llv8,
1310-
v8::Error& err) {
1320+
v8::LLV8* llv8, v8::Error& err) {
13111321
// Check type first
13121322
is_histogram = FindJSObjectsVisitor::IsAHistogramType(map, err);
13131323

src/llscan.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class FindReferencesCmd : public CommandBase {
8585
v8::JSObject& js_obj, v8::Error& err) {}
8686
virtual void PrintRefs(lldb::SBCommandReturnObject& result, v8::String& str,
8787
v8::Error& err) {}
88+
89+
static const char* const property_reference_template;
90+
static const char* const array_reference_template;
8891
};
8992

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

159+
static const char* const property_reference_template;
160+
static const char* const array_reference_template;
161+
156162
private:
157163
LLScan* llscan_;
158164
std::string search_value_;
@@ -257,8 +263,8 @@ class FindJSObjectsVisitor : MemoryVisitor {
257263
ShowArrayLength show_array_length = kShowArrayLength,
258264
size_t max_properties = 0);
259265

260-
bool Load(v8::Map map, v8::HeapObject heap_object,
261-
v8::LLV8* llv8, v8::Error& err);
266+
bool Load(v8::Map map, v8::HeapObject heap_object, v8::LLV8* llv8,
267+
v8::Error& err);
262268
};
263269

264270
static bool IsAHistogramType(v8::Map& map, v8::Error& err);

src/llv8-constants.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ void Module::Assign(SBTarget target, Common* common) {
3232

3333

3434
template <typename T>
35-
T ReadSymbolFromTarget(SBTarget& target, SBAddress& start,
36-
const char* name, Error& err) {
35+
T ReadSymbolFromTarget(SBTarget& target, SBAddress& start, const char* name,
36+
Error& err) {
3737
SBError sberr;
3838
T res = 0;
3939
target.ReadMemory(start, &res, sizeof(T), sberr);

0 commit comments

Comments
 (0)