1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use num_traits::Zero;
use thousands::Separable;
use super::*;
use std::{convert::TryInto, fmt};
#[derive(Debug, Clone, PartialEq)]
pub enum VariableKind {
Indexed,
Named,
Referenced,
Undetermined,
}
impl Default for VariableKind {
fn default() -> Self {
VariableKind::Undetermined
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum VariableInclusion {
Exclude,
Include,
Undetermined,
}
impl Default for VariableInclusion {
fn default() -> Self {
VariableInclusion::Undetermined
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum VariantRole {
VariantPart(u64),
Variant(u64),
NonVariant,
}
impl Default for VariantRole {
fn default() -> Self {
VariantRole::NonVariant
}
}
#[derive(Debug, Default, Clone)]
pub struct Variable {
pub name: String,
value: String,
pub file: String,
pub line: u64,
pub type_name: String,
pub memory_location: u64,
pub byte_size: u64,
pub(crate) member_index: Option<i64>,
pub(crate) range_lower_bound: i64,
pub(crate) range_upper_bound: i64,
pub kind: VariableKind,
pub role: VariantRole,
pub(crate) inclusion: VariableInclusion,
pub children: Option<Vec<Variable>>,
}
impl Variable {
pub fn new() -> Variable {
Variable {
name: String::new(),
value: String::new(),
file: String::new(),
memory_location: 0,
..Default::default()
}
}
pub fn set_value(&mut self, new_value: String) {
if self.value.is_empty() {
self.value = new_value;
} else {
self.value = format!("{} : {}", self.value, new_value);
}
}
pub fn get_value(&self) -> String {
self.value.clone()
}
pub fn extract_value(&mut self, core: &mut Core<'_>) {
if self.name.starts_with("__") {
self.kind = VariableKind::Indexed
} else {
self.kind = VariableKind::Named
}
if self.memory_location == u64::MAX
|| !self.value.is_empty()
|| self.memory_location.is_zero()
{
return;
}
let string_value = match self.type_name.as_str() {
"!" => "<Never returns>".to_string(),
"()" => "()".to_string(),
"bool" => bool::get_value(self, core)
.map_or_else(|err| format!("ERROR: {:?}", err), |value| value.to_string()),
"char" => char::get_value(self, core)
.map_or_else(|err| format!("ERROR: {:?}", err), |value| value.to_string()),
"&str" => {
let string_value = String::get_value(self, core)
.map_or_else(|err| format!("ERROR: {:?}", err), |value| value);
self.children = None;
string_value
}
"i8" => i8::get_value(self, core)
.map_or_else(|err| format!("ERROR: {:?}", err), |value| value.to_string()),
"i16" => i16::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"i32" => i32::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"i64" => i64::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"i128" => i128::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"isize" => isize::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"u8" => u8::get_value(self, core)
.map_or_else(|err| format!("ERROR: {:?}", err), |value| value.to_string()),
"u16" => u16::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"u32" => u32::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"u64" => u64::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"u128" => u128::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"usize" => usize::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"f32" => f32::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"f64" => f64::get_value(self, core).map_or_else(
|err| format!("ERROR: {:?}", err),
|value| value.separate_with_underscores(),
),
"None" => "None".to_string(),
oops => match &self.children {
Some(_children) => {
if oops.is_empty() {
"ERROR: This is a bug! Attempted to evaluate an empty Type".to_string()
} else {
format!("{}", self)
}
}
None => {
format!(
"UNIMPLEMENTED: Evaluate type {} of ({} bytes) at location 0x{:08x}",
oops, self.byte_size, self.memory_location
)
}
},
};
self.value = string_value;
}
pub fn add_child_variable(&mut self, child_variable: &mut Variable, core: &mut Core<'_>) {
if !(child_variable.inclusion == VariableInclusion::Undetermined
|| child_variable.inclusion == VariableInclusion::Exclude)
{
let children: &mut Vec<Variable> = match &mut self.children {
Some(children) => children,
None => {
self.children = Some(vec![]);
self.children.as_mut().unwrap()
}
};
child_variable.extract_value(core);
self.inclusion = VariableInclusion::Include;
if child_variable.inclusion == VariableInclusion::Include {
if let Some(existing_child) = children.iter_mut().find(|current_child| {
current_child.name == child_variable.name
&& current_child.type_name == child_variable.type_name
}) {
if let Some(new_children) = child_variable.children.clone() {
for mut new_child in new_children {
existing_child.add_child_variable(&mut new_child, core);
}
}
} else {
children.push(child_variable.clone());
}
}
}
}
}
impl fmt::Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.value.is_empty() {
if let Some(children) = self.children.clone() {
if self.type_name.starts_with('&') {
write!(f, "{}", children.first().unwrap())
} else if self.type_name.starts_with('(') {
write!(f, "(")?;
for child in children {
write!(f, "{}, ", child)?;
}
write!(f, ")")
} else if self.type_name.starts_with('[') {
write!(f, "[")?;
for child in children {
write!(f, "{}, ", child)?;
}
write!(f, "]")
} else {
if self.kind == VariableKind::Named {
write!(f, "{}:{{", self.name)?;
} else {
write!(f, "{{")?;
}
for child in children {
write!(f, "{}, ", child)?;
}
write!(f, "}}")
}
} else {
write!(f, "{}", self.type_name)
}
} else {
write!(f, "{}", self.value)
}
}
}
trait Value {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError>
where
Self: Sized;
}
impl Value for bool {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mem_data = core.read_word_8(variable.memory_location as u32)?;
let ret_value: bool = mem_data != 0;
Ok(ret_value)
}
}
impl Value for char {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mem_data = core.read_word_32(variable.memory_location as u32)?;
let ret_value: char = mem_data.try_into()?;
Ok(ret_value)
}
}
impl Value for String {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let str_value: String;
match variable.clone().children {
Some(children) => {
let string_length = match children
.clone()
.into_iter()
.find(|child_variable| child_variable.name == *"length")
{
Some(length_value) => length_value.value.parse().unwrap_or(0) as usize,
None => 0_usize,
};
let string_location = match children
.into_iter()
.find(|child_variable| child_variable.name == *"data_ptr")
{
Some(location_value) => {
if let Some(child_variables) = location_value.children {
if let Some(first_child) = child_variables.first() {
first_child.memory_location as u32
} else {
0_u32
}
} else {
0_u32
}
}
None => 0_u32,
};
if string_location.is_zero() {
str_value = "ERROR: Failed to determine &str memory location".to_string();
} else {
let mut buff = vec![0u8; string_length];
core.read(string_location as u32, &mut buff)?;
str_value = core::str::from_utf8(&buff)?.to_owned();
}
}
None => {
str_value = "ERROR: Failed to evaluate &str value".to_string();
}
};
Ok(str_value)
}
}
impl Value for i8 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 1];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = i8::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for i16 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 2];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = i16::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for i32 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 4];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = i32::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for i64 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 8];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = i64::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for i128 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 16];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = i128::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for isize {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 4];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = i32::from_le_bytes(buff);
Ok(ret_value as isize)
}
}
impl Value for u8 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 1];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = u8::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for u16 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 2];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = u16::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for u32 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 4];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = u32::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for u64 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 8];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = u64::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for u128 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 16];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = u128::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for usize {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 4];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = u32::from_le_bytes(buff);
Ok(ret_value as usize)
}
}
impl Value for f32 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 4];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = f32::from_le_bytes(buff);
Ok(ret_value)
}
}
impl Value for f64 {
fn get_value(variable: &Variable, core: &mut Core<'_>) -> Result<Self, DebugError> {
let mut buff = [0u8; 8];
core.read(variable.memory_location as u32, &mut buff)?;
let ret_value = f64::from_le_bytes(buff);
Ok(ret_value)
}
}