-
Notifications
You must be signed in to change notification settings - Fork 182
/
ctrl_core.c
5362 lines (4944 loc) · 200 KB
/
ctrl_core.c
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
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
////////////////////////////////
//~ rjf: Generated Code
#include "generated/ctrl.meta.c"
////////////////////////////////
//~ rjf: Basic Type Functions
internal U64
ctrl_hash_from_string(String8 string)
{
U64 result = 5381;
for(U64 i = 0; i < string.size; i += 1)
{
result = ((result << 5) + result) + string.str[i];
}
return result;
}
internal U64
ctrl_hash_from_machine_id_handle(CTRL_MachineID machine_id, DMN_Handle handle)
{
U64 buf[] = {machine_id, handle.u64[0]};
U64 hash = ctrl_hash_from_string(str8((U8 *)buf, sizeof(buf)));
return hash;
}
internal CTRL_EventCause
ctrl_event_cause_from_dmn_event_kind(DMN_EventKind event_kind)
{
CTRL_EventCause cause = CTRL_EventCause_Null;
switch(event_kind)
{
default:{}break;
case DMN_EventKind_Error: {cause = CTRL_EventCause_Error;}break;
case DMN_EventKind_Exception:{cause = CTRL_EventCause_InterruptedByException;}break;
case DMN_EventKind_Trap: {cause = CTRL_EventCause_InterruptedByTrap;}break;
case DMN_EventKind_Halt: {cause = CTRL_EventCause_InterruptedByHalt;}break;
}
return cause;
}
internal String8
ctrl_string_from_event_kind(CTRL_EventKind kind)
{
String8 result = {0};
switch(kind)
{
default:{}break;
case CTRL_EventKind_Null: { result = str8_lit("Null");}break;
case CTRL_EventKind_Error: { result = str8_lit("Error");}break;
case CTRL_EventKind_Started: { result = str8_lit("Started");}break;
case CTRL_EventKind_Stopped: { result = str8_lit("Stopped");}break;
case CTRL_EventKind_NewProc: { result = str8_lit("NewProc");}break;
case CTRL_EventKind_NewThread: { result = str8_lit("NewThread");}break;
case CTRL_EventKind_NewModule: { result = str8_lit("NewModule");}break;
case CTRL_EventKind_EndProc: { result = str8_lit("EndProc");}break;
case CTRL_EventKind_EndThread: { result = str8_lit("EndThread");}break;
case CTRL_EventKind_EndModule: { result = str8_lit("EndModule");}break;
case CTRL_EventKind_ModuleDebugInfoPathChange: { result = str8_lit("ModuleDebugInfoPathChange");}break;
case CTRL_EventKind_DebugString: { result = str8_lit("DebugString");}break;
case CTRL_EventKind_ThreadName: { result = str8_lit("ThreadName");}break;
case CTRL_EventKind_MemReserve: { result = str8_lit("MemReserve");}break;
case CTRL_EventKind_MemCommit: { result = str8_lit("MemCommit");}break;
case CTRL_EventKind_MemDecommit: { result = str8_lit("MemDecommit");}break;
case CTRL_EventKind_MemRelease: { result = str8_lit("MemRelease");}break;
}
return result;
}
internal String8
ctrl_string_from_msg_kind(CTRL_MsgKind kind)
{
String8 result = {0};
switch(kind)
{
default:{}break;
case CTRL_MsgKind_Launch: {result = str8_lit("Launch");}break;
case CTRL_MsgKind_Attach: {result = str8_lit("Attach");}break;
case CTRL_MsgKind_Kill: {result = str8_lit("Kill");}break;
case CTRL_MsgKind_Detach: {result = str8_lit("Detach");}break;
case CTRL_MsgKind_Run: {result = str8_lit("Run");}break;
case CTRL_MsgKind_SingleStep: {result = str8_lit("SingleStep");}break;
case CTRL_MsgKind_SetUserEntryPoints: {result = str8_lit("SetUserEntryPoints");}break;
case CTRL_MsgKind_SetModuleDebugInfoPath: {result = str8_lit("SetModuleDebugInfoPath");}break;
}
return result;
}
////////////////////////////////
//~ rjf: Machine/Handle Pair Type Functions
internal void
ctrl_machine_id_handle_pair_list_push(Arena *arena, CTRL_MachineIDHandlePairList *list, CTRL_MachineIDHandlePair *pair)
{
CTRL_MachineIDHandlePairNode *n = push_array(arena, CTRL_MachineIDHandlePairNode, 1);
MemoryCopyStruct(&n->v, pair);
SLLQueuePush(list->first, list->last, n);
list->count += 1;
}
internal CTRL_MachineIDHandlePairList
ctrl_machine_id_handle_pair_list_copy(Arena *arena, CTRL_MachineIDHandlePairList *src)
{
CTRL_MachineIDHandlePairList dst = {0};
for(CTRL_MachineIDHandlePairNode *n = src->first; n != 0; n = n->next)
{
ctrl_machine_id_handle_pair_list_push(arena, &dst, &n->v);
}
return dst;
}
////////////////////////////////
//~ rjf: Trap Type Functions
internal void
ctrl_trap_list_push(Arena *arena, CTRL_TrapList *list, CTRL_Trap *trap)
{
CTRL_TrapNode *node = push_array(arena, CTRL_TrapNode, 1);
MemoryCopyStruct(&node->v, trap);
SLLQueuePush(list->first, list->last, node);
list->count += 1;
}
internal CTRL_TrapList
ctrl_trap_list_copy(Arena *arena, CTRL_TrapList *src)
{
CTRL_TrapList dst = {0};
for(CTRL_TrapNode *src_n = src->first; src_n != 0; src_n = src_n->next)
{
ctrl_trap_list_push(arena, &dst, &src_n->v);
}
return dst;
}
////////////////////////////////
//~ rjf: User Breakpoint Type Functions
internal void
ctrl_user_breakpoint_list_push(Arena *arena, CTRL_UserBreakpointList *list, CTRL_UserBreakpoint *bp)
{
CTRL_UserBreakpointNode *n = push_array(arena, CTRL_UserBreakpointNode, 1);
MemoryCopyStruct(&n->v, bp);
SLLQueuePush(list->first, list->last, n);
list->count += 1;
}
internal CTRL_UserBreakpointList
ctrl_user_breakpoint_list_copy(Arena *arena, CTRL_UserBreakpointList *src)
{
CTRL_UserBreakpointList dst = {0};
for(CTRL_UserBreakpointNode *src_n = src->first; src_n != 0; src_n = src_n->next)
{
CTRL_UserBreakpoint dst_bp = zero_struct;
MemoryCopyStruct(&dst_bp, &src_n->v);
dst_bp.string = push_str8_copy(arena, src_n->v.string);
dst_bp.condition = push_str8_copy(arena, src_n->v.condition);
ctrl_user_breakpoint_list_push(arena, &dst, &dst_bp);
}
return dst;
}
////////////////////////////////
//~ rjf: Message Type Functions
//- rjf: deep copying
internal void
ctrl_msg_deep_copy(Arena *arena, CTRL_Msg *dst, CTRL_Msg *src)
{
MemoryCopyStruct(dst, src);
dst->path = push_str8_copy(arena, src->path);
dst->entry_points = str8_list_copy(arena, &src->entry_points);
dst->cmd_line_string_list = str8_list_copy(arena, &src->cmd_line_string_list);
dst->env_string_list = str8_list_copy(arena, &src->env_string_list);
dst->traps = ctrl_trap_list_copy(arena, &src->traps);
dst->user_bps = ctrl_user_breakpoint_list_copy(arena, &src->user_bps);
dst->freeze_state_threads = ctrl_machine_id_handle_pair_list_copy(arena, &src->freeze_state_threads);
}
//- rjf: list building
internal CTRL_Msg *
ctrl_msg_list_push(Arena *arena, CTRL_MsgList *list)
{
CTRL_MsgNode *n = push_array(arena, CTRL_MsgNode, 1);
SLLQueuePush(list->first, list->last, n);
list->count += 1;
CTRL_Msg *msg = &n->v;
return msg;
}
//- rjf: serialization
internal String8
ctrl_serialized_string_from_msg_list(Arena *arena, CTRL_MsgList *msgs)
{
Temp scratch = scratch_begin(&arena, 1);
String8List msgs_srlzed = {0};
str8_serial_begin(scratch.arena, &msgs_srlzed);
{
// rjf: write message count
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msgs->count);
// rjf: write all message data
for(CTRL_MsgNode *msg_n = msgs->first; msg_n != 0; msg_n = msg_n->next)
{
CTRL_Msg *msg = &msg_n->v;
// rjf: write flat parts
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->kind);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->run_flags);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->msg_id);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->machine_id);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->entity);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->parent);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->entity_id);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->exit_code);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->env_inherit);
str8_serial_push_array (scratch.arena, &msgs_srlzed, &msg->exception_code_filters[0], ArrayCount(msg->exception_code_filters));
// rjf: write path string
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->path.size);
str8_serial_push_data(scratch.arena, &msgs_srlzed, msg->path.str, msg->path.size);
// rjf: write entry point string list
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->entry_points.node_count);
for(String8Node *n = msg->entry_points.first; n != 0; n = n->next)
{
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &n->string.size);
str8_serial_push_data(scratch.arena, &msgs_srlzed, n->string.str, n->string.size);
}
// rjf: write command line string list
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->cmd_line_string_list.node_count);
for(String8Node *n = msg->cmd_line_string_list.first; n != 0; n = n->next)
{
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &n->string.size);
str8_serial_push_data(scratch.arena, &msgs_srlzed, n->string.str, n->string.size);
}
// rjf: write environment string list
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->env_string_list.node_count);
for(String8Node *n = msg->env_string_list.first; n != 0; n = n->next)
{
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &n->string.size);
str8_serial_push_data(scratch.arena, &msgs_srlzed, n->string.str, n->string.size);
}
// rjf: write trap list
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->traps.count);
for(CTRL_TrapNode *n = msg->traps.first; n != 0; n = n->next)
{
CTRL_Trap *trap = &n->v;
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &trap->flags);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &trap->vaddr);
}
// rjf: write user breakpoint list
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->user_bps.count);
for(CTRL_UserBreakpointNode *n = msg->user_bps.first; n != 0; n = n->next)
{
CTRL_UserBreakpoint *bp = &n->v;
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &bp->kind);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &bp->string.size);
str8_serial_push_data(scratch.arena, &msgs_srlzed, bp->string.str, bp->string.size);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &bp->pt);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &bp->u64);
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &bp->condition.size);
str8_serial_push_data(scratch.arena, &msgs_srlzed, bp->condition.str, bp->condition.size);
}
// rjf: write freeze state thread list
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->freeze_state_threads.count);
for(CTRL_MachineIDHandlePairNode *n = msg->freeze_state_threads.first; n != 0; n = n->next)
{
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &n->v);
}
// rjf: write freeze state
str8_serial_push_struct(scratch.arena, &msgs_srlzed, &msg->freeze_state_is_frozen);
}
}
String8 string = str8_serial_end(arena, &msgs_srlzed);
scratch_end(scratch);
return string;
}
internal CTRL_MsgList
ctrl_msg_list_from_serialized_string(Arena *arena, String8 string)
{
CTRL_MsgList msgs = {0};
{
U64 read_off = 0;
// rjf: read message count
U64 msg_count = 0;
read_off += str8_deserial_read_struct(string, read_off, &msg_count);
// rjf: read data for all messages
for(U64 msg_idx = 0; msg_idx < msg_count; msg_idx += 1)
{
// rjf: construct message
CTRL_MsgNode *msg_node = push_array(arena, CTRL_MsgNode, 1);
SLLQueuePush(msgs.first, msgs.last, msg_node);
msgs.count += 1;
CTRL_Msg *msg = &msg_node->v;
// rjf: read flat data
read_off += str8_deserial_read_struct(string, read_off, &msg->kind);
read_off += str8_deserial_read_struct(string, read_off, &msg->run_flags);
read_off += str8_deserial_read_struct(string, read_off, &msg->msg_id);
read_off += str8_deserial_read_struct(string, read_off, &msg->machine_id);
read_off += str8_deserial_read_struct(string, read_off, &msg->entity);
read_off += str8_deserial_read_struct(string, read_off, &msg->parent);
read_off += str8_deserial_read_struct(string, read_off, &msg->entity_id);
read_off += str8_deserial_read_struct(string, read_off, &msg->exit_code);
read_off += str8_deserial_read_struct(string, read_off, &msg->env_inherit);
read_off += str8_deserial_read_array (string, read_off, &msg->exception_code_filters[0], ArrayCount(msg->exception_code_filters));
// rjf: read path string
read_off += str8_deserial_read_struct(string, read_off, &msg->path.size);
msg->path.str = push_array_no_zero(arena, U8, msg->path.size);
read_off += str8_deserial_read(string, read_off, msg->path.str, msg->path.size, 1);
// rjf: read entry point string list
U64 entry_point_list_string_count = 0;
read_off += str8_deserial_read_struct(string, read_off, &entry_point_list_string_count);
for(U64 idx = 0; idx < entry_point_list_string_count; idx += 1)
{
String8 str = {0};
read_off += str8_deserial_read_struct(string, read_off, &str.size);
str.str = push_array_no_zero(arena, U8, str.size);
read_off += str8_deserial_read(string, read_off, str.str, str.size, 1);
str8_list_push(arena, &msg->entry_points, str);
}
// rjf: read command line string list
U64 cmd_line_string_count = 0;
read_off += str8_deserial_read_struct(string, read_off, &cmd_line_string_count);
for(U64 idx = 0; idx < cmd_line_string_count; idx += 1)
{
String8 cmd_line_str = {0};
read_off += str8_deserial_read_struct(string, read_off, &cmd_line_str.size);
cmd_line_str.str = push_array_no_zero(arena, U8, cmd_line_str.size);
read_off += str8_deserial_read(string, read_off, cmd_line_str.str, cmd_line_str.size, 1);
str8_list_push(arena, &msg->cmd_line_string_list, cmd_line_str);
}
// rjf: read environment string list
U64 env_string_count = 0;
read_off += str8_deserial_read_struct(string, read_off, &env_string_count);
for(U64 idx = 0; idx < env_string_count; idx += 1)
{
String8 env_str = {0};
read_off += str8_deserial_read_struct(string, read_off, &env_str.size);
env_str.str = push_array_no_zero(arena, U8, env_str.size);
read_off += str8_deserial_read(string, read_off, env_str.str, env_str.size, 1);
str8_list_push(arena, &msg->env_string_list, env_str);
}
// rjf: read trap list
U64 trap_count = 0;
read_off += str8_deserial_read_struct(string, read_off, &trap_count);
for(U64 idx = 0; idx < trap_count; idx += 1)
{
CTRL_TrapNode *n = push_array(arena, CTRL_TrapNode, 1);
SLLQueuePush(msg->traps.first, msg->traps.last, n);
msg->traps.count += 1;
CTRL_Trap *trap = &n->v;
read_off += str8_deserial_read_struct(string, read_off, &trap->flags);
read_off += str8_deserial_read_struct(string, read_off, &trap->vaddr);
}
// rjf: read user breakpoint list
U64 user_bp_count = 0;
read_off += str8_deserial_read_struct(string, read_off, &user_bp_count);
for(U64 idx = 0; idx < user_bp_count; idx += 1)
{
CTRL_UserBreakpointNode *n = push_array(arena, CTRL_UserBreakpointNode, 1);
SLLQueuePush(msg->user_bps.first, msg->user_bps.last, n);
msg->user_bps.count += 1;
CTRL_UserBreakpoint *bp = &n->v;
read_off += str8_deserial_read_struct(string, read_off, &bp->kind);
read_off += str8_deserial_read_struct(string, read_off, &bp->string.size);
bp->string.str = push_array_no_zero(arena, U8, bp->string.size);
read_off += str8_deserial_read(string, read_off, bp->string.str, bp->string.size, 1);
read_off += str8_deserial_read_struct(string, read_off, &bp->pt);
read_off += str8_deserial_read_struct(string, read_off, &bp->u64);
read_off += str8_deserial_read_struct(string, read_off, &bp->condition.size);
bp->condition.str = push_array_no_zero(arena, U8, bp->condition.size);
read_off += str8_deserial_read(string, read_off, bp->condition.str, bp->condition.size, 1);
}
// rjf: read freeze state thread list
U64 frozen_thread_count = 0;
read_off += str8_deserial_read_struct(string, read_off, &frozen_thread_count);
for(U64 idx = 0; idx < frozen_thread_count; idx += 1)
{
CTRL_MachineIDHandlePair pair = {0};
read_off += str8_deserial_read_struct(string, read_off, &pair);
ctrl_machine_id_handle_pair_list_push(arena, &msg->freeze_state_threads, &pair);
}
// rjf: read freeze state
read_off += str8_deserial_read_struct(string, read_off, &msg->freeze_state_is_frozen);
}
}
return msgs;
}
////////////////////////////////
//~ rjf: Event Type Functions
//- rjf: list building
internal CTRL_Event *
ctrl_event_list_push(Arena *arena, CTRL_EventList *list)
{
CTRL_EventNode *n = push_array(arena, CTRL_EventNode, 1);
SLLQueuePush(list->first, list->last, n);
list->count += 1;
CTRL_Event *event = &n->v;
return event;
}
internal void
ctrl_event_list_concat_in_place(CTRL_EventList *dst, CTRL_EventList *to_push)
{
if(dst->last == 0)
{
MemoryCopyStruct(dst, to_push);
}
else if(to_push->first != 0)
{
dst->last->next = to_push->first;
dst->last = to_push->last;
dst->count += to_push->count;
}
MemoryZeroStruct(to_push);
}
//- rjf: serialization
internal String8
ctrl_serialized_string_from_event(Arena *arena, CTRL_Event *event, U64 max)
{
Temp scratch = scratch_begin(&arena, 1);
String8List srl = {0};
str8_serial_begin(scratch.arena, &srl);
{
str8_serial_push_struct(scratch.arena, &srl, &event->kind);
str8_serial_push_struct(scratch.arena, &srl, &event->cause);
str8_serial_push_struct(scratch.arena, &srl, &event->exception_kind);
str8_serial_push_struct(scratch.arena, &srl, &event->msg_id);
str8_serial_push_struct(scratch.arena, &srl, &event->machine_id);
str8_serial_push_struct(scratch.arena, &srl, &event->entity);
str8_serial_push_struct(scratch.arena, &srl, &event->parent);
str8_serial_push_struct(scratch.arena, &srl, &event->arch);
str8_serial_push_struct(scratch.arena, &srl, &event->u64_code);
str8_serial_push_struct(scratch.arena, &srl, &event->entity_id);
str8_serial_push_struct(scratch.arena, &srl, &event->vaddr_rng);
str8_serial_push_struct(scratch.arena, &srl, &event->rip_vaddr);
str8_serial_push_struct(scratch.arena, &srl, &event->stack_base);
str8_serial_push_struct(scratch.arena, &srl, &event->tls_root);
str8_serial_push_struct(scratch.arena, &srl, &event->timestamp);
str8_serial_push_struct(scratch.arena, &srl, &event->exception_code);
String8 string = event->string;
string.size = Min(string.size, max-srl.total_size);
str8_serial_push_struct(scratch.arena, &srl, &string.size);
str8_serial_push_data(scratch.arena, &srl, string.str, string.size);
}
String8 string = str8_serial_end(arena, &srl);
scratch_end(scratch);
return string;
}
internal CTRL_Event
ctrl_event_from_serialized_string(Arena *arena, String8 string)
{
CTRL_Event event = zero_struct;
{
U64 read_off = 0;
read_off += str8_deserial_read_struct(string, read_off, &event.kind);
read_off += str8_deserial_read_struct(string, read_off, &event.cause);
read_off += str8_deserial_read_struct(string, read_off, &event.exception_kind);
read_off += str8_deserial_read_struct(string, read_off, &event.msg_id);
read_off += str8_deserial_read_struct(string, read_off, &event.machine_id);
read_off += str8_deserial_read_struct(string, read_off, &event.entity);
read_off += str8_deserial_read_struct(string, read_off, &event.parent);
read_off += str8_deserial_read_struct(string, read_off, &event.arch);
read_off += str8_deserial_read_struct(string, read_off, &event.u64_code);
read_off += str8_deserial_read_struct(string, read_off, &event.entity_id);
read_off += str8_deserial_read_struct(string, read_off, &event.vaddr_rng);
read_off += str8_deserial_read_struct(string, read_off, &event.rip_vaddr);
read_off += str8_deserial_read_struct(string, read_off, &event.stack_base);
read_off += str8_deserial_read_struct(string, read_off, &event.tls_root);
read_off += str8_deserial_read_struct(string, read_off, &event.timestamp);
read_off += str8_deserial_read_struct(string, read_off, &event.exception_code);
read_off += str8_deserial_read_struct(string, read_off, &event.string.size);
event.string.str = push_array_no_zero(arena, U8, event.string.size);
read_off += str8_deserial_read(string, read_off, event.string.str, event.string.size, 1);
}
return event;
}
////////////////////////////////
//~ rjf: Entity Type Functions
//- rjf: cache creation/destruction
internal CTRL_EntityStore *
ctrl_entity_store_alloc(void)
{
Arena *arena = arena_alloc();
CTRL_EntityStore *store = push_array(arena, CTRL_EntityStore, 1);
store->arena = arena;
store->hash_slots_count = 1024;
store->hash_slots = push_array(arena, CTRL_EntityHashSlot, store->hash_slots_count);
CTRL_Entity *root = store->root = ctrl_entity_alloc(store, &ctrl_entity_nil, CTRL_EntityKind_Root, Architecture_Null, 0, dmn_handle_zero(), 0);
CTRL_Entity *local_machine = ctrl_entity_alloc(store, root, CTRL_EntityKind_Machine, architecture_from_context(), CTRL_MachineID_Local, dmn_handle_zero(), 0);
(void)local_machine;
return store;
}
internal void
ctrl_entity_store_release(CTRL_EntityStore *cache)
{
arena_release(cache->arena);
}
//- rjf: string allocation/deletion
internal U64
ctrl_name_bucket_idx_from_string_size(U64 size)
{
U64 size_rounded = u64_up_to_pow2(size+1);
size_rounded = ClampBot((1<<4), size_rounded);
U64 bucket_idx = 0;
switch(size_rounded)
{
case 1<<4: {bucket_idx = 0;}break;
case 1<<5: {bucket_idx = 1;}break;
case 1<<6: {bucket_idx = 2;}break;
case 1<<7: {bucket_idx = 3;}break;
case 1<<8: {bucket_idx = 4;}break;
case 1<<9: {bucket_idx = 5;}break;
case 1<<10:{bucket_idx = 6;}break;
default:{bucket_idx = ArrayCount(((CTRL_EntityStore *)0)->free_string_chunks)-1;}break;
}
return bucket_idx;
}
internal String8
ctrl_entity_string_alloc(CTRL_EntityStore *store, String8 string)
{
if(string.size == 0) {return str8_zero();}
U64 bucket_idx = ctrl_name_bucket_idx_from_string_size(string.size);
CTRL_EntityStringChunkNode *node = store->free_string_chunks[bucket_idx];
// rjf: pull from bucket free list
if(node != 0)
{
if(bucket_idx == ArrayCount(store->free_string_chunks)-1)
{
node = 0;
CTRL_EntityStringChunkNode *prev = 0;
for(CTRL_EntityStringChunkNode *n = store->free_string_chunks[bucket_idx];
n != 0;
prev = n, n = n->next)
{
if(n->size >= string.size+1)
{
if(prev == 0)
{
store->free_string_chunks[bucket_idx] = n->next;
}
else
{
prev->next = n->next;
}
node = n;
break;
}
}
}
else
{
SLLStackPop(store->free_string_chunks[bucket_idx]);
}
}
// rjf: no found node -> allocate new
if(node == 0)
{
U64 chunk_size = 0;
if(bucket_idx < ArrayCount(store->free_string_chunks)-1)
{
chunk_size = 1<<(bucket_idx+4);
}
else
{
chunk_size = u64_up_to_pow2(string.size);
}
U8 *chunk_memory = push_array(store->arena, U8, chunk_size);
node = (CTRL_EntityStringChunkNode *)chunk_memory;
}
// rjf: fill string & return
String8 allocated_string = str8((U8 *)node, string.size);
MemoryCopy((U8 *)node, string.str, string.size);
return allocated_string;
}
internal void
ctrl_entity_string_release(CTRL_EntityStore *store, String8 string)
{
if(string.size == 0) {return;}
U64 bucket_idx = ctrl_name_bucket_idx_from_string_size(string.size);
CTRL_EntityStringChunkNode *node = (CTRL_EntityStringChunkNode *)string.str;
node->size = u64_up_to_pow2(string.size);
SLLStackPush(store->free_string_chunks[bucket_idx], node);
}
//- rjf: entity construction/deletion
internal CTRL_Entity *
ctrl_entity_alloc(CTRL_EntityStore *store, CTRL_Entity *parent, CTRL_EntityKind kind, Architecture arch, CTRL_MachineID machine_id, DMN_Handle handle, U64 id)
{
CTRL_Entity *entity = &ctrl_entity_nil;
{
// rjf: allocate
entity = store->free;
{
if(entity != 0)
{
SLLStackPop(store->free);
}
else
{
entity = push_array_no_zero(store->arena, CTRL_Entity, 1);
}
MemoryZeroStruct(entity);
}
// rjf: fill
{
entity->kind = kind;
entity->arch = arch;
entity->machine_id = machine_id;
entity->handle = handle;
entity->id = id;
entity->parent = parent;
entity->next = entity->prev = entity->first = entity->last = &ctrl_entity_nil;
if(parent != &ctrl_entity_nil)
{
DLLPushBack_NPZ(&ctrl_entity_nil, parent->first, parent->last, entity, next, prev);
}
}
// rjf: insert into hash map
{
U64 hash = ctrl_hash_from_machine_id_handle(machine_id, handle);
U64 slot_idx = hash%store->hash_slots_count;
CTRL_EntityHashSlot *slot = &store->hash_slots[slot_idx];
CTRL_EntityHashNode *node = 0;
for(CTRL_EntityHashNode *n = slot->first; n != 0; n = n->next)
{
if(n->entity->machine_id == machine_id && dmn_handle_match(n->entity->handle, handle))
{
node = n;
break;
}
}
if(node == 0)
{
node = store->hash_node_free;
if(node != 0)
{
SLLStackPop(store->hash_node_free);
}
else
{
node = push_array_no_zero(store->arena, CTRL_EntityHashNode, 1);
}
MemoryZeroStruct(node);
DLLPushBack(slot->first, slot->last, node);
node->entity = entity;
}
}
}
return entity;
}
internal void
ctrl_entity_release(CTRL_EntityStore *store, CTRL_Entity *entity)
{
// rjf: unhook root
if(entity->parent != &ctrl_entity_nil)
{
DLLRemove_NPZ(&ctrl_entity_nil, entity->parent->first, entity->parent->last, entity, next, prev);
}
// rjf: walk every entity in this tree, free each
if(entity != &ctrl_entity_nil)
{
Temp scratch = scratch_begin(0, 0);
typedef struct Task Task;
struct Task
{
Task *next;
CTRL_Entity *e;
};
Task start_task = {0, entity};
Task *first_task = &start_task;
Task *last_task = &start_task;
for(Task *t = first_task; t != 0; t = t->next)
{
for(CTRL_Entity *child = t->e->first; child != &ctrl_entity_nil; child = child->next)
{
Task *t = push_array(scratch.arena, Task, 1);
t->e = child;
SLLQueuePush(first_task, last_task, t);
}
// rjf: free entity
SLLStackPush(store->free, t->e);
// rjf: remove from hash map
{
U64 hash = ctrl_hash_from_machine_id_handle(t->e->machine_id, t->e->handle);
U64 slot_idx = hash%store->hash_slots_count;
CTRL_EntityHashSlot *slot = &store->hash_slots[slot_idx];
CTRL_EntityHashNode *node = 0;
for(CTRL_EntityHashNode *n = slot->first; n != 0; n = n->next)
{
if(n->entity->machine_id == t->e->machine_id && dmn_handle_match(n->entity->handle, t->e->handle))
{
DLLRemove(slot->first, slot->last, n);
SLLStackPush(store->hash_node_free, n);
break;
}
}
}
}
scratch_end(scratch);
}
}
//- rjf: entity equipment
internal void
ctrl_entity_equip_string(CTRL_EntityStore *store, CTRL_Entity *entity, String8 string)
{
if(entity->string.size != 0)
{
ctrl_entity_string_release(store, entity->string);
}
entity->string = ctrl_entity_string_alloc(store, string);
}
//- rjf: entity store lookups
internal CTRL_Entity *
ctrl_entity_from_machine_id_handle(CTRL_EntityStore *store, CTRL_MachineID machine_id, DMN_Handle handle)
{
CTRL_Entity *entity = &ctrl_entity_nil;
{
U64 hash = ctrl_hash_from_machine_id_handle(machine_id, handle);
U64 slot_idx = hash%store->hash_slots_count;
CTRL_EntityHashSlot *slot = &store->hash_slots[slot_idx];
CTRL_EntityHashNode *node = 0;
for(CTRL_EntityHashNode *n = slot->first; n != 0; n = n->next)
{
if(n->entity->machine_id == machine_id && dmn_handle_match(n->entity->handle, handle))
{
entity = n->entity;
break;
}
}
}
return entity;
}
internal CTRL_Entity *
ctrl_entity_child_from_kind(CTRL_Entity *parent, CTRL_EntityKind kind)
{
CTRL_Entity *result = &ctrl_entity_nil;
for(CTRL_Entity *child = parent->first;
child != &ctrl_entity_nil;
child = child->next)
{
if(child->kind == kind)
{
result = child;
break;
}
}
return result;
}
//- rjf: applying events to entity caches
internal void
ctrl_entity_store_apply_events(CTRL_EntityStore *store, CTRL_EventList *list)
{
//- rjf: scan events & construct entities
for(CTRL_EventNode *n = list->first; n != 0; n = n->next)
{
CTRL_Event *event = &n->v;
switch(event->kind)
{
default:{}break;
//- rjf: processes
case CTRL_EventKind_NewProc:
{
CTRL_Entity *machine = ctrl_entity_from_machine_id_handle(store, event->machine_id, dmn_handle_zero());
CTRL_Entity *process = ctrl_entity_alloc(store, machine, CTRL_EntityKind_Process, event->arch, event->machine_id, event->entity, (U64)event->entity_id);
}break;
case CTRL_EventKind_EndProc:
{
CTRL_Entity *process = ctrl_entity_from_machine_id_handle(store, event->machine_id, event->entity);
ctrl_entity_release(store, process);
for(CTRL_Entity *entry = store->root->first, *next = &ctrl_entity_nil;
entry != &ctrl_entity_nil;
entry = next)
{
next = entry->next;
if(entry->kind == CTRL_EntityKind_EntryPoint && entry->id == process->id)
{
ctrl_entity_release(store, entry);
}
}
}break;
//- rjf: threads
case CTRL_EventKind_NewThread:
{
CTRL_Entity *process = ctrl_entity_from_machine_id_handle(store, event->machine_id, event->parent);
CTRL_Entity *thread = ctrl_entity_alloc(store, process, CTRL_EntityKind_Thread, event->arch, event->machine_id, event->entity, (U64)event->entity_id);
ctrl_query_cached_rip_from_thread(store, event->machine_id, event->entity);
}break;
case CTRL_EventKind_EndThread:
{
CTRL_Entity *thread = ctrl_entity_from_machine_id_handle(store, event->machine_id, event->entity);
ctrl_entity_release(store, thread);
}break;
case CTRL_EventKind_ThreadName:
{
CTRL_Entity *thread = ctrl_entity_from_machine_id_handle(store, event->machine_id, event->entity);
ctrl_entity_equip_string(store, thread, event->string);
}break;
//- rjf: modules
case CTRL_EventKind_NewModule:
{
Temp scratch = scratch_begin(0, 0);
CTRL_Entity *process = ctrl_entity_from_machine_id_handle(store, event->machine_id, event->parent);
CTRL_Entity *module = ctrl_entity_alloc(store, process, CTRL_EntityKind_Module, event->arch, event->machine_id, event->entity, event->vaddr_rng.min);
ctrl_entity_equip_string(store, module, event->string);
module->timestamp = event->timestamp;
module->vaddr_range = event->vaddr_rng;
scratch_end(scratch);
}break;
case CTRL_EventKind_EndModule:
{
CTRL_Entity *module = ctrl_entity_from_machine_id_handle(store, event->machine_id, event->entity);
ctrl_entity_release(store, module);
}break;
case CTRL_EventKind_ModuleDebugInfoPathChange:
{
CTRL_Entity *module = ctrl_entity_from_machine_id_handle(store, event->machine_id, event->entity);
CTRL_Entity *debug_info_path = ctrl_entity_child_from_kind(module, CTRL_EntityKind_DebugInfoPath);
if(debug_info_path == &ctrl_entity_nil)
{
debug_info_path = ctrl_entity_alloc(store, module, CTRL_EntityKind_DebugInfoPath, Architecture_Null, 0, dmn_handle_zero(), 0);
}
ctrl_entity_equip_string(store, debug_info_path, event->string);
debug_info_path->timestamp = event->timestamp;
}break;
}
}
}
////////////////////////////////
//~ rjf: Main Layer Initialization
internal void
ctrl_init(void)
{
Arena *arena = arena_alloc();
ctrl_state = push_array(arena, CTRL_State, 1);
ctrl_state->arena = arena;
for(Architecture arch = (Architecture)0; arch < Architecture_COUNT; arch = (Architecture)(arch+1))
{
String8 *reg_names = regs_reg_code_string_table_from_architecture(arch);
U64 reg_count = regs_reg_code_count_from_architecture(arch);
String8 *alias_names = regs_alias_code_string_table_from_architecture(arch);
U64 alias_count = regs_alias_code_count_from_architecture(arch);
ctrl_state->arch_string2reg_tables[arch] = e_string2num_map_make(ctrl_state->arena, 256);
ctrl_state->arch_string2alias_tables[arch] = e_string2num_map_make(ctrl_state->arena, 256);
for(U64 idx = 1; idx < reg_count; idx += 1)
{
e_string2num_map_insert(ctrl_state->arena, &ctrl_state->arch_string2reg_tables[arch], reg_names[idx], idx);
}
for(U64 idx = 1; idx < alias_count; idx += 1)
{
e_string2num_map_insert(ctrl_state->arena, &ctrl_state->arch_string2alias_tables[arch], alias_names[idx], idx);
}
}
ctrl_state->process_memory_cache.slots_count = 256;
ctrl_state->process_memory_cache.slots = push_array(arena, CTRL_ProcessMemoryCacheSlot, ctrl_state->process_memory_cache.slots_count);
ctrl_state->process_memory_cache.stripes_count = os_get_system_info()->logical_processor_count;
ctrl_state->process_memory_cache.stripes = push_array(arena, CTRL_ProcessMemoryCacheStripe, ctrl_state->process_memory_cache.stripes_count);
for(U64 idx = 0; idx < ctrl_state->process_memory_cache.stripes_count; idx += 1)
{
ctrl_state->process_memory_cache.stripes[idx].rw_mutex = os_rw_mutex_alloc();
ctrl_state->process_memory_cache.stripes[idx].cv = os_condition_variable_alloc();
}
ctrl_state->thread_reg_cache.slots_count = 1024;
ctrl_state->thread_reg_cache.slots = push_array(arena, CTRL_ThreadRegCacheSlot, ctrl_state->thread_reg_cache.slots_count);
ctrl_state->thread_reg_cache.stripes_count = os_get_system_info()->logical_processor_count;
ctrl_state->thread_reg_cache.stripes = push_array(arena, CTRL_ThreadRegCacheStripe, ctrl_state->thread_reg_cache.stripes_count);
for(U64 idx = 0; idx < ctrl_state->thread_reg_cache.stripes_count; idx += 1)
{
ctrl_state->thread_reg_cache.stripes[idx].arena = arena_alloc();
ctrl_state->thread_reg_cache.stripes[idx].rw_mutex = os_rw_mutex_alloc();
}
ctrl_state->module_image_info_cache.slots_count = 1024;
ctrl_state->module_image_info_cache.slots = push_array(arena, CTRL_ModuleImageInfoCacheSlot, ctrl_state->module_image_info_cache.slots_count);
ctrl_state->module_image_info_cache.stripes_count = os_get_system_info()->logical_processor_count;
ctrl_state->module_image_info_cache.stripes = push_array(arena, CTRL_ModuleImageInfoCacheStripe, ctrl_state->module_image_info_cache.stripes_count);
for(U64 idx = 0; idx < ctrl_state->module_image_info_cache.stripes_count; idx += 1)
{
ctrl_state->module_image_info_cache.stripes[idx].arena = arena_alloc();
ctrl_state->module_image_info_cache.stripes[idx].rw_mutex = os_rw_mutex_alloc();
}
ctrl_state->u2c_ring_size = KB(64);
ctrl_state->u2c_ring_base = push_array_no_zero(arena, U8, ctrl_state->u2c_ring_size);
ctrl_state->u2c_ring_mutex = os_mutex_alloc();
ctrl_state->u2c_ring_cv = os_condition_variable_alloc();
ctrl_state->c2u_ring_size = KB(64);
ctrl_state->c2u_ring_max_string_size = ctrl_state->c2u_ring_size/2;
ctrl_state->c2u_ring_base = push_array_no_zero(arena, U8, ctrl_state->c2u_ring_size);
ctrl_state->c2u_ring_mutex = os_mutex_alloc();
ctrl_state->c2u_ring_cv = os_condition_variable_alloc();
{
Temp scratch = scratch_begin(0, 0);
String8 user_program_data_path = os_get_process_info()->user_program_data_path;
String8 user_data_folder = push_str8f(scratch.arena, "%S/raddbg/logs", user_program_data_path);
os_make_directory(user_data_folder);
ctrl_state->ctrl_thread_log_path = push_str8f(ctrl_state->arena, "%S/ctrl_thread.raddbg_log", user_data_folder);
os_write_data_to_file_path(ctrl_state->ctrl_thread_log_path, str8_zero());
scratch_end(scratch);
}
ctrl_state->ctrl_thread_entity_store = ctrl_entity_store_alloc();
ctrl_state->dmn_event_arena = arena_alloc();
ctrl_state->user_entry_point_arena = arena_alloc();
for(CTRL_ExceptionCodeKind k = (CTRL_ExceptionCodeKind)0; k < CTRL_ExceptionCodeKind_COUNT; k = (CTRL_ExceptionCodeKind)(k+1))
{
if(ctrl_exception_code_kind_default_enable_table[k])
{
ctrl_state->exception_code_filters[k/64] |= 1ull<<(k%64);
}
}
ctrl_state->u2ms_ring_size = KB(64);
ctrl_state->u2ms_ring_base = push_array(arena, U8, ctrl_state->u2ms_ring_size);
ctrl_state->u2ms_ring_mutex = os_mutex_alloc();
ctrl_state->u2ms_ring_cv = os_condition_variable_alloc();
ctrl_state->ctrl_thread_log = log_alloc();
ctrl_state->ctrl_thread = os_thread_launch(ctrl_thread__entry_point, 0, 0);
ctrl_state->ms_thread_count = Clamp(1, os_get_system_info()->logical_processor_count-1, 4);
ctrl_state->ms_threads = push_array(arena, OS_Handle, ctrl_state->ms_thread_count);
for(U64 idx = 0; idx < ctrl_state->ms_thread_count; idx += 1)
{
ctrl_state->ms_threads[idx] = os_thread_launch(ctrl_mem_stream_thread__entry_point, (void *)idx, 0);
}
}
////////////////////////////////
//~ rjf: Wakeup Callback Registration
internal void
ctrl_set_wakeup_hook(CTRL_WakeupFunctionType *wakeup_hook)
{
ctrl_state->wakeup_hook = wakeup_hook;
}
////////////////////////////////
//~ rjf: Process Memory Functions
//- rjf: process memory cache interaction
internal U128
ctrl_calc_hash_store_key_from_process_vaddr_range(CTRL_MachineID machine_id, DMN_Handle process, Rng1U64 range, B32 zero_terminated)
{