forked from bytecodealliance/wasm-micro-runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_engine.c
More file actions
1433 lines (1195 loc) · 41.7 KB
/
debug_engine.c
File metadata and controls
1433 lines (1195 loc) · 41.7 KB
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) 2021 Ant Group. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "debug_engine.h"
#include "gdbserver.h"
#include "handler.h"
#include "bh_platform.h"
#include "wasm_interp.h"
#include "wasm_opcode.h"
#include "wasm_runtime.h"
static const uint8 break_instr[] = { DEBUG_OP_BREAK };
typedef struct WASMDebugEngine {
struct WASMDebugEngine *next;
WASMDebugControlThread *control_thread;
char ip_addr[128];
int32 process_base_port;
bh_list debug_instance_list;
korp_mutex instance_list_lock;
} WASMDebugEngine;
void
on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env)
{
os_mutex_lock(&debug_inst->wait_lock);
debug_inst->stopped_thread = exec_env;
if (debug_inst->current_state == DBG_LAUNCHING) {
/* In launching phase, send a signal so that handle_threadstop_request
* can be woken up */
os_cond_signal(&debug_inst->wait_cond);
}
os_mutex_unlock(&debug_inst->wait_lock);
}
void
on_thread_exit_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env)
{
os_mutex_lock(&debug_inst->wait_lock);
/* DBG_LAUNCHING: exit when debugger detached,
* DBG_ERROR: exit when debugger error */
if (debug_inst->current_state != DBG_LAUNCHING
&& debug_inst->current_state != DBG_ERROR) {
/* only when exit normally the debugger thread will participate in
* teardown phase */
debug_inst->stopped_thread = exec_env;
}
os_mutex_unlock(&debug_inst->wait_lock);
}
static WASMDebugEngine *g_debug_engine;
static uint32 current_instance_id = 1;
static uint32
allocate_instance_id(void)
{
uint32 id;
bh_assert(g_debug_engine);
os_mutex_lock(&g_debug_engine->instance_list_lock);
id = current_instance_id++;
os_mutex_unlock(&g_debug_engine->instance_list_lock);
return id;
}
static bool
is_thread_running(WASMDebugControlThread *control_thread)
{
return control_thread->status == RUNNING;
}
static bool
is_thread_stopped(WASMDebugControlThread *control_thread)
{
return control_thread->status == STOPPED;
}
static bool
is_thread_detached(WASMDebugControlThread *control_thread)
{
return control_thread->status == DETACHED;
}
static void *
control_thread_routine(void *arg)
{
WASMDebugInstance *debug_inst = (WASMDebugInstance *)arg;
WASMDebugControlThread *control_thread = NULL;
control_thread = debug_inst->control_thread;
bh_assert(control_thread);
os_mutex_lock(&debug_inst->wait_lock);
control_thread->status = RUNNING;
debug_inst->id = allocate_instance_id();
control_thread->debug_engine = g_debug_engine;
control_thread->debug_instance = debug_inst;
bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr),
g_debug_engine->ip_addr);
if (control_thread->port == -1) {
control_thread->port =
(g_debug_engine->process_base_port == 0)
? 0
: g_debug_engine->process_base_port + debug_inst->id - 1;
}
LOG_WARNING("control thread of debug object %p start\n", debug_inst);
control_thread->server =
wasm_create_gdbserver(control_thread->ip_addr, &control_thread->port);
if (!control_thread->server) {
LOG_ERROR("Failed to create debug server\n");
control_thread->port = 0;
os_cond_signal(&debug_inst->wait_cond);
os_mutex_unlock(&debug_inst->wait_lock);
return NULL;
}
control_thread->server->thread = control_thread;
/*
* wasm gdbserver created, the execution thread
* doesn't need to wait for the debugger connection,
* so we wake up the execution thread before listen
*/
os_cond_signal(&debug_inst->wait_cond);
os_mutex_unlock(&debug_inst->wait_lock);
if (!wasm_gdbserver_listen(control_thread->server)) {
LOG_ERROR("Failed while listening for debugger\n");
goto fail;
}
/* outer infinite loop: try to connect with the debugger */
while (true) {
/* wait lldb client to connect */
if (!wasm_gdbserver_accept(control_thread->server)) {
LOG_ERROR("Failed while accepting debugger connection\n");
goto fail;
}
control_thread->status = RUNNING;
/* when reattached, send signal */
wasm_cluster_send_signal_all(debug_inst->cluster, WAMR_SIG_SINGSTEP);
/* inner infinite loop: keep serving until detach */
while (true) {
os_mutex_lock(&control_thread->wait_lock);
if (is_thread_running(control_thread)) {
/* send thread stop reply */
if (debug_inst->stopped_thread
&& debug_inst->current_state == APP_RUNNING) {
uint32 status;
korp_tid tid;
status = (uint32)debug_inst->stopped_thread->current_status
->signal_flag;
tid = debug_inst->stopped_thread->handle;
if (debug_inst->stopped_thread->current_status
->running_status
== STATUS_EXIT) {
/* If the thread exits, report "W00" if it's the last
* thread in the cluster, otherwise ignore this event */
status = 0;
/* By design, all the other threads should have been
* stopped at this moment, so it is safe to access the
* exec_env_list.len without lock */
if (debug_inst->cluster->exec_env_list.len != 1) {
debug_inst->stopped_thread = NULL;
/* The exiting thread may wait for the signal */
os_cond_signal(&debug_inst->wait_cond);
os_mutex_unlock(&control_thread->wait_lock);
continue;
}
}
wasm_debug_instance_set_cur_thread(
debug_inst, debug_inst->stopped_thread->handle);
send_thread_stop_status(control_thread->server, status,
tid);
debug_inst->current_state = APP_STOPPED;
debug_inst->stopped_thread = NULL;
if (status == 0) {
/* The exiting thread may wait for the signal */
os_cond_signal(&debug_inst->wait_cond);
}
}
/* Processing incoming requests */
if (!wasm_gdbserver_handle_packet(control_thread->server)) {
control_thread->status = STOPPED;
LOG_ERROR("An error occurs when handling a packet\n");
os_mutex_unlock(&control_thread->wait_lock);
goto fail;
}
}
else if (is_thread_detached(control_thread)) {
os_mutex_unlock(&control_thread->wait_lock);
break;
}
else if (is_thread_stopped(control_thread)) {
os_mutex_unlock(&control_thread->wait_lock);
return NULL;
}
os_mutex_unlock(&control_thread->wait_lock);
}
}
fail:
wasm_debug_instance_on_failure(debug_inst);
LOG_VERBOSE("control thread of debug object [%p] stopped with failure\n",
debug_inst);
return NULL;
}
static WASMDebugControlThread *
wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port)
{
WASMDebugControlThread *control_thread;
if (!(control_thread =
wasm_runtime_malloc(sizeof(WASMDebugControlThread)))) {
LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
return NULL;
}
memset(control_thread, 0, sizeof(WASMDebugControlThread));
control_thread->port = port;
if (os_mutex_init(&control_thread->wait_lock) != 0)
goto fail;
debug_instance->control_thread = control_thread;
os_mutex_lock(&debug_instance->wait_lock);
if (0
!= os_thread_create(&control_thread->tid, control_thread_routine,
debug_instance, APP_THREAD_STACK_SIZE_DEFAULT)) {
os_mutex_unlock(&debug_instance->wait_lock);
goto fail1;
}
/* wait until the debug control thread ready */
os_cond_wait(&debug_instance->wait_cond, &debug_instance->wait_lock);
os_mutex_unlock(&debug_instance->wait_lock);
if (!control_thread->server) {
os_thread_join(control_thread->tid, NULL);
goto fail1;
}
os_mutex_lock(&g_debug_engine->instance_list_lock);
/* create control thread success, append debug instance to debug engine */
bh_list_insert(&g_debug_engine->debug_instance_list, debug_instance);
os_mutex_unlock(&g_debug_engine->instance_list_lock);
/* If we set WAMR_SIG_STOP here, the VSCode debugger adaptor will raise an
* exception in the UI. We use WAMR_SIG_SINGSTEP to avoid this exception for
* better user experience */
wasm_cluster_send_signal_all(debug_instance->cluster, WAMR_SIG_SINGSTEP);
return control_thread;
fail1:
os_mutex_destroy(&control_thread->wait_lock);
fail:
wasm_runtime_free(control_thread);
return NULL;
}
static void
wasm_debug_control_thread_destroy(WASMDebugInstance *debug_instance)
{
WASMDebugControlThread *control_thread = debug_instance->control_thread;
LOG_VERBOSE("stopping control thread of debug object [%p]\n",
debug_instance);
control_thread->status = STOPPED;
os_mutex_lock(&control_thread->wait_lock);
wasm_close_gdbserver(control_thread->server);
os_mutex_unlock(&control_thread->wait_lock);
os_thread_join(control_thread->tid, NULL);
wasm_runtime_free(control_thread->server);
os_mutex_destroy(&control_thread->wait_lock);
wasm_runtime_free(control_thread);
}
static WASMDebugEngine *
wasm_debug_engine_create(void)
{
WASMDebugEngine *engine;
if (!(engine = wasm_runtime_malloc(sizeof(WASMDebugEngine)))) {
LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
return NULL;
}
memset(engine, 0, sizeof(WASMDebugEngine));
if (os_mutex_init(&engine->instance_list_lock) != 0) {
wasm_runtime_free(engine);
LOG_ERROR("WASM Debug Engine error: failed to init mutex");
return NULL;
}
/* reset current instance id */
current_instance_id = 1;
bh_list_init(&engine->debug_instance_list);
return engine;
}
void
wasm_debug_engine_destroy(void)
{
if (g_debug_engine) {
wasm_debug_handler_deinit();
os_mutex_destroy(&g_debug_engine->instance_list_lock);
wasm_runtime_free(g_debug_engine);
g_debug_engine = NULL;
}
}
bool
wasm_debug_engine_init(char *ip_addr, int32 process_port)
{
if (wasm_debug_handler_init() != 0) {
return false;
}
if (g_debug_engine == NULL) {
g_debug_engine = wasm_debug_engine_create();
}
if (g_debug_engine) {
g_debug_engine->process_base_port =
(process_port > 0) ? process_port : 0;
if (ip_addr)
snprintf(g_debug_engine->ip_addr, sizeof(g_debug_engine->ip_addr),
"%s", ip_addr);
else
snprintf(g_debug_engine->ip_addr, sizeof(g_debug_engine->ip_addr),
"%s", "127.0.0.1");
}
else {
wasm_debug_handler_deinit();
}
return g_debug_engine != NULL ? true : false;
}
/* A debug Instance is a debug "process" in gdb remote protocol
and bound to a runtime cluster */
WASMDebugInstance *
wasm_debug_instance_create(WASMCluster *cluster, int32 port)
{
WASMDebugInstance *instance;
WASMExecEnv *exec_env = NULL;
wasm_module_inst_t module_inst = NULL;
if (!g_debug_engine) {
return NULL;
}
if (!(instance = wasm_runtime_malloc(sizeof(WASMDebugInstance)))) {
LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
return NULL;
}
memset(instance, 0, sizeof(WASMDebugInstance));
if (os_mutex_init(&instance->wait_lock) != 0) {
goto fail1;
}
if (os_cond_init(&instance->wait_cond) != 0) {
goto fail2;
}
bh_list_init(&instance->break_point_list);
bh_list_init(&instance->watch_point_list_read);
bh_list_init(&instance->watch_point_list_write);
instance->cluster = cluster;
exec_env = bh_list_first_elem(&cluster->exec_env_list);
bh_assert(exec_env);
instance->current_tid = exec_env->handle;
module_inst = wasm_runtime_get_module_inst(exec_env);
bh_assert(module_inst);
/* Allocate linear memory for evaluating expressions during debugging. If
* the allocation failed, the debugger will not be able to evaluate
* expressions */
instance->exec_mem_info.size = DEBUG_EXECUTION_MEMORY_SIZE;
instance->exec_mem_info.start_offset = wasm_runtime_module_malloc(
module_inst, (uint64)instance->exec_mem_info.size, NULL);
if (instance->exec_mem_info.start_offset == 0) {
LOG_WARNING(
"WASM Debug Engine warning: failed to allocate linear memory for "
"execution. \n"
"Will not be able to evaluate expressions during "
"debugging");
}
instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
if (!wasm_debug_control_thread_create(instance, port)) {
LOG_ERROR("WASM Debug Engine error: failed to create control thread");
goto fail3;
}
wasm_cluster_set_debug_inst(cluster, instance);
return instance;
fail3:
os_cond_destroy(&instance->wait_cond);
fail2:
os_mutex_destroy(&instance->wait_lock);
fail1:
wasm_runtime_free(instance);
return NULL;
}
static void
wasm_debug_instance_destroy_breakpoints(WASMDebugInstance *instance)
{
WASMDebugBreakPoint *breakpoint, *next_bp;
breakpoint = bh_list_first_elem(&instance->break_point_list);
while (breakpoint) {
next_bp = bh_list_elem_next(breakpoint);
bh_list_remove(&instance->break_point_list, breakpoint);
wasm_runtime_free(breakpoint);
breakpoint = next_bp;
}
}
static void
wasm_debug_instance_destroy_watchpoints(WASMDebugInstance *instance,
bh_list *watchpoints)
{
WASMDebugWatchPoint *watchpoint, *next;
watchpoint = bh_list_first_elem(watchpoints);
while (watchpoint) {
next = bh_list_elem_next(watchpoint);
bh_list_remove(watchpoints, watchpoint);
wasm_runtime_free(watchpoint);
watchpoint = next;
}
}
void
wasm_debug_instance_destroy(WASMCluster *cluster)
{
WASMDebugInstance *instance = NULL;
if (!g_debug_engine) {
return;
}
instance = cluster->debug_inst;
if (instance) {
/* destroy control thread */
wasm_debug_control_thread_destroy(instance);
os_mutex_lock(&g_debug_engine->instance_list_lock);
bh_list_remove(&g_debug_engine->debug_instance_list, instance);
os_mutex_unlock(&g_debug_engine->instance_list_lock);
/* destroy all breakpoints */
wasm_debug_instance_destroy_breakpoints(instance);
wasm_debug_instance_destroy_watchpoints(
instance, &instance->watch_point_list_read);
wasm_debug_instance_destroy_watchpoints(
instance, &instance->watch_point_list_write);
os_mutex_destroy(&instance->wait_lock);
os_cond_destroy(&instance->wait_cond);
wasm_runtime_free(instance);
cluster->debug_inst = NULL;
}
}
WASMExecEnv *
wasm_debug_instance_get_current_env(WASMDebugInstance *instance)
{
WASMExecEnv *exec_env = NULL;
if (instance) {
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
while (exec_env) {
if (exec_env->handle == instance->current_tid)
break;
exec_env = bh_list_elem_next(exec_env);
}
}
return exec_env;
}
#if WASM_ENABLE_LIBC_WASI != 0
bool
wasm_debug_instance_get_current_object_name(WASMDebugInstance *instance,
char name_buffer[], uint32 len)
{
WASMExecEnv *exec_env;
WASIArguments *wasi_args;
WASMModuleInstance *module_inst;
if (!instance)
return false;
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
if (!exec_env)
return false;
module_inst = (WASMModuleInstance *)exec_env->module_inst;
wasi_args = &module_inst->module->wasi_args;
if (wasi_args && wasi_args->argc > 0) {
char *argv_name = wasi_args->argv[0];
uint32 name_len = (uint32)strlen(argv_name);
printf("the module name is %s\n", argv_name);
if (len - 1 >= name_len)
bh_strcpy_s(name_buffer, len, argv_name);
else
bh_strcpy_s(name_buffer, len, argv_name + (name_len + 1 - len));
return true;
}
return false;
}
#endif
uint64
wasm_debug_instance_get_pid(WASMDebugInstance *instance)
{
if (instance != NULL) {
return (uint64)instance->id;
}
return (uint64)0;
}
korp_tid
wasm_debug_instance_get_tid(WASMDebugInstance *instance)
{
if (instance != NULL) {
return instance->current_tid;
}
return (korp_tid)(uintptr_t)0;
}
uint32
wasm_debug_instance_get_tids(WASMDebugInstance *instance, korp_tid tids[],
uint32 len)
{
WASMExecEnv *exec_env;
uint32 i = 0, threads_num = 0;
if (!instance)
return 0;
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
while (exec_env && i < len) {
/* Some threads may not be ready */
if (exec_env->handle != 0) {
tids[i++] = exec_env->handle;
threads_num++;
}
exec_env = bh_list_elem_next(exec_env);
}
LOG_VERBOSE("find %d tids\n", threads_num);
return threads_num;
}
uint32
wasm_debug_instance_get_thread_status(WASMDebugInstance *instance, korp_tid tid)
{
WASMExecEnv *exec_env = NULL;
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
while (exec_env) {
if (exec_env->handle == tid) {
return (uint32)exec_env->current_status->signal_flag;
}
exec_env = bh_list_elem_next(exec_env);
}
return 0;
}
void
wasm_debug_instance_set_cur_thread(WASMDebugInstance *instance, korp_tid tid)
{
instance->current_tid = tid;
}
uint64
wasm_debug_instance_get_pc(WASMDebugInstance *instance)
{
WASMExecEnv *exec_env;
if (!instance)
return 0;
exec_env = wasm_debug_instance_get_current_env(instance);
if ((exec_env != NULL) && (exec_env->cur_frame != NULL)
&& (exec_env->cur_frame->ip != NULL)) {
WASMModuleInstance *module_inst =
(WASMModuleInstance *)exec_env->module_inst;
return WASM_ADDR(
WasmObj, instance->id,
(exec_env->cur_frame->ip - module_inst->module->load_addr));
}
return 0;
}
uint64
wasm_debug_instance_get_load_addr(WASMDebugInstance *instance)
{
WASMExecEnv *exec_env;
if (!instance)
return WASM_ADDR(WasmInvalid, 0, 0);
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
if (exec_env) {
return WASM_ADDR(WasmObj, instance->id, 0);
}
return WASM_ADDR(WasmInvalid, 0, 0);
}
WASMDebugMemoryInfo *
wasm_debug_instance_get_memregion(WASMDebugInstance *instance, uint64 addr)
{
WASMDebugMemoryInfo *mem_info;
WASMExecEnv *exec_env;
WASMModuleInstance *module_inst;
WASMMemoryInstance *memory;
uint32 num_bytes_per_page;
uint32 linear_mem_size = 0;
if (!instance)
return NULL;
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
if (!exec_env)
return NULL;
if (!(mem_info = wasm_runtime_malloc(sizeof(WASMDebugMemoryInfo)))) {
LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
return NULL;
}
memset(mem_info, 0, sizeof(WASMDebugMemoryInfo));
mem_info->start = WASM_ADDR(WasmInvalid, 0, 0);
mem_info->size = 0;
mem_info->name[0] = '\0';
mem_info->permisson[0] = '\0';
module_inst = (WASMModuleInstance *)exec_env->module_inst;
switch (WASM_ADDR_TYPE(addr)) {
case WasmObj:
if (WASM_ADDR_OFFSET(addr) < module_inst->module->load_size) {
mem_info->start = WASM_ADDR(WasmObj, instance->id, 0);
mem_info->size = module_inst->module->load_size;
snprintf(mem_info->name, sizeof(mem_info->name), "%s",
"module");
snprintf(mem_info->permisson, sizeof(mem_info->permisson), "%s",
"rx");
}
break;
case WasmMemory:
{
memory = wasm_get_default_memory(module_inst);
if (memory) {
num_bytes_per_page = memory->num_bytes_per_page;
linear_mem_size = num_bytes_per_page * memory->cur_page_count;
}
if (WASM_ADDR_OFFSET(addr) < linear_mem_size) {
mem_info->start = WASM_ADDR(WasmMemory, instance->id, 0);
mem_info->size = linear_mem_size;
snprintf(mem_info->name, sizeof(mem_info->name), "%s",
"memory");
snprintf(mem_info->permisson, sizeof(mem_info->permisson), "%s",
"rw");
}
break;
}
default:
mem_info->start = WASM_ADDR(WasmInvalid, 0, 0);
mem_info->size = 0;
}
return mem_info;
}
void
wasm_debug_instance_destroy_memregion(WASMDebugInstance *instance,
WASMDebugMemoryInfo *mem_info)
{
wasm_runtime_free(mem_info);
}
bool
wasm_debug_instance_get_obj_mem(WASMDebugInstance *instance, uint64 offset,
char *buf, uint64 *size)
{
WASMExecEnv *exec_env;
WASMModuleInstance *module_inst;
WASMDebugBreakPoint *breakpoint;
WASMFastOPCodeNode *fast_opcode;
if (!instance)
return false;
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
if (!exec_env)
return false;
module_inst = (WASMModuleInstance *)exec_env->module_inst;
if (offset + *size > module_inst->module->load_size) {
LOG_VERBOSE("wasm_debug_instance_get_data_mem size over flow!\n");
*size = module_inst->module->load_size >= offset
? module_inst->module->load_size - offset
: 0;
}
bh_memcpy_s(buf, (uint32)*size, module_inst->module->load_addr + offset,
(uint32)*size);
breakpoint = bh_list_first_elem(&instance->break_point_list);
while (breakpoint) {
if (offset <= breakpoint->addr && breakpoint->addr < offset + *size) {
bh_memcpy_s(buf + (breakpoint->addr - offset), sizeof(break_instr),
&breakpoint->orignal_data, sizeof(break_instr));
}
breakpoint = bh_list_elem_next(breakpoint);
}
fast_opcode = bh_list_first_elem(&module_inst->module->fast_opcode_list);
while (fast_opcode) {
if (offset <= fast_opcode->offset
&& fast_opcode->offset < offset + *size) {
*(uint8 *)(buf + (fast_opcode->offset - offset)) =
fast_opcode->orig_op;
}
fast_opcode = bh_list_elem_next(fast_opcode);
}
return true;
}
bool
wasm_debug_instance_get_linear_mem(WASMDebugInstance *instance, uint64 offset,
char *buf, uint64 *size)
{
WASMExecEnv *exec_env;
WASMModuleInstance *module_inst;
WASMMemoryInstance *memory;
uint32 num_bytes_per_page;
uint32 linear_mem_size;
if (!instance)
return false;
exec_env = wasm_debug_instance_get_current_env(instance);
if (!exec_env)
return false;
module_inst = (WASMModuleInstance *)exec_env->module_inst;
memory = wasm_get_default_memory(module_inst);
if (memory) {
num_bytes_per_page = memory->num_bytes_per_page;
linear_mem_size = num_bytes_per_page * memory->cur_page_count;
if (offset + *size > linear_mem_size) {
LOG_VERBOSE("wasm_debug_instance_get_linear_mem size over flow!\n");
*size = linear_mem_size >= offset ? linear_mem_size - offset : 0;
}
bh_memcpy_s(buf, (uint32)*size, memory->memory_data + offset,
(uint32)*size);
return true;
}
return false;
}
bool
wasm_debug_instance_set_linear_mem(WASMDebugInstance *instance, uint64 offset,
char *buf, uint64 *size)
{
WASMExecEnv *exec_env;
WASMModuleInstance *module_inst;
WASMMemoryInstance *memory;
uint32 num_bytes_per_page;
uint32 linear_mem_size;
if (!instance)
return false;
exec_env = wasm_debug_instance_get_current_env(instance);
if (!exec_env)
return false;
module_inst = (WASMModuleInstance *)exec_env->module_inst;
memory = wasm_get_default_memory(module_inst);
if (memory) {
num_bytes_per_page = memory->num_bytes_per_page;
linear_mem_size = num_bytes_per_page * memory->cur_page_count;
if (offset + *size > linear_mem_size) {
LOG_VERBOSE("wasm_debug_instance_get_linear_mem size over flow!\n");
*size = linear_mem_size >= offset ? linear_mem_size - offset : 0;
}
bh_memcpy_s(memory->memory_data + offset, (uint32)*size, buf,
(uint32)*size);
return true;
}
return false;
}
bool
wasm_debug_instance_get_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
uint64 *size)
{
switch (WASM_ADDR_TYPE(addr)) {
case WasmMemory:
return wasm_debug_instance_get_linear_mem(
instance, WASM_ADDR_OFFSET(addr), buf, size);
break;
case WasmObj:
return wasm_debug_instance_get_obj_mem(
instance, WASM_ADDR_OFFSET(addr), buf, size);
break;
default:
return false;
}
}
bool
wasm_debug_instance_set_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
uint64 *size)
{
switch (WASM_ADDR_TYPE(addr)) {
case WasmMemory:
return wasm_debug_instance_set_linear_mem(
instance, WASM_ADDR_OFFSET(addr), buf, size);
break;
case WasmObj:
default:
return false;
}
}
WASMDebugInstance *
wasm_exec_env_get_instance(WASMExecEnv *exec_env)
{
WASMDebugInstance *instance = NULL;
if (!g_debug_engine) {
return NULL;
}
os_mutex_lock(&g_debug_engine->instance_list_lock);
instance = bh_list_first_elem(&g_debug_engine->debug_instance_list);
while (instance) {
if (instance->cluster == exec_env->cluster)
break;
instance = bh_list_elem_next(instance);
}
os_mutex_unlock(&g_debug_engine->instance_list_lock);
return instance;
}
uint32
wasm_debug_instance_get_call_stack_pcs(WASMDebugInstance *instance,
korp_tid tid, uint64 buf[], uint64 size)
{
WASMExecEnv *exec_env;
struct WASMInterpFrame *frame;
uint32 i = 0;
if (!instance)
return 0;
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
while (exec_env) {
if (exec_env->handle == tid) {
WASMModuleInstance *module_inst =
(WASMModuleInstance *)exec_env->module_inst;
frame = exec_env->cur_frame;
while (frame && i < size) {
if (frame->ip != NULL) {
buf[i++] =
WASM_ADDR(WasmObj, instance->id,
(frame->ip - module_inst->module->load_addr));
}
frame = frame->prev_frame;
}
return i;
}
exec_env = bh_list_elem_next(exec_env);
}
return 0;
}
bool
wasm_debug_instance_add_breakpoint(WASMDebugInstance *instance, uint64 addr,
uint64 length)
{
WASMExecEnv *exec_env;
WASMModuleInstance *module_inst;
uint64 offset;
if (!instance)
return false;
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
if (!exec_env)
return false;
module_inst = (WASMModuleInstance *)exec_env->module_inst;
if (WASM_ADDR_TYPE(addr) != WasmObj)
return false;
offset = WASM_ADDR_OFFSET(addr);
if (length >= sizeof(break_instr)) {
if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
WASMDebugBreakPoint *breakpoint;
if (!(breakpoint =
wasm_runtime_malloc(sizeof(WASMDebugBreakPoint)))) {
LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
return false;
}
memset(breakpoint, 0, sizeof(WASMDebugBreakPoint));
breakpoint->addr = offset;
/* TODO: how to if more than one breakpoints are set
at the same addr? */
bh_memcpy_s(&breakpoint->orignal_data, (uint32)sizeof(break_instr),
module_inst->module->load_addr + offset,
(uint32)sizeof(break_instr));
bh_memcpy_s(module_inst->module->load_addr + offset,
(uint32)sizeof(break_instr), break_instr,
(uint32)sizeof(break_instr));
bh_list_insert(&instance->break_point_list, breakpoint);
return true;
}
}
return false;
}
bool
wasm_debug_instance_remove_breakpoint(WASMDebugInstance *instance, uint64 addr,
uint64 length)
{
WASMExecEnv *exec_env;
WASMModuleInstance *module_inst;
uint64 offset;
if (!instance)
return false;
exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
if (!exec_env)
return false;
module_inst = (WASMModuleInstance *)exec_env->module_inst;
if (WASM_ADDR_TYPE(addr) != WasmObj)
return false;
offset = WASM_ADDR_OFFSET(addr);
if (length >= sizeof(break_instr)) {
if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
WASMDebugBreakPoint *breakpoint =