probe-rs 0.13.0

A collection of on chip debugging tools to communicate with microchips.
Documentation
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
use super::*;
use anyhow::anyhow;
use gimli::{DebugInfoOffset, UnitOffset};
use num_traits::Zero;
use std::str::FromStr;

/// Define the role that a variable plays in a Variant relationship. See section '5.7.10 Variant Entries' of the DWARF 5 specification
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum VariantRole {
    /// A (parent) Variable that can have any number of Variant's as its value
    VariantPart(u64),
    /// A (child) Variable that defines one of many possible types to hold the current value of a VariantPart.
    Variant(u64),
    /// This variable doesn't play a role in a Variant relationship
    NonVariant,
}

impl Default for VariantRole {
    fn default() -> Self {
        VariantRole::NonVariant
    }
}

/// A [Variable] will have either a valid value, or some reason why a value could not be constructed.
/// - If we encounter expected errors, they will be displayed to the user as defined below.
/// - If we encounter unexpected errors, they will be treated as proper errors and will propogated to the calling process as an `Err()`
#[derive(Clone, Debug, PartialEq)]
pub enum VariableValue {
    /// A valid value of this variable
    Valid(String),
    /// Notify the user that we encountered a problem correctly resolving the variable.
    /// - The variable will be visible to the user, as will the other field of the variable.
    /// - The contained warning message will be displayed to the user.
    /// - The debugger will not attempt to resolve additional fields or children of this variable.
    Error(String),
    /// The value has not been set. This could be because ...
    /// - It is too early in the process to have discovered its value, or ...
    /// - The variable cannot have a stored value, e.g. a `struct`. In this case, please use `Variable::get_value` to infer a human readable value from the value of the struct's fields.
    Empty,
}

impl Default for VariableValue {
    fn default() -> Self {
        VariableValue::Empty
    }
}

impl std::fmt::Display for VariableValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VariableValue::Valid(value) => value.fmt(f),
            VariableValue::Error(error) => write!(f, "< {} >", error,),
            VariableValue::Empty => write!(
                f,
                "Value not set. Please use Variable::get_value() to infer a human readable variable value"
            ),
        }
    }
}

impl VariableValue {
    /// A VariableValue is valid if it doesn't contain an Info or a Warning.
    pub fn is_valid(&self) -> bool {
        !matches!(self, VariableValue::Error(_))
    }
    /// No value or error is present
    pub fn is_empty(&self) -> bool {
        matches!(self, VariableValue::Empty)
    }
}

/// The type of variable we have at hand.
#[derive(Debug, PartialEq, Clone)]
pub enum VariableName {
    /// Top-level variable for static variables, child of a stack frame variable, and holds all the static scoped variables which are directly visible to the compile unit of the frame.
    StaticScopeRoot,
    /// Top-level variable for registers, child of a stack frame variable.
    RegistersRoot,
    /// Top-level variable for local scoped variables, child of a stack frame variable.
    LocalScopeRoot,
    /// Top-level variable for CMSIS-SVD file Device peripherals/registers/fields.
    PeripheralScopeRoot,
    /// Artificial variable, without a name (e.g. enum discriminant)
    Artifical,
    /// Anonymous namespace
    AnonymousNamespace,
    /// A Namespace with a specific name
    Namespace(String),
    /// Variable with a specific name
    Named(String),
    /// Variable with an unknown name
    Unknown,
}

impl Default for VariableName {
    fn default() -> Self {
        VariableName::Unknown
    }
}

impl std::fmt::Display for VariableName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VariableName::StaticScopeRoot => write!(f, "Static Variable"),
            VariableName::RegistersRoot => write!(f, "Platform Register"),
            VariableName::LocalScopeRoot => write!(f, "Function Variable"),
            VariableName::PeripheralScopeRoot => write!(f, "Peripheral Variable"),
            VariableName::Artifical => write!(f, "<artifical>"),
            VariableName::AnonymousNamespace => write!(f, "<anonymous_namespace>"),
            VariableName::Namespace(name) => name.fmt(f),
            VariableName::Named(name) => name.fmt(f),
            VariableName::Unknown => write!(f, "<unknown>"),
        }
    }
}

/// Encode the nature of the Debug Information Entry in a way that we can resolve child nodes of a [Variable]
/// The rules for 'lazy loading'/deferred recursion of [Variable] children are described under each of the enum values.
#[derive(Debug, PartialEq, Clone)]
pub enum VariableNodeType {
    /// For pointer values, their referenced variables are found at an [gimli::UnitOffset] in the [DebugInfo].
    /// - Rule: Pointers to `struct` variables WILL NOT BE recursed, because  this may lead to infinite loops/stack overflows in `struct`s that self-reference.
    /// - Rule: Pointers to "base" datatypes SHOULD BE, but ARE NOT resolved, because it would keep the UX simple, but DWARF doesn't make it easy to determine when a pointer points to a base data type. We can read ahead in the DIE children, but that feels rather inefficient.
    ReferenceOffset(UnitOffset),
    /// Use the `header_offset` and `type_offset` as direct references for recursing the variable children. With the current implementation, the `type_offset` will point to a DIE with a tag of `DW_TAG_structure_type`.
    /// - Rule: For structured variables, we WILL NOT automatically expand their children, but we have enough information to expand it on demand. Except if they fall into one of the special cases handled by [VariableNodeType::RecurseAsIntermediate]
    TypeOffset(UnitOffset),
    /// Use the `header_offset` and `entries_offset` as direct references for recursing the variable children.
    /// - Rule: All top level variables in a [StackFrame] are automatically deferred, i.e [VariableName::StaticScope], [VariableName::Registers], [VariableName::LocalScope].
    DirectLookup,
    /// Sometimes it doesn't make sense to recurse the children of a specific node type
    /// - Rule: Pointers to `unit` datatypes WILL NOT BE resolved, because it doesn't make sense.
    /// - Rule: Once we determine that a variable can not be recursed further, we update the variable_node_type to indicate that no further recursion is possible/required. This can be because the variable is a 'base' data type, or because there was some kind of error in processing the current node, so we don't want to incur cascading errors.
    /// TODO: Find code instances where we use magic values (e.g. u32::MAX) and replace with DoNotRecurse logic if appropriate.
    DoNotRecurse,
    /// Unless otherwise specified, always recurse the children of every node until we get to the base data type.
    /// - Rule: (Default) Unless it is prevented by any of the other rules, we always recurse the children of these variables.
    /// - Rule: Certain structured variables (e.g. `&str`, `Some`, `Ok`, `Err`, etc.) are set to [VariableNodeType::RecurseToBaseType] to improve the debugger UX.
    /// - Rule: Pointers to `const` variables WILL ALWAYS BE recursed, because they provide essential information, for example about the length of strings, or the size of arrays.
    /// - Rule: Enumerated types WILL ALWAYS BE recursed, because we only ever want to see the 'active' child as the value.
    /// - Rule: For now, Array types WILL ALWAYS BE recursed. TODO: Evaluate if it is beneficial to defer these.
    /// - Rule: For now, Union types WILL ALWAYS BE recursed. TODO: Evaluate if it is beneficial to defer these.
    RecurseToBaseType,
    /// SVD Device Peripherals
    SvdPeripheral,
    /// SVD Peripheral Registers
    SvdRegister,
    /// SVD Register Fields
    SvdField,
}

impl VariableNodeType {
    /// Will return true if any of the `variable_node_type` value implies that the variable will be 'lazy' resolved.
    pub fn is_deferred(&self) -> bool {
        match self {
            VariableNodeType::ReferenceOffset(_)
            | VariableNodeType::TypeOffset(_)
            | VariableNodeType::DirectLookup => true,
            _other => false,
        }
    }
}

impl Default for VariableNodeType {
    fn default() -> Self {
        VariableNodeType::RecurseToBaseType
    }
}

/// The variants of VariableType allows us to streamline the conditional logic that requires specific handling depending on the nature of the variable.
#[derive(Debug, Clone, PartialEq)]
pub enum VariableType {
    /// A variable with a Rust base datatype.
    Base(String),
    /// A Rust struct.
    Struct(String),
    /// A Rust enum.
    Enum(String),
    /// Namespace refers to the path that qualifies a variable. e.g. "std::string" is the namespace for the strucct "String"
    Namespace,
    /// A Pointer is a variable that contains a reference to another variable
    Pointer(Option<String>),
    /// A Rust array.
    Array {
        // TODO: Use a proper type here, not variable name
        /// The name of the variable.
        entry_type: VariableName,
        /// The number of entries in the array.
        count: usize,
    },
    /// When we are unable to determine the name of a variable.
    Unknown,
    /// For infrequently used categories of variables that does not fall into any of the other VriableType variants.
    Other(String),
}

impl Default for VariableType {
    fn default() -> Self {
        VariableType::Unknown
    }
}

impl VariableType {
    /// A Rust PhantomData type used as a marker for to "act like" they own a specific type.
    pub fn is_phantom_data(&self) -> bool {
        match self {
            VariableType::Struct(name) => name.starts_with("PhantomData"),
            _ => false,
        }
    }

    /// This variable is a reference to another variable.
    pub fn is_reference(&self) -> bool {
        match self {
            VariableType::Pointer(Some(name)) => name.starts_with('&'),
            _ => false,
        }
    }

    /// This variable is an array, and requires special processing during
    pub fn is_array(&self) -> bool {
        matches!(self, VariableType::Array { .. })
    }
}

impl std::fmt::Display for VariableType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VariableType::Base(base) => base.fmt(f),
            VariableType::Struct(struct_name) => struct_name.fmt(f),
            VariableType::Enum(enum_name) => enum_name.fmt(f),
            VariableType::Namespace => "<namespace>".fmt(f),
            VariableType::Pointer(pointer_name) => pointer_name
                .clone()
                .unwrap_or_else(|| "<unnamed>".to_string())
                .fmt(f),
            VariableType::Array { entry_type, count } => write!(f, "[{}; {}]", entry_type, count),
            VariableType::Unknown => "<unknown>".fmt(f),
            VariableType::Other(other) => other.fmt(f),
        }
    }
}

/// Location of a variable
#[derive(Debug, Clone, PartialEq)]
pub enum VariableLocation {
    /// Location of the variable is not known. This means that it has not been evaluated yet.
    Unknown,
    /// The variable does not have a location currently, probably due to optimisations.
    Unavailable,
    /// The variable can be found in memory, at this address.
    Address(u64),
    /// The value of the variable is directly available.
    Value,
    /// There was an error evaluating the variable location.
    Error(String),
    /// Support for handling the location of this variable is not (yet) implemented.
    Unsupported(String),
}

impl VariableLocation {
    /// Return the memory address, if available. Otherwise an error is returned.
    pub fn memory_address(&self) -> Result<u64, DebugError> {
        match self {
            VariableLocation::Address(address) => Ok(*address),
            other => Err(DebugError::Other(anyhow!(
                "Variable does not have a memory location: location={:?}",
                other
            ))),
        }
    }

    /// Check if the location is valid, ie. not an error, unsupported, or unavailable.
    pub fn valid(&self) -> bool {
        match self {
            VariableLocation::Address(_) | VariableLocation::Value | VariableLocation::Unknown => {
                true
            }
            _other => false,
        }
    }
}

impl std::fmt::Display for VariableLocation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VariableLocation::Unknown => "<unknown value>".fmt(f),
            VariableLocation::Unavailable => "<value not available>".fmt(f),
            VariableLocation::Address(address) => write!(f, "{:#010X}", address),
            VariableLocation::Value => "<not applicable - statically stored value>".fmt(f),
            VariableLocation::Error(error) => error.fmt(f),
            VariableLocation::Unsupported(reason) => reason.fmt(f),
        }
    }
}

impl Default for VariableLocation {
    fn default() -> Self {
        VariableLocation::Unknown
    }
}

/// The `Variable` struct is used in conjunction with `VariableCache` to cache data about variables.
///
/// Any modifications to the `Variable` value will be transient (lost when it goes out of scope),
/// unless it is updated through one of the available methods on `VariableCache`.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Variable {
    /// Every variable must have a unique key value assigned to it. The value will be zero until it is stored in VariableCache, at which time its value will be set to the same as the VariableCache::variable_cache_key
    pub variable_key: i64,
    /// Every variable must have a unique parent assigned to it when stored in the VariableCache. A parent_key of None in the cache simply implies that this variable doesn't have a parent, i.e. it is the root of a tree.
    pub parent_key: Option<i64>,
    /// The variable name refers to the name of any of the types of values described in the [VariableCache]
    pub name: VariableName,
    /// Use `Variable::set_value()` and `Variable::get_value()` to correctly process this `value`
    value: VariableValue,
    /// The source location of the declaration of this variable, if available.
    pub source_location: Option<SourceLocation>,

    /// The name of the type of this variable.
    pub type_name: VariableType,
    /// The unit_header_offset and variable_unit_offset are cached to allow on-demand access to the variable's gimli::Unit, through functions like:
    ///   `gimli::Read::DebugInfo.header_from_offset()`, and   
    ///   `gimli::Read::UnitHeader.entries_tree()`
    pub unit_header_offset: Option<DebugInfoOffset>,
    /// The offset of this variable into the compilation unit debug information.
    pub variable_unit_offset: Option<UnitOffset>,
    /// For 'lazy loading' of certain variable types we have to determine if the variable recursion should be deferred, and if so, how to resolve it when the request for further recursion happens.
    /// See [VariableNodeType] for more information.
    pub variable_node_type: VariableNodeType,
    /// The starting location/address in memory where this Variable's value is stored.
    pub memory_location: VariableLocation,
    /// The size of this variable in bytes.
    pub byte_size: u64,
    /// If  this is a subrange (array, vector, etc.), is the ordinal position of this variable in that range
    pub member_index: Option<i64>,
    /// If this is a subrange (array, vector, etc.), we need to temporarily store the lower bound.
    pub range_lower_bound: i64,
    /// If this is a subrange (array, vector, etc.), we need to temporarily store the the upper bound of the range.
    pub range_upper_bound: i64,
    /// The role of this variable.
    pub role: VariantRole,
}

impl Variable {
    /// In most cases, Variables will be initialized with their ELF references so that we resolve their data types and values on demand.
    pub fn new(
        header_offset: Option<DebugInfoOffset>,
        entries_offset: Option<UnitOffset>,
    ) -> Variable {
        Variable {
            unit_header_offset: header_offset,
            variable_unit_offset: entries_offset,
            ..Default::default()
        }
    }

    /// Implementing set_value(), because the library passes errors into the value of the variable.
    /// This ensures debug front ends can see the errors, but doesn't fail because of a single variable not being able to decode correctly.
    pub fn set_value(&mut self, new_value: VariableValue) {
        // Allow some block when logic requires it.
        #[allow(clippy::if_same_then_else)]
        if new_value.is_valid() {
            // Simply overwrite existing value with a new valid one.
            self.value = new_value;
        } else if self.value.is_valid() {
            // Overwrite a valid value with an error.
            self.value = new_value;
        } else {
            // Concatenate the error messages ...
            self.value = VariableValue::Error(format!("{} : {}", self.value, new_value));
        }
    }

    /// Call the underlaying [Value::update_value] trait to convert the [String] value into the appropriate memory format and update the target memory with the new value.
    /// Currently this only works for base data types. There is no provision in the MS DAP API to catch this client side, so we can only respond with a 'gentle' error message if the user attemtps unsupported data types.
    pub fn update_value(
        &self,
        core: &mut Core,
        variable_cache: &mut variable_cache::VariableCache,
        new_value: String,
    ) -> Result<String, DebugError> {
        let variable_name = if let VariableName::Named(variable_name) = &self.name {
            variable_name.clone()
        } else {
            String::new()
        };
        let updated_value = if !self.is_valid()
                // Need a valid type
                || self.type_name == VariableType::Unknown
                // Need a valid memory location
                || !self.memory_location.valid()
        {
            // Insufficient data available.
            return Err(anyhow!(
                "Cannot update variable: {:?}, with supplied information (value={:?}, type={:?}, memory location={:#010x?}).",
                self.name, self.value, self.type_name, self.memory_location).into());
        } else if variable_name.starts_with('*') {
            // Writing the values of pointers is a bit more complex, and not currently supported.
            return  Err(anyhow!("Please only update variables with a base data type. Updating pointer variable types is not yet supported.").into());
        } else {
            // We have everything we need to update the variable value.
            let update_result = match &self.type_name {
                VariableType::Base(name) => match name.as_str() {
                    "bool" => bool::update_value(self, core, new_value.as_str()),
                    "char" => char::update_value(self, core, new_value.as_str()),
                    "i8" => i8::update_value(self, core, new_value.as_str()),
                    "i16" => i16::update_value(self, core, new_value.as_str()),
                    "i32" => i32::update_value(self, core, new_value.as_str()),
                    "i64" => i64::update_value(self, core, new_value.as_str()),
                    "i128" => i128::update_value(self, core, new_value.as_str()),
                    "isize" => isize::update_value(self, core, new_value.as_str()),
                    "u8" => u8::update_value(self, core, new_value.as_str()),
                    "u16" => u16::update_value(self, core, new_value.as_str()),
                    "u32" => u32::update_value(self, core, new_value.as_str()),
                    "u64" => u64::update_value(self, core, new_value.as_str()),
                    "u128" => u128::update_value(self, core, new_value.as_str()),
                    "usize" => usize::update_value(self, core, new_value.as_str()),
                    "f32" => f32::update_value(self, core, new_value.as_str()),
                    "f64" => f64::update_value(self, core, new_value.as_str()),
                    other => Err(DebugError::Other(anyhow::anyhow!(
                    "Unsupported datatype: {}. Please only update variables with a base data type.",
                    other.to_string()
                ))),
                },
                other => Err(DebugError::Other(anyhow::anyhow!(
                    "Unsupported variable type {:?}. Only base variables can be updated.",
                    other
                ))),
            };

            match update_result {
                Ok(()) => {
                    // Now update the cache with the new value for this variable.
                    let mut cache_variable = self.clone();
                    cache_variable.value = VariableValue::Valid(new_value.clone());
                    variable_cache.cache_variable(
                        cache_variable.parent_key,
                        cache_variable,
                        core,
                    )?;
                    new_value
                }
                Err(error) => {
                    return Err(DebugError::Other(anyhow::anyhow!(
                        "Invalid data value={:?}: {}",
                        new_value,
                        error
                    )));
                }
            }
        };
        Ok(updated_value)
    }

    /// Implementing get_value(), because Variable.value has to be private (a requirement of updating the value without overriding earlier values ... see set_value()).
    pub fn get_value(&self, variable_cache: &variable_cache::VariableCache) -> String {
        // Allow for chained `if let` without complaining
        #[allow(clippy::if_same_then_else)]
        if VariableNodeType::SvdRegister == self.variable_node_type {
            if let VariableValue::Valid(register_value) = &self.value {
                if let Ok(register_u32_value) = register_value.parse::<u32>() {
                    format!(
                        "{:032b} @ {:#010X}",
                        register_u32_value,
                        self.memory_location.memory_address().unwrap_or(u64::MAX) // We should never encounter a memory location that is invalid if we already used it to read the register value.
                    )
                } else {
                    format!("Invalid register value {}", register_value)
                }
            } else {
                format!("{}", self.value)
            }
        } else if VariableNodeType::SvdField == self.variable_node_type {
            // In this special case, we extract just the bits we need from the stored value of the register.
            if let VariableValue::Valid(register_value) = &self.value {
                if let Ok(register_u32_value) = register_value.parse::<u32>() {
                    let mut bit_value: u32 = register_u32_value;
                    bit_value <<= 32 - self.range_upper_bound;
                    bit_value >>= 32 - (self.range_upper_bound - self.range_lower_bound);
                    format!(
                        "{:0width$b} @ {:#010X}:{}..{}",
                        bit_value,
                        self.memory_location.memory_address().unwrap_or(u64::MAX),
                        self.range_lower_bound,
                        self.range_upper_bound,
                        width = (self.range_upper_bound - self.range_lower_bound) as usize
                    )
                } else {
                    format!(
                        "Invalid bit range {}..{} from value {}",
                        self.range_lower_bound, self.range_upper_bound, register_value
                    )
                }
            } else {
                format!("{}", self.value)
            }
        } else if !self.value.is_empty() {
            // The `value` for this `Variable` is non empty because ...
            // - It is base data type for which a value was determined based on the core runtime, or ...
            // - We encountered an error somewhere, so report it to the user
            format!("{}", self.value)
        } else if let VariableName::AnonymousNamespace = self.name {
            // Namespaces do not have values
            String::new()
        } else if let VariableName::Namespace(_) = self.name {
            // Namespaces do not have values
            String::new()
        } else {
            // We need to construct a 'human readable' value using `fmt::Display` to represent the values of complex types and pointers.
            match variable_cache.has_children(self) {
                Ok(has_children) => {
                    if has_children {
                        self.formatted_variable_value(variable_cache, 0_usize, false)
                    } else if self.type_name == VariableType::Unknown
                        || !self.memory_location.valid()
                    {
                        if self.variable_node_type.is_deferred() {
                            // When we will do a lazy-load of variable children, and they have not yet been requested by the user, just display the type_name as the value
                            format!("{:?}", self.type_name.clone())
                        } else {
                            // This condition should only be true for intermediate nodes from DWARF. These should not show up in the final `VariableCache`
                            // If a user sees this error, then there is a logic problem in the stack unwind
                            "Error: This is a bug! Attempted to evaluate a Variable with no type or no memory location".to_string()
                        }
                    } else if self.type_name == VariableType::Struct("None".to_string()) {
                        "None".to_string()
                    } else {
                        format!(
                            "Unimplemented: Evaluate type {:?} of ({} bytes) at location 0x{:08x?}",
                            self.type_name, self.byte_size, self.memory_location
                        )
                    }
                }
                Err(error) => format!(
                    "Failed to determine children for `Variable`:{}. {:?}",
                    self.name, error
                ),
            }
        }
    }

    /// Evaluate the variable's result if possible and set self.value, or else set self.value as the error String.
    pub fn extract_value(
        &mut self,
        core: &mut Core<'_>,
        variable_cache: &variable_cache::VariableCache,
    ) {
        if let VariableValue::Error(_) = self.value {
            // Nothing more to do ...
            return;
        } else if self.variable_node_type == VariableNodeType::SvdRegister
            || self.variable_node_type == VariableNodeType::SvdField
        {
            // Special handling for SVD registers.
            // Because we cache the SVD structure once per sesion, we have to re-read the actual register values whenever queried.
            match core.read_word_32(self.memory_location.memory_address().unwrap_or(u64::MAX)) {
                Ok(u32_value) => self.value = VariableValue::Valid(u32_value.to_le().to_string()),
                Err(error) => {
                    self.value = VariableValue::Error(format!(
                        "Unable to read peripheral register value @ {:#010X} : {:?}",
                        self.memory_location.memory_address().unwrap_or(u64::MAX),
                        error
                    ))
                }
            }
            return;
        } else if !self.value.is_empty()
        // The value was set explicitly, so just leave it as is, or it was an error, so don't attempt anything else
        || !self.memory_location.valid()
        // This may just be that we are early on in the process of `Variable` evaluation
        || self.type_name == VariableType::Unknown
        // This may just be that we are early on in the process of `Variable` evaluation
        {
            // Quick exit if we don't really need to do much more.
            return;
        } else if self.variable_node_type.is_deferred() {
            // And we have not previously assigned the value, then assign the type and address as the value
            self.value =
                VariableValue::Valid(format!("{} @ {}", self.type_name, self.memory_location));
            return;
        }

        log::trace!(
            "Extracting value for {:?}, type={:?}",
            self.name,
            self.type_name
        );

        // This is the primary logic for decoding a variable's value, once we know the type and memory_location.
        let known_value = match &self.type_name {
            VariableType::Base(name) => {
                if self.memory_location == VariableLocation::Unknown {
                    self.value = VariableValue::Empty;
                    return;
                }

                match name.as_str() {
                    "!" => VariableValue::Valid("<Never returns>".to_string()),
                    "()" => VariableValue::Valid("()".to_string()),
                    "bool" => bool::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "char" => char::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "i8" => i8::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "i16" => i16::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "i32" => i32::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "i64" => i64::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "i128" => i128::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "isize" => isize::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "u8" => u8::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "u16" => u16::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "u32" => u32::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "u64" => u64::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "u128" => u128::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "usize" => usize::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "f32" => f32::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "f64" => f64::get_value(self, core, variable_cache).map_or_else(
                        |err| VariableValue::Error(format!("{:?}", err)),
                        |value| VariableValue::Valid(value.to_string()),
                    ),
                    "None" => VariableValue::Valid("None".to_string()),
                    _undetermined_value => VariableValue::Empty,
                }
            }
            VariableType::Struct(name) if name == "&str" => {
                String::get_value(self, core, variable_cache).map_or_else(
                    |err| VariableValue::Error(format!("{:?}", err)),
                    VariableValue::Valid,
                )
            }
            _other => VariableValue::Empty,
        };
        self.value = known_value;
    }

    /// The variable is considered to be an 'indexed' variable if the name starts with two underscores followed by a number. e.g. "__1".
    /// TODO: Consider replacing this logic with `std::str::pattern::Pattern` when that API stabilizes
    pub fn is_indexed(&self) -> bool {
        match &self.name {
            VariableName::Named(name) => {
                name.starts_with("__")
                    && name
                        .find(char::is_numeric)
                        .map_or(false, |zero_based_position| zero_based_position == 2)
            }
            // Other kind of variables are never indexed
            _ => false,
        }
    }

    /// `true` if the Variable has a valid value, or an empty value.
    /// `false` if the Variable has a VariableValue::Error(_)value
    pub fn is_valid(&self) -> bool {
        self.value.is_valid()
    }

    fn formatted_variable_value(
        &self,
        variable_cache: &variable_cache::VariableCache,
        indentation: usize,
        show_name: bool,
    ) -> String {
        let line_feed = if indentation.is_zero() { "" } else { "\n" }.to_string();
        // Allow for chained `if let` without complaining
        #[allow(clippy::if_same_then_else)]
        if !self.value.is_empty() {
            if show_name {
                // Use the supplied value or error message.
                format!(
                    "{}{:\t<indentation$}{}: {} = {}",
                    line_feed, "", self.name, self.type_name, self.value
                )
            } else {
                // Use the supplied value or error message.
                format!("{}{:\t<indentation$}{}", line_feed, "", self.value)
            }
        } else if let VariableName::AnonymousNamespace = self.name {
            // Namespaces do not have values
            String::new()
        } else if let VariableName::Namespace(_) = self.name {
            // Namespaces do not have values
            String::new()
        } else {
            // Infer a human readable value using the available children of this variable.
            let mut compound_value = String::new();
            if let Ok(children) = variable_cache.get_children(Some(self.variable_key)) {
                // Make sure we can safely unwrap() children.
                match &self.type_name {
                    VariableType::Pointer(_) => {
                        // Pointers
                        compound_value = format!(
                            "{}{}{:\t<indentation$}{}",
                            compound_value,
                            line_feed,
                            "",
                            if let Some(first_child) = children.first() {
                                first_child.formatted_variable_value(
                                    variable_cache,
                                    indentation + 1,
                                    true,
                                )
                            } else {
                                "Unable to resolve referenced variable value".to_string()
                            }
                        );
                        compound_value
                    }
                    VariableType::Array { .. } => {
                        // Arrays
                        compound_value = format!(
                            "{}{}{:\t<indentation$}{}: {} = [",
                            compound_value,
                            line_feed,
                            "",
                            self.name,
                            self.type_name,
                        );
                        let mut child_count: usize = 0;
                        for child in children.iter() {
                            child_count += 1;
                            if child_count == children.len() {
                                // Do not add a separator at the end of the list
                                compound_value = format!(
                                    "{}{}",
                                    compound_value,
                                    child.formatted_variable_value(
                                        variable_cache,
                                        indentation + 1,
                                        false
                                    )
                                );
                            } else {
                                compound_value = format!(
                                    "{}{}, ",
                                    compound_value,
                                    child.formatted_variable_value(
                                        variable_cache,
                                        indentation + 1,
                                        false
                                    )
                                );
                            }
                        }
                        format!("{}{}{:\t<indentation$}]", compound_value, line_feed, "")
                    }
                    VariableType::Struct(name)
                        if /* name.starts_with("Some")
                            || */ name.starts_with("Ok") 
                            || name.starts_with("Err") =>
                    {
                        // Handle special structure types like the variant values of `Option<>` and `Result<>`
                        compound_value = format!(
                            "{}{:\t<indentation$}{}: {} = {}(",
                            line_feed,
                            "",
                            self.name,
                            self.type_name,
                            compound_value
                        );
                        for child in children {
                            compound_value = format!(
                                "{}{}",
                                compound_value,
                                child.formatted_variable_value(
                                    variable_cache,
                                    indentation + 1,
                                    false
                                )
                            );
                        }
                        format!("{}{}{:\t<indentation$})", compound_value, line_feed, "")
                    }
                    _ => {
                        // Generic handling of other structured types.
                        // The pre- and post- fix is determined by the type of children.
                        // compound_value = format!("{} {}", compound_value, self.type_name);

                        if children.is_empty() {
                                // Struct with no children -> just print type name
                                // This is for example the None value of an Option.

                                format!("{}{:\t<indentation$}{}", line_feed, "", self.name)
                        } else {

                        let (mut pre_fix, mut post_fix): (Option<String>, Option<String>) =
                            (None, None);

                        let mut child_count: usize = 0;

                        let mut is_tuple = false;

                        for child in children.iter() {
                            child_count += 1;
                            if pre_fix.is_none() && post_fix.is_none() {
                                if let VariableName::Named(child_name) = &child.name {
                                    if child_name.starts_with("__0") {
                                        is_tuple = true;
                                        // Treat this structure as a tuple
                                        pre_fix = Some(format!(
                                            "{}{:\t<indentation$}{}: {}({}) = {}(",
                                            line_feed,
                                            "",
                                            self.name,
                                            self.type_name,
                                            child.type_name,
                                            self.type_name,
                                        ));
                                        post_fix =
                                            Some(format!("{}{:\t<indentation$})", line_feed, ""));
                                    } else {
                                        // Treat this structure as a `struct`

                                        if show_name {
                                            pre_fix = Some(format!(
                                                "{}{:\t<indentation$}{}: {} = {} {{",
                                                line_feed,
                                                "",
                                                self.name,
                                                self.type_name,
                                                self.type_name,
                                            ));
                                        } else {
                                            pre_fix = Some(format!(
                                                "{}{:\t<indentation$}{} {{",
                                                line_feed,
                                                "",
                                                self.type_name,
                                            ));
                                        }
                                        post_fix =
                                            Some(format!("{}{:\t<indentation$}}}", line_feed, ""));
                                    }
                                };
                                if let Some(pre_fix) = &pre_fix {
                                    compound_value = format!("{}{}", compound_value, pre_fix);
                                };
                            }

                            let print_name = !is_tuple;

                            if child_count == children.len() {
                                // Do not add a separator at the end of the list
                                compound_value = format!(
                                    "{}{}",
                                    compound_value,
                                    child.formatted_variable_value(
                                        variable_cache,
                                        indentation + 1,
                                        print_name
                                    )
                                );
                            } else {
                                compound_value = format!(
                                    "{}{}, ",
                                    compound_value,
                                    child.formatted_variable_value(
                                        variable_cache,
                                        indentation + 1,
                                        print_name
                                    )
                                );
                            }
                        }
                        if let Some(post_fix) = &post_fix {
                            compound_value = format!("{}{}", compound_value, post_fix);
                        };
                        compound_value
                        }
                    }
                }
            } else {
                // We don't have a value, and we can't generate one from children values, so use the type_name
                format!("{:\t<indentation$}{}", "", self.type_name)
            }
        }
    }
}

/// Traits and Impl's to read from, and write to, memory value based on Variable::typ and Variable::location.
trait Value {
    /// The MS DAP protocol passes the value as a string, so this trait is here to provide the memory read logic before returning it as a string.
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError>
    where
        Self: Sized;

    /// This `update_value` will update the target memory with a new value for the [`Variable`], ...
    /// - Only `base` data types can have their value updated in target memory.
    /// - The input format of the [Variable.value] is a [String], and the impl of this trait must convert the memory value appropriately before storing.
    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError>;
}

impl Value for bool {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mem_data = core.read_word_8(variable.memory_location.memory_address()?)?;
        let ret_value: bool = mem_data != 0;
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        core.write_word_8(
            variable.memory_location.memory_address()?,
            <bool as FromStr>::from_str(new_value).map_err(|error| {
                DebugError::Other(anyhow::anyhow!(
                    "Invalid data conversion from value: {:?}. {:?}",
                    new_value,
                    error
                ))
            })? as u8,
        )
        .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for char {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mem_data = core.read_word_32(variable.memory_location.memory_address()?)?;
        if let Some(return_value) = char::from_u32(mem_data) {
            Ok(return_value)
        } else {
            Ok('?')
        }
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        core.write_word_32(
            variable.memory_location.memory_address()?,
            <char as FromStr>::from_str(new_value).map_err(|error| {
                DebugError::Other(anyhow::anyhow!(
                    "Invalid data conversion from value: {:?}. {:?}",
                    new_value,
                    error
                ))
            })? as u32,
        )
        .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for String {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut str_value: String = "".to_owned();
        if let Ok(children) = variable_cache.get_children(Some(variable.variable_key)) {
            if !children.is_empty() {
                let mut string_length = match children.iter().find(|child_variable| {
                    child_variable.name == VariableName::Named("length".to_string())
                }) {
                    Some(string_length) => {
                        if let VariableValue::Valid(length_value) = &string_length.value {
                            length_value.parse().unwrap_or(0_usize)
                        } else {
                            0_usize
                        }
                    }
                    None => 0_usize,
                };
                let string_location = match children.iter().find(|child_variable| {
                    child_variable.name == VariableName::Named("data_ptr".to_string())
                }) {
                    Some(location_value) => {
                        if let Ok(child_variables) =
                            variable_cache.get_children(Some(location_value.variable_key))
                        {
                            if let Some(first_child) = child_variables.first() {
                                first_child.memory_location.memory_address()?
                            } else {
                                0_u64
                            }
                        } else {
                            0_u64
                        }
                    }
                    None => 0_u64,
                };
                if string_location.is_zero() {
                    str_value = "Error: Failed to determine &str memory location".to_string();
                } else {
                    // Limit string length to work around buggy information, otherwise the debugger
                    // can hang due to buggy debug information.
                    //
                    // TODO: If implemented, the variable should not be fetched automatically,
                    // but only when requested by the user. This workaround can then be removed.
                    if string_length > 200 {
                        log::warn!(
                            "Very long string ({} bytes), truncating to 200 bytes.",
                            string_length
                        );
                        string_length = 200;
                    }

                    if string_length.is_zero() {
                        // A string with length 0 doesn't need to be read from memory.
                    } else {
                        let mut buff = vec![0u8; string_length];
                        core.read(string_location, &mut buff)?;
                        str_value = core::str::from_utf8(&buff)?.to_owned();
                    }
                }
            } else {
                str_value = "Error: Failed to evaluate &str value".to_string();
            }
        };
        Ok(str_value)
    }

    fn update_value(
        _variable: &Variable,
        _core: &mut Core<'_>,
        _new_value: &str,
    ) -> Result<(), DebugError> {
        Err(DebugError::Other(anyhow::anyhow!(
            "Unsupported datatype: \"String\". Please only update variables with a base data type.",
        )))
    }
}
impl Value for i8 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 1];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = i8::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        core.write_word_8(
            variable.memory_location.memory_address()?,
            <i8 as FromStr>::from_str(new_value).map_err(|error| {
                DebugError::Other(anyhow::anyhow!(
                    "Invalid data conversion from value: {:?}. {:?}",
                    new_value,
                    error
                ))
            })? as u8,
        )
        .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for i16 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 2];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = i16::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = i16::to_le_bytes(<i16 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for i32 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 4];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = i32::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = i32::to_le_bytes(<i32 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for i64 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 8];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = i64::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = i64::to_le_bytes(<i64 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for i128 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 16];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = i128::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = i128::to_le_bytes(<i128 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for isize {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 4];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        // TODO: We can get the actual WORD length from [DWARF] instead of assuming `u32`
        let ret_value = i32::from_le_bytes(buff);
        Ok(ret_value as isize)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff =
            isize::to_le_bytes(<isize as FromStr>::from_str(new_value).map_err(|error| {
                DebugError::Other(anyhow::anyhow!(
                    "Invalid data conversion from value: {:?}. {:?}",
                    new_value,
                    error
                ))
            })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for u8 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 1];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = u8::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        core.write_word_8(
            variable.memory_location.memory_address()?,
            <u8 as FromStr>::from_str(new_value).map_err(|error| {
                DebugError::Other(anyhow::anyhow!(
                    "Invalid data conversion from value: {:?}. {:?}",
                    new_value,
                    error
                ))
            })? as u8,
        )
        .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for u16 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 2];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = u16::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = u16::to_le_bytes(<u16 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for u32 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 4];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = u32::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = u32::to_le_bytes(<u32 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for u64 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 8];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = u64::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = u64::to_le_bytes(<u64 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for u128 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 16];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = u128::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = u128::to_le_bytes(<u128 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for usize {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 4];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        // TODO: We can get the actual WORD length from [DWARF] instead of assuming `u32`
        let ret_value = u32::from_le_bytes(buff);
        Ok(ret_value as usize)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff =
            usize::to_le_bytes(<usize as FromStr>::from_str(new_value).map_err(|error| {
                DebugError::Other(anyhow::anyhow!(
                    "Invalid data conversion from value: {:?}. {:?}",
                    new_value,
                    error
                ))
            })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for f32 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 4];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = f32::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = f32::to_le_bytes(<f32 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}
impl Value for f64 {
    fn get_value(
        variable: &Variable,
        core: &mut Core<'_>,
        _variable_cache: &variable_cache::VariableCache,
    ) -> Result<Self, DebugError> {
        let mut buff = [0u8; 8];
        core.read(variable.memory_location.memory_address()?, &mut buff)?;
        let ret_value = f64::from_le_bytes(buff);
        Ok(ret_value)
    }

    fn update_value(
        variable: &Variable,
        core: &mut Core<'_>,
        new_value: &str,
    ) -> Result<(), DebugError> {
        let buff = f64::to_le_bytes(<f64 as FromStr>::from_str(new_value).map_err(|error| {
            DebugError::Other(anyhow::anyhow!(
                "Invalid data conversion from value: {:?}. {:?}",
                new_value,
                error
            ))
        })?);
        core.write_8(variable.memory_location.memory_address()?, &buff)
            .map_err(|error| DebugError::Other(anyhow::anyhow!("{:?}", error)))
    }
}