11#include < cinttypes>
2+ #include < initializer_list>
3+ #include < iterator>
4+ #include < sstream>
25
36#include < lldb/API/SBExpressionOptions.h>
47
@@ -14,59 +17,51 @@ namespace llnode {
1417
1518template <typename T>
1619T ReadSymbolFromTarget (SBTarget& target, SBAddress& start, const char * name,
17- Error& err) {
18- SBError sberr;
20+ SBError& sberr) {
1921 T res = 0 ;
2022 target.ReadMemory (start, &res, sizeof (T), sberr);
21- if (!sberr.Fail ()) {
22- err = Error::Ok ();
23- } else {
24- err = Error::Failure (" Failed to read symbol %s" , name);
25- }
2623 return res;
2724}
2825
29- int64_t Constants::LookupConstant (SBTarget target, const char * name,
30- int64_t def, Error& err) {
31- int64_t res = 0 ;
32- res = def;
26+ Constant<int64_t > Constants::LookupConstant (SBTarget target, const char * name) {
27+ int64_t res;
3328
3429 SBSymbolContextList context_list = target.FindSymbols (name);
3530
3631 if (!context_list.IsValid () || context_list.GetSize () == 0 ) {
37- err = Error::Failure (" Failed to find symbol %s" , name);
38- return res;
32+ return Constant<int64_t >();
3933 }
4034
4135 SBSymbolContext context = context_list.GetContextAtIndex (0 );
4236 SBSymbol symbol = context.GetSymbol ();
4337 if (!symbol.IsValid ()) {
44- err = Error::Failure (" Failed to fetch symbol %s from context" , name);
45- return res;
38+ return Constant<int64_t >();
4639 }
4740
4841 SBAddress start = symbol.GetStartAddress ();
4942 SBAddress end = symbol.GetEndAddress ();
5043 uint32_t size = end.GetOffset () - start.GetOffset ();
5144
5245 // NOTE: size could be bigger for at the end symbols
46+ SBError sberr;
5347 if (size >= 8 ) {
54- res = ReadSymbolFromTarget<int64_t >(target, start, name, err );
48+ res = ReadSymbolFromTarget<int64_t >(target, start, name, sberr );
5549 } else if (size == 4 ) {
56- int32_t tmp = ReadSymbolFromTarget<int32_t >(target, start, name, err );
50+ int32_t tmp = ReadSymbolFromTarget<int32_t >(target, start, name, sberr );
5751 res = static_cast <int64_t >(tmp);
5852 } else if (size == 2 ) {
59- int16_t tmp = ReadSymbolFromTarget<int16_t >(target, start, name, err );
53+ int16_t tmp = ReadSymbolFromTarget<int16_t >(target, start, name, sberr );
6054 res = static_cast <int64_t >(tmp);
6155 } else if (size == 1 ) {
62- int8_t tmp = ReadSymbolFromTarget<int8_t >(target, start, name, err );
56+ int8_t tmp = ReadSymbolFromTarget<int8_t >(target, start, name, sberr );
6357 res = static_cast <int64_t >(tmp);
6458 } else {
65- err = Error::Failure (" Unexpected symbol size %" PRIu32 " of symbol %s" ,
66- size, name);
59+ return Constant<int64_t >();
6760 }
6861
69- return res;
62+ if (sberr.Fail ()) return Constant<int64_t >();
63+
64+ return Constant<int64_t >(res, name);
7065}
7166
7267void Constants::Assign (SBTarget target) {
@@ -76,44 +71,71 @@ void Constants::Assign(SBTarget target) {
7671
7772
7873int64_t Constants::LoadRawConstant (const char * name, int64_t def) {
79- Error err;
80- int64_t v = Constants::LookupConstant (target_, name, def, err);
81- if (err.Fail ()) {
74+ auto constant = Constants::LookupConstant (target_, name);
75+ if (!constant.Check ()) {
8276 PRINT_DEBUG (" Failed to load raw constant %s, default to %" PRId64, name,
8377 def);
78+ return def;
8479 }
8580
86- return v;
87- }
88-
89- int64_t Constants::LoadConstant (const char * name, Error& err, int64_t def) {
90- int64_t v = Constants::LookupConstant (
91- target_, (constant_prefix () + name).c_str (), def, err);
92- return v;
81+ return *constant;
9382}
9483
9584int64_t Constants::LoadConstant (const char * name, int64_t def) {
96- Error err;
97- int64_t v = LoadConstant (name, err, def);
98- if (err.Fail ()) {
99- 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;
10090 }
10191
102- return v ;
92+ return *constant ;
10393}
10494
10595int64_t Constants::LoadConstant (const char * name, const char * fallback,
10696 int64_t def) {
107- Error err;
108- int64_t v = LoadConstant (name, err, def);
109- if (err.Fail ()) v = LoadConstant (fallback, err, def);
110- if (err.Fail ()) {
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 ()) {
111103 PRINT_DEBUG (" Failed to load constant %s, fallback %s, default to %" PRId64,
112104 name, fallback, def);
105+ return def;
113106 }
114107
115- return v ;
108+ return *constant ;
116109}
117110
111+ Constant<int64_t > Constants::LoadConstant (
112+ std::initializer_list<const char *> names) {
113+ for (std::string name : names) {
114+ auto constant =
115+ Constants::LookupConstant (target_, (constant_prefix () + name).c_str ());
116+ if (constant.Check ()) return constant;
117+ }
118+
119+ if (Error::IsDebugMode ()) {
120+ std::string joined = " " ;
121+ for (std::string name : names) {
122+ joined += (joined.empty () ? " '" : " , '" ) + name + " '" ;
123+ }
124+ PRINT_DEBUG (" Failed to load constants: %s" , joined.c_str ());
125+ }
126+
127+ return Constant<int64_t >();
128+ }
129+
130+ Constant<int64_t > Constants::LoadOptionalConstant (
131+ std::initializer_list<const char *> names, int def) {
132+ for (std::string name : names) {
133+ auto constant =
134+ Constants::LookupConstant (target_, (constant_prefix () + name).c_str ());
135+ if (constant.Check ()) return constant;
136+ }
137+
138+ return Constant<int64_t >(def);
139+ }
118140
119141} // namespace llnode
0 commit comments