-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathas_debugger.cpp
1272 lines (1030 loc) · 41.5 KB
/
as_debugger.cpp
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
// MIT Licensed
// see https://github.com/Paril/angelscript-debugger
#include "as_debugger.h"
#include <array>
#include <bitset>
#include <charconv>
void asIDBVariable::Evaluate()
{
if (evaluated)
return;
// getters don't evaluate and are
// just placeholders, but they need
// a ref ID.
else if (getter)
{
SetRefId();
return;
}
auto var = ptr.lock();
dbg.cache->GetEvaluator(var->address).Evaluate(var);
evaluated = true;
if (expandable)
SetRefId();
}
void asIDBVariable::SetRefId()
{
if (expandRefId.has_value())
return;
auto &refs = dbg.cache->variable_refs;
int64_t next_id = refs.size() + 1;
expandRefId = next_id;
refs.emplace(next_id, ptr);
}
void asIDBVariable::Expand()
{
Evaluate();
if (expanded)
return;
else if (!expandRefId)
return;
expanded = true;
auto var = ptr.lock();
if (!getter)
{
dbg.cache->GetEvaluator(var->address).Expand(var);
return;
}
// getters are a bit special; we have to fetch the variable
// that our getter is linked to, & store the result in stack memory.
auto ctx = dbg.cache->ctx;
dbg.internal_execution = true;
ctx->PushState();
ctx->Prepare(getter);
ctx->SetObject(this->owner.lock()->address.ResolveAs<void>());
ctx->Execute();
var->namedProps.clear();
var->indexedProps.clear();
if (ctx->GetState() != asEXECUTION_FINISHED)
{
var->get_evaluated = var->CreateChildVariable(var->identifier, {}, "");
var->get_evaluated->value = fmt::format("Exception thrown ({})", ctx->GetExceptionString());
var->get_evaluated->evaluated = true;
}
else
{
asDWORD returnFlags;
int typeId = getter->GetReturnTypeId(&returnFlags);
asIDBValue returnValue(ctx->GetEngine(), ctx->GetAddressOfReturnValue(), typeId,
(returnFlags & asTM_INOUTREF) != 0);
asIDBVariable::Ptr child =
var->CreateChildVariable(var->identifier, { typeId, (returnFlags & asTM_CONST) != 0, nullptr },
dbg.cache->GetTypeNameFromType({ typeId, (asETypeModifiers) returnFlags }));
child->stackValue = std::move(returnValue);
child->address.address = child->stackValue.GetPointer<void>(true);
}
ctx->PopState();
dbg.internal_execution = false;
}
asIDBVariable::Ptr asIDBVariable::CreateChildVariable(asIDBVarName identifier, asIDBVarAddr address,
std::string_view typeName)
{
asIDBVariable::Ptr child = dbg.cache->CreateVariable();
child->owner = ptr;
child->identifier = identifier;
child->address = address;
child->typeName = typeName;
if (identifier.name[0] == '[')
indexedProps.push_back(child);
else
namedProps.insert(child);
return child;
}
asIDBScope::asIDBScope(asUINT offset, asIDBDebugger &dbg, asIScriptFunction *function) :
offset(offset),
parameters(dbg.cache->CreateVariable()),
locals(dbg.cache->CreateVariable()),
registers(dbg.cache->CreateVariable())
{
CalcLocals(dbg, function, parameters);
CalcLocals(dbg, function, locals);
CalcLocals(dbg, function, registers);
}
void asIDBScope::CalcLocals(asIDBDebugger &dbg, asIScriptFunction *function, asIDBVariable::Ptr &container)
{
if (!function || offset == SCOPE_SYSTEM)
return;
auto &cache = *dbg.cache.get();
auto ctx = cache.ctx;
asUINT numParams = function->GetParamCount();
asUINT numLocals = ctx->GetVarCount(offset);
asUINT start = 0, end = 0;
if (container == parameters)
end = numParams;
else
{
start = numParams;
end = numLocals;
}
if (container == locals)
{
if (auto thisPtr = ctx->GetThisPointer(offset))
{
int thisTypeId = ctx->GetThisTypeId(offset);
asIDBTypeId typeKey { thisTypeId, asTM_NONE };
const std::string_view viewType = cache.GetTypeNameFromType(typeKey);
asIDBVarAddr idKey { thisTypeId, false, thisPtr };
asIDBVariable::Ptr var = container->CreateChildVariable("this", idKey, viewType);
this_ptr = var;
}
}
for (asUINT n = start; n < end; n++)
{
const char *name;
int typeId;
asETypeModifiers modifiers;
int stackOffset;
ctx->GetVar(n, offset, &name, &typeId, &modifiers, 0, &stackOffset);
bool isTemporary = (container != parameters) && (!name || !*name);
if (!ctx->IsVarInScope(n, offset))
continue;
else if (isTemporary != (container == registers))
continue;
void *ptr = ctx->GetAddressOfVar(n, offset);
asIDBTypeId typeKey { typeId, modifiers };
std::string localName = (name && *name) ? fmt::format("{} (&{})", name, n) : fmt::format("&{}", n);
const std::string_view viewType = cache.GetTypeNameFromType(typeKey);
asIDBVarAddr idKey { typeId, (modifiers & asTM_CONST) != 0, ptr };
asIDBVariable::Ptr var = container->CreateChildVariable(std::move(localName), idKey, viewType);
local_by_index.emplace(n, var);
}
container->evaluated = container->expanded = true;
if (!container->namedProps.empty() ||
!container->indexedProps.empty())
container->SetRefId();
}
/*virtual*/ void asIDBCache::Refresh()
{
}
/*virtual*/ const std::string_view asIDBCache::GetTypeNameFromType(asIDBTypeId id)
{
if (auto f = type_names.find(id); f != type_names.end())
return f->second.c_str();
auto type = ctx->GetEngine()->GetTypeInfoById(id.typeId);
const char *rawName = "???";
if (!type)
{
// a primitive
switch (id.typeId & asTYPEID_MASK_SEQNBR)
{
case asTYPEID_BOOL: rawName = "bool"; break;
case asTYPEID_INT8: rawName = "int8"; break;
case asTYPEID_INT16: rawName = "int16"; break;
case asTYPEID_INT32: rawName = "int32"; break;
case asTYPEID_INT64: rawName = "int64"; break;
case asTYPEID_UINT8: rawName = "uint8"; break;
case asTYPEID_UINT16: rawName = "uint16"; break;
case asTYPEID_UINT32: rawName = "uint32"; break;
case asTYPEID_UINT64: rawName = "uint64"; break;
case asTYPEID_FLOAT: rawName = "float"; break;
case asTYPEID_DOUBLE: rawName = "double"; break;
default: rawName = "???"; break;
}
}
else
{
rawName = type->GetName();
}
std::string name = fmt::format("{}{}{}{}", (id.modifiers & asTM_CONST) ? "const " : "", rawName,
(id.typeId & (asTYPEID_HANDLETOCONST | asTYPEID_OBJHANDLE)) ? "@" : "",
((id.modifiers & asTM_INOUTREF) == asTM_INOUTREF) ? "&"
: ((id.modifiers & asTM_INOUTREF) == asTM_INREF) ? "&in"
: ((id.modifiers & asTM_INOUTREF) == asTM_OUTREF) ? "&out"
: "");
return type_names.emplace(id, std::move(name)).first->second;
}
void *asIDBCache::ResolvePropertyAddress(const asIDBVarAddr &id, int propertyIndex, int offset, int compositeOffset,
bool isCompositeIndirect)
{
if (id.typeId & asTYPEID_SCRIPTOBJECT)
{
asIScriptObject *obj = id.ResolveAs<asIScriptObject>();
return obj->GetAddressOfProperty(propertyIndex);
}
// indirect changes our ptr to
// *(object + compositeOffset) + offset
if (isCompositeIndirect)
{
void *propAddr = *reinterpret_cast<uint8_t **>(id.ResolveAs<uint8_t>() + compositeOffset);
// if we're null, leave it alone, otherwise point to
// where we really need to be pointing
if (propAddr)
propAddr = reinterpret_cast<uint8_t *>(propAddr) + offset;
return propAddr;
}
return id.ResolveAs<uint8_t>() + offset + compositeOffset;
}
/*virtual*/ asIDBExpected<asIDBVariable::WeakPtr> asIDBCache::ResolveExpression(std::string_view expr,
std::optional<int> stack_index)
{
// just in case your IDE sends `@ent` or `&ent` for a hover
if (!expr.empty() && (expr[0] == '@' || (expr.size() >= 2 && expr[0] == '&' && !isdigit(expr[1]))))
expr.remove_prefix(1);
if (expr.empty())
return asIDBExpected("empty string");
CacheCallstack();
// isolate the variable name first
size_t variable_end = expr.find_first_of(".[", 0);
std::string_view variable_name = expr.substr(0, variable_end);
if (variable_name.empty())
return asIDBExpected("bad expression");
asIDBExpected<asIDBVariable::WeakPtr> variable;
asIDBCallStackEntry *stack = nullptr;
if (stack_index.has_value())
stack = &call_stack[stack_index.value()];
// if it starts with a & it has to be a local variable index
if (stack && variable_name[0] == '&')
{
uint32_t offset;
auto result = std::from_chars(&variable_name.front(), &variable_name.front() + variable_name.size(), offset);
if (result.ec != std::errc())
return asIDBExpected("invalid numerical offset");
// check bounds
int m = ctx->GetVarCount(stack_index.value());
if (m < 0)
return asIDBExpected("bad stack index");
if (offset >= (asUINT) m)
return asIDBExpected("stack offset out of bounds");
if (!ctx->IsVarInScope(offset, stack_index.value()))
return asIDBExpected("variable out of scope");
if (auto varit = stack->scope.local_by_index.find(offset); varit != stack->scope.local_by_index.end())
variable = varit->second;
else
return asIDBExpected("missing local index");
}
// check this
else if (stack && variable_name == "this")
{
if (stack->scope.this_ptr.expired())
return asIDBExpected("not a method");
variable = stack->scope.this_ptr;
}
else
{
struct asIDBNamespacedVar
{
asIDBVariable::WeakPtr var;
std::string_view name;
std::string_view ns;
};
std::vector<asIDBNamespacedVar> matches;
std::string_view variable_ns;
if (auto ns_end = variable_name.find_last_of(':'); ns_end != std::string_view::npos)
{
variable_ns = variable_name.substr(0, ns_end - 1);
variable_name = variable_name.substr(ns_end + 1);
}
if (stack)
{
// not an offset; in order, check the following:
// - local variables (in reverse order)
// - function parameters
// - class member properties (if appropriate)
// - globals
for (int i = ctx->GetVarCount(stack_index.value()) - 1; i >= 0; i--)
{
if (!ctx->IsVarInScope(i, stack_index.value()))
continue;
const char *name;
int typeId;
asETypeModifiers modifiers;
ctx->GetVar(i, stack_index.value(), &name, &typeId, &modifiers);
if (variable_name != name)
continue;
if (auto varit = stack->scope.local_by_index.find(i); varit != stack->scope.local_by_index.end())
matches.push_back({ varit->second, name });
break;
}
// check `this` parameters
if (!stack->scope.this_ptr.expired())
{
auto var = stack->scope.this_ptr.lock();
var->Expand();
for (auto ¶m : var->namedProps)
if (variable_name == param->identifier.name)
matches.push_back({ param, param->identifier.name });
for (auto ¶m : var->indexedProps)
if (variable_name == param->identifier.name)
matches.push_back({ param, param->identifier.name });
}
}
// check globals
CacheGlobals();
for (auto &global : globals->namedProps)
if (variable_name == global->identifier.name)
matches.push_back({ global, global->identifier.name, global->identifier.ns });
for (auto &global : globals->indexedProps)
if (variable_name == global->identifier.name)
matches.push_back({ global, global->identifier.name, global->identifier.ns });
if (matches.size() == 1)
variable = matches[0].var;
// if we didn't specify a ns but had multiple
// matches, return an error
else if (variable_ns.empty())
return asIDBExpected(matches.empty() ? "can't find variable" : "ambiguous variable name");
else
{
for (auto &match : matches)
{
if (variable_ns == match.ns)
{
variable = match.var;
break;
}
}
}
if (!variable)
return asIDBExpected("can't find variable");
}
// variable_key should be non-null and with
// a valid type ID here.
return ResolveSubExpression(variable.value(), variable_end == std::string_view::npos ? std::string_view {}
: expr.substr(variable_end));
}
/*virtual*/ asIDBExpected<asIDBVariable::WeakPtr> asIDBCache::ResolveSubExpression(asIDBVariable::WeakPtr var,
const std::string_view rest)
{
// nothing left, so this is the result.
if (rest.empty())
return var;
// make sure we're a type that supports properties
auto varp = var.lock();
varp->Evaluate();
if (!varp->expandRefId)
return asIDBExpected("invalid expression");
varp->Expand();
if (varp->namedProps.empty() && varp->indexedProps.empty())
return asIDBExpected("no members");
// check what kind of sub-evaluator to use
size_t eval_start = rest.find_first_of(".[", 1);
std::string_view eval_name = rest.substr(0, eval_start);
if (eval_name[0] == '.')
eval_name.remove_prefix(1);
for (auto &child : varp->namedProps)
{
if (child->identifier.name == eval_name)
return ResolveSubExpression(child, eval_start == std::string_view::npos ? std::string_view {}
: rest.substr(eval_start));
}
for (auto &child : varp->indexedProps)
{
if (child->identifier.name == eval_name)
return ResolveSubExpression(child, eval_start == std::string_view::npos ? std::string_view {}
: rest.substr(eval_start));
}
return asIDBExpected("can't resolve sub-expression");
}
/*virtual*/ void asIDBCache::CacheCallstack()
{
if (!ctx || !call_stack.empty())
return;
if (auto sysfunc = ctx->GetSystemFunction())
call_stack.emplace_back(asIDBCallStackEntry { dbg.frame_offset++, sysfunc->GetDeclaration(true, false, true),
"(system function)", 0, 0,
asIDBScope(SCOPE_SYSTEM, dbg, sysfunc) });
for (asUINT n = 0; n < ctx->GetCallstackSize(); n++)
{
asIScriptFunction *func = nullptr;
int column = 0;
const char *section = "";
int row = 0;
// FIXME: check this, because this will skip GetFunction(n).
// I think this is correct though...?
if (n == 0 && ctx->GetState() == asEXECUTION_EXCEPTION)
{
func = ctx->GetExceptionFunction();
if (func)
row = ctx->GetExceptionLineNumber(&column, §ion);
}
else
{
func = ctx->GetFunction(n);
if (func)
row = ctx->GetLineNumber(n, &column, §ion);
}
std::string decl;
if (func)
decl = func->GetDeclaration(true, false, true);
else
decl = "???"; // FIXME: why does this happen?
call_stack.push_back(
asIDBCallStackEntry { dbg.frame_offset++, std::move(decl), section, row, column,
asIDBScope(func->GetFuncType() == asFUNC_SYSTEM ? SCOPE_SYSTEM : n, dbg, func) });
}
}
// restore data from the given cache that is
// being replaced by this one.
/*virtual*/ void asIDBCache::Restore(asIDBCache &cache)
{
}
/*virtual*/ void asIDBCache::CacheGlobals()
{
if (!ctx)
return;
if (!globals)
globals = CreateVariable();
if (globals->expanded)
return;
auto main = ctx->GetFunction(0)->GetModule();
for (asUINT n = 0; n < main->GetGlobalVarCount(); n++)
{
const char *name;
const char *nameSpace;
int typeId;
void *ptr;
bool isConst;
main->GetGlobalVar(n, &name, &nameSpace, &typeId, &isConst);
ptr = main->GetAddressOfGlobalVar(n);
asIDBTypeId typeKey { typeId, isConst ? asTM_CONST : asTM_NONE };
const std::string_view viewType = GetTypeNameFromType(typeKey);
asIDBVarAddr idKey { typeId, isConst, ptr };
globals->CreateChildVariable(asIDBVarName((nameSpace && nameSpace[0]) ? nameSpace : "", name), idKey, viewType);
}
for (asUINT n = 0; n < main->GetEngine()->GetGlobalPropertyCount(); n++)
{
const char *name;
const char *nameSpace;
int typeId;
void *ptr;
bool isConst;
main->GetEngine()->GetGlobalPropertyByIndex(n, &name, &nameSpace, &typeId, &isConst, nullptr, &ptr);
asIDBTypeId typeKey { typeId, isConst ? asTM_CONST : asTM_NONE };
const std::string_view viewType = GetTypeNameFromType(typeKey);
asIDBVarAddr idKey { typeId, isConst, ptr };
std::string localName = (nameSpace && nameSpace[0]) ? fmt::format("{}::{}", nameSpace, name) : name;
globals->CreateChildVariable(std::move(localName), idKey, viewType);
}
globals->evaluated = globals->expanded = true;
if (!globals->namedProps.empty() ||
!globals->indexedProps.empty())
globals->SetRefId();
}
class asIDBNullTypeEvaluator : public asIDBTypeEvaluator
{
public:
virtual void Evaluate(asIDBVariable::Ptr var) const override
{
var->value = "(null)";
}
};
class asIDBUninitTypeEvaluator : public asIDBTypeEvaluator
{
public:
virtual void Evaluate(asIDBVariable::Ptr var) const override
{
var->value = "(uninit)";
}
};
class asIDBEnumTypeEvaluator : public asIDBTypeEvaluator
{
public:
virtual void Evaluate(asIDBVariable::Ptr var) const override
{
auto &dbg = var->dbg;
// for enums where we have a single matched value
// just display it directly; it might be a mask but that's OK.
auto type = dbg.cache->ctx->GetEngine()->GetTypeInfoById(var->address.typeId);
union {
asINT64 v = 0;
asQWORD uv;
};
switch (type->GetTypedefTypeId())
{
case asTYPEID_INT8: v = *var->address.ResolveAs<const int8_t>(); break;
case asTYPEID_UINT8: uv = *var->address.ResolveAs<const uint8_t>(); break;
case asTYPEID_INT16: v = *var->address.ResolveAs<const int16_t>(); break;
case asTYPEID_UINT16: uv = *var->address.ResolveAs<const uint16_t>(); break;
case asTYPEID_INT32: v = *var->address.ResolveAs<const int32_t>(); break;
case asTYPEID_UINT32: uv = *var->address.ResolveAs<const uint32_t>(); break;
case asTYPEID_INT64: v = *var->address.ResolveAs<const int64_t>(); break;
case asTYPEID_UINT64: uv = *var->address.ResolveAs<const uint64_t>(); break;
}
for (asUINT e = 0; e < type->GetEnumValueCount(); e++)
{
asINT64 ov = 0;
const char *name = type->GetEnumValueByIndex(e, &ov);
if (ov == v)
{
if (type->GetTypedefTypeId() >= asTYPEID_UINT8 && type->GetTypedefTypeId() <= asTYPEID_UINT64)
{
var->value = fmt::format("{} ({})", name, uv);
return;
}
var->value = fmt::format("{} ({})", name, v);
return;
}
}
std::bitset<32> bits(v);
if (bits.count() == 1)
{
if (type->GetTypedefTypeId() >= asTYPEID_UINT8 && type->GetTypedefTypeId() <= asTYPEID_UINT64)
{
var->value = fmt::format("{}", uv);
return;
}
var->value = fmt::format("{}", v);
return;
}
var->value = fmt::format("{} bits", bits.count());
var->expandable = true;
}
virtual void Expand(asIDBVariable::Ptr var) const override
{
auto &dbg = var->dbg;
auto &cache = *dbg.cache;
auto type = cache.ctx->GetEngine()->GetTypeInfoById(var->address.typeId);
union {
asINT64 v = 0;
asQWORD uv;
};
switch (type->GetTypedefTypeId())
{
case asTYPEID_INT8: v = *var->address.ResolveAs<const int8_t>(); break;
case asTYPEID_UINT8: uv = *var->address.ResolveAs<const uint8_t>(); break;
case asTYPEID_INT16: v = *var->address.ResolveAs<const int16_t>(); break;
case asTYPEID_UINT16: uv = *var->address.ResolveAs<const uint16_t>(); break;
case asTYPEID_INT32: v = *var->address.ResolveAs<const int32_t>(); break;
case asTYPEID_UINT32: uv = *var->address.ResolveAs<const uint32_t>(); break;
case asTYPEID_INT64: v = *var->address.ResolveAs<const int64_t>(); break;
case asTYPEID_UINT64: uv = *var->address.ResolveAs<const uint64_t>(); break;
}
{
std::string rawValue;
if (type->GetTypedefTypeId() >= asTYPEID_UINT8 && type->GetTypedefTypeId() <= asTYPEID_UINT64)
rawValue = fmt::format("{}", uv);
else
rawValue = fmt::format("{}", v);
auto child = var->CreateChildVariable("value", {}, "");
child->value = std::move(rawValue);
child->evaluated = true;
}
// find bit names
asINT64 ov = 0;
std::array<const char *, sizeof(ov) * 8> bit_names {};
for (asUINT e = 0; e < type->GetEnumValueCount(); e++)
{
const char *name = type->GetEnumValueByIndex(e, &ov);
std::bitset<sizeof(ov) * 8> obits(ov);
// skip masks
if (obits.count() != 1)
continue;
if (ov & v)
{
int p = 0;
while (ov && !(ov & 1))
{
ov >>= 1;
p++;
}
// only take the first name, just incase
// there's later overrides
if (p <= (obits.size() - 1) && !bit_names[p])
bit_names[p] = name;
}
}
// display bits
for (asQWORD e = 0; e < bit_names.size(); e++)
{
if (v & (1ull << e))
{
std::string bitEntry;
if (bit_names[e])
bitEntry = bit_names[e];
else
bitEntry = fmt::format("{}", 1 << e);
auto child = var->CreateChildVariable(fmt::format("[{:{}}]", e, type->GetSize() == 1 ? 1 : 2), {}, "");
child->value = std::move(bitEntry);
child->evaluated = true;
}
}
}
};
class asIDBFuncDefTypeEvaluator : public asIDBTypeEvaluator
{
public:
virtual void Evaluate(asIDBVariable::Ptr var) const override
{
asIScriptFunction *ptr = var->address.ResolveAs<asIScriptFunction>();
auto &dbg = var->dbg;
var->value = ptr->GetName();
}
};
/*virtual*/ void asIDBObjectTypeEvaluator::Evaluate(asIDBVariable::Ptr var) const /*override*/
{
auto &dbg = var->dbg;
auto &cache = *dbg.cache;
auto ctx = cache.ctx;
auto type = ctx->GetEngine()->GetTypeInfoById(var->address.typeId);
var->expandable = CanExpand(var);
if (ctx->GetState() != asEXECUTION_EXCEPTION)
{
asIDBObjectIteratorHelper it(type, var->address.ResolveAs<void>());
if (!it)
{
if (!it.error.empty())
{
var->value = std::string(it.error);
return;
}
if (var->value.empty())
var->value = fmt::format("{{{}}}", var->typeName);
}
else
{
dbg.internal_execution = true;
size_t numElements = it.CalculateLength(ctx);
dbg.internal_execution = false;
if (var->value.empty())
var->value = fmt::format("{} elements", numElements);
if (numElements)
var->expandable = true;
}
}
}
/*virtual*/ void asIDBObjectTypeEvaluator::Expand(asIDBVariable::Ptr var) const /*override*/
{
QueryVariableProperties(var);
QueryVariableGetters(var);
QueryVariableForEach(var);
}
// convenience function that queries the properties of the given
// address (and object, if set) of the given type.
void asIDBObjectTypeEvaluator::QueryVariableProperties(asIDBVariable::Ptr var) const
{
auto &dbg = var->dbg;
auto &cache = *dbg.cache;
auto type = cache.ctx->GetEngine()->GetTypeInfoById(var->address.typeId);
for (asUINT n = 0; n < type->GetPropertyCount(); n++)
{
const char *name;
int propTypeId;
void *propAddr = nullptr;
int offset;
int compositeOffset;
bool isCompositeIndirect;
bool isReadOnly;
type->GetProperty(n, &name, &propTypeId, 0, 0, &offset, 0, 0, &compositeOffset, &isCompositeIndirect,
&isReadOnly);
propAddr = cache.ResolvePropertyAddress(var->address, n, offset, compositeOffset, isCompositeIndirect);
asIDBVarAddr propId { propTypeId, isReadOnly, propAddr };
// TODO: variables that overlap memory space will
// get culled by this. this helps in the case of
// vec3_t::x and vec3_t::pitch for instance, but
// causes some confusion for edict_t::number and
// edict_t::s::number, where `s` is now just an empty
// struct. it'd be ideal if, in this case, it prefers
// the deeper nested ones. not sure how we'd express that
// with the limited context we have, though.
// TODO 2.0: this causes an issue with Watch variables
// because of the way dereferencing works. For now, it
// will add duplicates, and the old var state cache is gone.
var->CreateChildVariable(name, propId,
cache.GetTypeNameFromType({ propTypeId, isReadOnly ? asTM_CONST : asTM_NONE }));
}
}
// convenience function that queries for getter property functions.
void asIDBObjectTypeEvaluator::QueryVariableGetters(asIDBVariable::Ptr var) const
{
auto &dbg = var->dbg;
auto &cache = *dbg.cache;
auto type = cache.ctx->GetEngine()->GetTypeInfoById(var->address.typeId);
for (asUINT n = 0; n < type->GetMethodCount(); n++)
{
asIScriptFunction *function = type->GetMethodByIndex(n, true);
if (!IsCompatibleGetter(function))
continue;
auto child = var->CreateChildVariable(std::string(std::string_view(function->GetName()).substr(4)), {},
cache.GetTypeNameFromType({ function->GetReturnTypeId(), asTM_NONE }));
child->getter = function;
child->Evaluate();
}
}
bool asIDBObjectTypeEvaluator::CanExpand(asIDBVariable::Ptr var) const
{
auto &dbg = var->dbg;
auto &cache = *dbg.cache;
auto type = cache.ctx->GetEngine()->GetTypeInfoById(var->address.typeId);
if (type->GetPropertyCount())
return true;
for (asUINT n = 0; n < type->GetMethodCount(); n++)
{
asIScriptFunction *function = type->GetMethodByIndex(n, true);
if (IsCompatibleGetter(function))
return true;
}
return false;
}
bool asIDBObjectTypeEvaluator::IsCompatibleGetter(asIScriptFunction *function) const
{
return function->IsReadOnly() && function->IsProperty() && function->GetParamCount() == 0;
}
// convenience function that iterates the opFor* of the given
// address (and object, if set) of the given type. If positive,
// a specific index will be used.
void asIDBObjectTypeEvaluator::QueryVariableForEach(asIDBVariable::Ptr var, int index) const
{
auto &dbg = var->dbg;
auto &cache = *dbg.cache;
auto ctx = cache.ctx;
if (ctx->GetState() == asEXECUTION_EXCEPTION)
return;
auto type = ctx->GetEngine()->GetTypeInfoById(var->address.typeId);
dbg.internal_execution = true;
asIDBObjectIteratorHelper it(type, var->address.ResolveAs<void>());
if (!it)
{
dbg.internal_execution = false;
return;
}
ctx->PushState();
auto itValue = it.Begin(ctx);
int elementId = 0;
bool multiElement = index == -1 && it.opForValues.size() > 1;
while (true)
{
if (it.End(ctx, itValue))
break;
asIDBVariable::Ptr indexVar;
// if we're a multi-element, the root is fake
// and just exists to store the element id.
if (multiElement)
{
indexVar = var->CreateChildVariable(fmt::format("[{}]", elementId), {},
"" // FIXME: could show types as tuple?
);
indexVar->expanded = indexVar->evaluated = true;
}
for (int offset = (index == -1 ? 0 : index), visibleOffset = 0;
offset < (index == -1 ? it.opForValues.size() : index + 1); offset++, visibleOffset++)
{
it.Value(ctx, itValue, offset);
asDWORD returnFlags;
int typeId = it.opForValues[offset]->GetReturnTypeId(&returnFlags);
asIDBValue returnValue(ctx->GetEngine(), ctx->GetAddressOfReturnValue(), typeId,
(returnFlags & asTM_INOUTREF) != 0);
auto child =
(multiElement ? indexVar : var)
->CreateChildVariable(fmt::format("[{}]", multiElement ? visibleOffset : elementId),
{ typeId, (returnFlags & asTM_CONST) != 0, nullptr },
dbg.cache->GetTypeNameFromType({ typeId, (asETypeModifiers) returnFlags }));
child->stackValue = std::move(returnValue);
child->address.address = child->stackValue.GetPointer<void>(true);
}
itValue = it.Next(ctx, itValue);
elementId++;
}
ctx->PopState();
cache.dbg.internal_execution = false;
}
const asIDBTypeEvaluator &asIDBCache::GetEvaluator(const asIDBVarAddr &id) const
{
// the only way the base address is null is if
// it's uninitialized.
static constexpr const asIDBUninitTypeEvaluator uninitType;
static constexpr const asIDBNullTypeEvaluator nullType;
if (id.address == nullptr)
return uninitType;
else if (id.ResolveAs<void>() == nullptr)
return nullType;
auto type = ctx->GetEngine()->GetTypeInfoById(id.typeId);
// we'll use the fall back evaluators.
// check primitives first.
#define CHECK_PRIMITIVE_EVAL(asTypeId, cTypeName) \
if (id.typeId == asTypeId) \
{ \
static constexpr const asIDBPrimitiveTypeEvaluator<cTypeName> cTypeName##Type; \
return cTypeName##Type; \
}
CHECK_PRIMITIVE_EVAL(asTYPEID_BOOL, bool);
CHECK_PRIMITIVE_EVAL(asTYPEID_INT8, int8_t);
CHECK_PRIMITIVE_EVAL(asTYPEID_INT16, int16_t);
CHECK_PRIMITIVE_EVAL(asTYPEID_INT32, int32_t);
CHECK_PRIMITIVE_EVAL(asTYPEID_INT64, int64_t);
CHECK_PRIMITIVE_EVAL(asTYPEID_UINT8, uint8_t);
CHECK_PRIMITIVE_EVAL(asTYPEID_UINT16, uint16_t);