2
2
3
3
#include < algorithm>
4
4
#include < cinttypes>
5
+ #include < cstdarg>
5
6
6
7
#include " llv8-inl.h"
7
8
#include " llv8.h"
@@ -57,14 +58,56 @@ void LLV8::Load(SBTarget target) {
57
58
types.Assign (target, &common);
58
59
}
59
60
61
+ bool Error::is_debug_mode = false ;
62
+
63
+ Error::Error (bool failed, const char * format, ...) {
64
+ failed_ = failed;
65
+ char tmp[kMaxMessageLength ];
66
+ va_list arglist;
67
+ va_start (arglist, format);
68
+ vsnprintf (tmp, sizeof (tmp), format, arglist);
69
+ va_end (arglist);
70
+ msg_ = tmp;
71
+ }
72
+
73
+
74
+ void Error::PrintInDebugMode (const char * format, ...) {
75
+ if (!is_debug_mode) {
76
+ return ;
77
+ }
78
+ char fmt[kMaxMessageLength ];
79
+ snprintf (fmt, sizeof (fmt), " [llv8] %s\n " , format);
80
+ va_list arglist;
81
+ va_start (arglist, format);
82
+ vfprintf (stderr, fmt, arglist);
83
+ va_end (arglist);
84
+ }
85
+
86
+
87
+ Error Error::Failure (std::string msg) {
88
+ PrintInDebugMode (" %s" , msg.c_str ());
89
+ return Error (true , msg);
90
+ }
91
+
92
+
93
+ Error Error::Failure (const char * format, ...) {
94
+ char tmp[kMaxMessageLength ];
95
+ va_list arglist;
96
+ va_start (arglist, format);
97
+ vsnprintf (tmp, sizeof (tmp), format, arglist);
98
+ va_end (arglist);
99
+ return Error::Failure (std::string (tmp));
100
+ }
101
+
60
102
61
103
int64_t LLV8::LoadPtr (int64_t addr, Error& err) {
62
104
SBError sberr;
63
105
int64_t value =
64
106
process_.ReadPointerFromMemory (static_cast <addr_t >(addr), sberr);
65
107
if (sberr.Fail ()) {
66
- // TODO(indutny): add more information
67
- err = Error::Failure (" Failed to load V8 value" );
108
+ // TODO(joyeecheung): use Error::Failure() to report information when
109
+ // there is less noise from here.
110
+ err = Error (true , " Failed to load pointer from v8 memory" );
68
111
return -1 ;
69
112
}
70
113
@@ -79,8 +122,9 @@ int64_t LLV8::LoadUnsigned(int64_t addr, uint32_t byte_size, Error& err) {
79
122
byte_size, sberr);
80
123
81
124
if (sberr.Fail ()) {
82
- // TODO(indutny): add more information
83
- err = Error::Failure (" Failed to load V8 value" );
125
+ // TODO(joyeecheung): use Error::Failure() to report information when
126
+ // there is less noise from here.
127
+ err = Error (true , " Failed to load unsigned from v8 memory" );
84
128
return -1 ;
85
129
}
86
130
@@ -94,8 +138,10 @@ double LLV8::LoadDouble(int64_t addr, Error& err) {
94
138
int64_t value = process_.ReadUnsignedFromMemory (static_cast <addr_t >(addr),
95
139
sizeof (double ), sberr);
96
140
if (sberr.Fail ()) {
97
- // TODO(indutny): add more information
98
- err = Error::Failure (" Failed to load V8 double value" );
141
+ err = Error::Failure (
142
+ " Failed to load double from v8 memory, "
143
+ " addr=0x%016" PRIx64,
144
+ addr);
99
145
return -1.0 ;
100
146
}
101
147
@@ -104,12 +150,15 @@ double LLV8::LoadDouble(int64_t addr, Error& err) {
104
150
}
105
151
106
152
107
- std::string LLV8::LoadBytes (int64_t length , int64_t addr , Error& err) {
153
+ std::string LLV8::LoadBytes (int64_t addr , int64_t length , Error& err) {
108
154
uint8_t * buf = new uint8_t [length + 1 ];
109
155
SBError sberr;
110
156
process_.ReadMemory (addr, buf, static_cast <size_t >(length), sberr);
111
157
if (sberr.Fail ()) {
112
- err = Error::Failure (" Failed to load V8 raw buffer" );
158
+ err = Error::Failure (
159
+ " Failed to load v8 backing store memory, "
160
+ " addr=0x%016" PRIx64 " , length=%" PRId64,
161
+ addr, length);
113
162
delete[] buf;
114
163
return std::string ();
115
164
}
@@ -135,8 +184,10 @@ std::string LLV8::LoadString(int64_t addr, int64_t length, Error& err) {
135
184
process_.ReadMemory (static_cast <addr_t >(addr), buf,
136
185
static_cast <size_t >(length), sberr);
137
186
if (sberr.Fail ()) {
138
- // TODO(indutny): add more information
139
- err = Error::Failure (" Failed to load V8 one byte string" );
187
+ err = Error::Failure (
188
+ " Failed to load v8 one byte string memory, "
189
+ " addr=0x%016" PRIx64 " , length=%" PRId64,
190
+ addr, length);
140
191
delete[] buf;
141
192
return std::string ();
142
193
}
@@ -161,8 +212,10 @@ std::string LLV8::LoadTwoByteString(int64_t addr, int64_t length, Error& err) {
161
212
process_.ReadMemory (static_cast <addr_t >(addr), buf,
162
213
static_cast <size_t >(length * 2 ), sberr);
163
214
if (sberr.Fail ()) {
164
- // TODO(indutny): add more information
165
- err = Error::Failure (" Failed to load V8 two byte string" );
215
+ err = Error::Failure (
216
+ " Failed to load V8 two byte string memory, "
217
+ " addr=0x%016" PRIx64 " , length=%" PRId64,
218
+ addr, length);
166
219
delete[] buf;
167
220
return std::string ();
168
221
}
@@ -183,8 +236,10 @@ uint8_t* LLV8::LoadChunk(int64_t addr, int64_t length, Error& err) {
183
236
process_.ReadMemory (static_cast <addr_t >(addr), buf,
184
237
static_cast <size_t >(length), sberr);
185
238
if (sberr.Fail ()) {
186
- // TODO(indutny): add more information
187
- err = Error::Failure (" Failed to load V8 memory chunk" );
239
+ err = Error::Failure (
240
+ " Failed to load V8 chunk memory, "
241
+ " addr=0x%016" PRIx64 " , length=%" PRId64,
242
+ addr, length);
188
243
delete[] buf;
189
244
return nullptr ;
190
245
}
@@ -235,7 +290,7 @@ uint32_t JSFrame::GetSourceForDisplay(bool reset_line, uint32_t line_start,
235
290
if (err.Fail ()) {
236
291
const char * msg = err.GetMessage ();
237
292
if (msg == nullptr ) {
238
- err = Error ( true , " Failed to get Function Source" );
293
+ err = Error::Failure ( " Failed to get Function Source" );
239
294
}
240
295
return line_start;
241
296
}
@@ -288,7 +343,7 @@ std::string JSFrame::Inspect(bool with_args, Error& err) {
288
343
return " <stub>" ;
289
344
} else if (value != v8 ()->frame ()->kJSFrame &&
290
345
value != v8 ()->frame ()->kOptimizedFrame ) {
291
- err = Error::Failure (" Unknown frame marker" );
346
+ err = Error::Failure (" Unknown frame marker % " PRId64, value );
292
347
return std::string ();
293
348
}
294
349
}
@@ -432,7 +487,7 @@ std::string JSFunction::GetSource(Error& err) {
432
487
433
488
// No source
434
489
if (source_type > v8 ()->types ()->kFirstNonstringType ) {
435
- err = Error ( true , " No source" );
490
+ err = Error::Failure ( " No source, source_type=% " PRId64, source_type );
436
491
return std::string ();
437
492
}
438
493
@@ -591,7 +646,7 @@ void Script::GetLines(uint64_t start_line, std::string lines[],
591
646
592
647
// No source
593
648
if (type > v8 ()->types ()->kFirstNonstringType ) {
594
- err = Error ( true , " No source" );
649
+ err = Error::Failure ( " No source, source_type=% " PRId64, type );
595
650
return ;
596
651
}
597
652
@@ -828,6 +883,8 @@ std::string HeapObject::Inspect(InspectOptions* options, Error& err) {
828
883
return pre + date.Inspect (err);
829
884
}
830
885
886
+ Error::PrintInDebugMode (
887
+ " Unknown HeapObject Type %" PRId64 " at 0x%016" PRIx64 " " , type, raw ());
831
888
return pre + " <unknown>" ;
832
889
}
833
890
@@ -956,7 +1013,7 @@ std::string String::ToString(Error& err) {
956
1013
return two.ToString (err);
957
1014
}
958
1015
959
- err = Error::Failure (" Unsupported seq string encoding" );
1016
+ err = Error::Failure (" Unsupported seq string encoding % " PRId64, encoding );
960
1017
return std::string ();
961
1018
}
962
1019
@@ -980,7 +1037,7 @@ std::string String::ToString(Error& err) {
980
1037
return thin.ToString (err);
981
1038
}
982
1039
983
- err = Error::Failure (" Unsupported string representation" );
1040
+ err = Error::Failure (" Unsupported string representation % " PRId64, repr );
984
1041
return std::string ();
985
1042
}
986
1043
@@ -1143,7 +1200,7 @@ std::string JSArrayBuffer::Inspect(InspectOptions* options, Error& err) {
1143
1200
res += " : [\n " ;
1144
1201
1145
1202
int display_length = std::min<int >(byte_length, options->length );
1146
- res += v8 ()->LoadBytes (display_length, data , err);
1203
+ res += v8 ()->LoadBytes (data, display_length , err);
1147
1204
1148
1205
if (display_length < byte_length) {
1149
1206
res += " ..." ;
@@ -1201,7 +1258,7 @@ std::string JSArrayBufferView::Inspect(InspectOptions* options, Error& err) {
1201
1258
res += " : [\n " ;
1202
1259
1203
1260
int display_length = std::min<int >(byte_length, options->length );
1204
- res += v8 ()->LoadBytes (display_length, data + byte_offset, err);
1261
+ res += v8 ()->LoadBytes (data + byte_offset, display_length , err);
1205
1262
1206
1263
if (display_length < byte_length) {
1207
1264
res += " ..." ;
@@ -1502,6 +1559,8 @@ std::string JSObject::InspectDescriptors(Map map, Error& err) {
1502
1559
1503
1560
// Skip non-fields for now
1504
1561
if (!descriptors.IsFieldDetails (details)) {
1562
+ Error::PrintInDebugMode (" Unknown field Type %" PRId64,
1563
+ details.GetValue ());
1505
1564
res += " <unknown field type>" ;
1506
1565
continue ;
1507
1566
}
0 commit comments