Skip to content

Commit 6a44355

Browse files
committed
fixup! src: use new SystemPointerSize constant
1 parent d819d66 commit 6a44355

File tree

2 files changed

+66
-42
lines changed

2 files changed

+66
-42
lines changed

src/constants.cc

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,19 @@ T ReadSymbolFromTarget(SBTarget& target, SBAddress& start, const char* name,
2323
return res;
2424
}
2525

26-
std::pair<int64_t, bool> Constants::LookupConstant(SBTarget target,
27-
const char* name,
28-
int64_t def) {
29-
int64_t res = 0;
30-
res = def;
26+
Constant<int64_t> Constants::LookupConstant(SBTarget target, const char* name) {
27+
int64_t res;
3128

3229
SBSymbolContextList context_list = target.FindSymbols(name);
3330

3431
if (!context_list.IsValid() || context_list.GetSize() == 0) {
35-
return {res, false};
32+
return Constant<int64_t>();
3633
}
3734

3835
SBSymbolContext context = context_list.GetContextAtIndex(0);
3936
SBSymbol symbol = context.GetSymbol();
4037
if (!symbol.IsValid()) {
41-
return {res, false};
38+
return Constant<int64_t>();
4239
}
4340

4441
SBAddress start = symbol.GetStartAddress();
@@ -59,10 +56,12 @@ std::pair<int64_t, bool> Constants::LookupConstant(SBTarget target,
5956
int8_t tmp = ReadSymbolFromTarget<int8_t>(target, start, name, sberr);
6057
res = static_cast<int64_t>(tmp);
6158
} else {
62-
return {res, false};
59+
return Constant<int64_t>();
6360
}
6461

65-
return {res, !sberr.Fail()};
62+
if (sberr.Fail()) return Constant<int64_t>();
63+
64+
return Constant<int64_t>(res, name);
6665
}
6766

6867
void Constants::Assign(SBTarget target) {
@@ -72,46 +71,49 @@ void Constants::Assign(SBTarget target) {
7271

7372

7473
int64_t Constants::LoadRawConstant(const char* name, int64_t def) {
75-
auto v = Constants::LookupConstant(target_, name, def);
76-
if (!v.second) {
74+
auto constant = Constants::LookupConstant(target_, name);
75+
if (!constant.Check()) {
7776
PRINT_DEBUG("Failed to load raw constant %s, default to %" PRId64, name,
7877
def);
78+
return def;
7979
}
8080

81-
return v.first;
81+
return *constant;
8282
}
8383

8484
int64_t Constants::LoadConstant(const char* name, int64_t def) {
85-
auto v = Constants::LookupConstant(target_,
86-
(constant_prefix() + name).c_str(), def);
87-
if (!v.second) {
88-
PRINT_DEBUG("Failed to load constant %s, default to %" PRId64, name, def);
85+
auto constant =
86+
Constants::LookupConstant(target_, (constant_prefix() + name).c_str());
87+
if (!constant.Check()) {
88+
PRINT_DEBUG("Failed to load constant %s, default to %" PRId64, name);
89+
return def;
8990
}
9091

91-
return v.first;
92+
return *constant;
9293
}
9394

9495
int64_t Constants::LoadConstant(const char* name, const char* fallback,
9596
int64_t def) {
96-
auto v = Constants::LookupConstant(target_,
97-
(constant_prefix() + name).c_str(), def);
98-
if (!v.second)
99-
v = Constants::LookupConstant(target_,
100-
(constant_prefix() + fallback).c_str(), def);
101-
if (!v.second) {
97+
auto constant =
98+
Constants::LookupConstant(target_, (constant_prefix() + name).c_str());
99+
if (!constant.Check())
100+
constant = Constants::LookupConstant(
101+
target_, (constant_prefix() + fallback).c_str());
102+
if (!constant.Check()) {
102103
PRINT_DEBUG("Failed to load constant %s, fallback %s, default to %" PRId64,
103104
name, fallback, def);
105+
return def;
104106
}
105107

106-
return v.first;
108+
return *constant;
107109
}
108110

109111
Constant<int64_t> Constants::LoadConstant(
110112
std::initializer_list<const char*> names) {
111113
for (std::string name : names) {
112-
auto v = Constants::LookupConstant(target_,
113-
(constant_prefix() + name).c_str(), -1);
114-
if (v.second) return Constant<int64_t>(v.first, name);
114+
auto constant =
115+
Constants::LookupConstant(target_, (constant_prefix() + name).c_str());
116+
if (constant.Check()) return constant;
115117
}
116118

117119
if (Error::IsDebugMode()) {
@@ -128,9 +130,9 @@ Constant<int64_t> Constants::LoadConstant(
128130
Constant<int64_t> Constants::LoadOptionalConstant(
129131
std::initializer_list<const char*> names, int def) {
130132
for (std::string name : names) {
131-
auto v = Constants::LookupConstant(target_,
132-
(constant_prefix() + name).c_str(), -1);
133-
if (v.second) return Constant<int64_t>(v.first, name);
133+
auto constant =
134+
Constants::LookupConstant(target_, (constant_prefix() + name).c_str());
135+
if (constant.Check()) return constant;
134136
}
135137

136138
return Constant<int64_t>(def);

src/constants.h

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,36 @@ using lldb::SBTarget;
1010

1111
namespace llnode {
1212

13+
enum ConstantStatus { kInvalid, kValid, kLoaded };
14+
15+
// Class representing a constant which is used to interpret memory data. Most
16+
// constants represent offset of fields on an object, bit-masks or "tags" which
17+
// are used to identify types, but there are some constants with different
18+
// meanings as well.
19+
//
20+
// By default, constants are loaded from the binary debug symbols, and usually
21+
// those debug symbols are generated by V8's gen-postmortem-metadata.py or by
22+
// Node.js' node-postmortem-metadata.cc. Some constants can also come from C++
23+
// generated debug symbols.
24+
//
25+
// When a constant is successfully loaded, Check() and Loaded() will return
26+
// true, which means we can safely use this constant and make assumptions based
27+
// on its existence. In some cases, it's safe to assume defaults for a given
28+
// constant. If that's the case, the constant will return false on Loaded() but
29+
// true on Check(). A constant returning false for both Check() and Loaded() is
30+
// not safe to use.
31+
//
32+
// Use the dereference operator (*constant) to access a constant value.
1333
template <typename T>
1434
class Constant {
1535
public:
16-
Constant() : value_(-1), valid_(false), loaded_(false) {}
17-
explicit Constant(T value)
18-
: value_(value), valid_(true), loaded_(false), name_("") {}
19-
Constant(T value, std::string name)
20-
: value_(value), valid_(true), loaded_(true), name_(name) {}
21-
22-
inline bool Check() { return valid_; }
36+
Constant() : value_(-1), status_(kInvalid) {}
37+
inline bool Check() {
38+
return (status_ == ConstantStatus::kValid ||
39+
status_ == ConstantStatus::kLoaded);
40+
}
2341

24-
inline bool Loaded() { return loaded_; }
42+
inline bool Loaded() { return status_ == kLoaded; }
2543

2644
T operator*() {
2745
// TODO(mmarchini): Check()
@@ -31,9 +49,14 @@ class Constant {
3149
inline std::string name() { return name_; }
3250

3351
protected:
52+
friend class Constants;
53+
explicit Constant(T value) : value_(value), status_(kValid), name_("") {}
54+
Constant(T value, std::string name)
55+
: value_(value), status_(kLoaded), name_(name) {}
56+
57+
private:
3458
T value_;
35-
bool valid_;
36-
bool loaded_;
59+
ConstantStatus status_;
3760
std::string name_;
3861
};
3962

@@ -55,8 +78,7 @@ class Constants {
5578

5679
inline virtual std::string constant_prefix() { return ""; };
5780

58-
static std::pair<int64_t, bool> LookupConstant(SBTarget target,
59-
const char* name, int64_t def);
81+
static Constant<int64_t> LookupConstant(SBTarget target, const char* name);
6082

6183
protected:
6284
int64_t LoadRawConstant(const char* name, int64_t def = -1);

0 commit comments

Comments
 (0)