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
|
/*
* wadb, a WebAssembly debugger
* Copyright (C) 2025 Xavier Del Campo Romero
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define _POSIX_C_SOURCE 200809L
#include <dwarf.h>
#include <libdwarf/libdwarf.h>
#include "types.h"
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum
{
DEBUG_EMPTY,
DEBUG_ABBREV,
DEBUG_INFO,
DEBUG_STR,
DEBUG_ARANGES,
DEBUG_FRAME,
DEBUG_LINE,
DEBUG_LOC,
DEBUG_MACINFO,
DEBUG_PUBNAMES,
DEBUG_PUBTYPES,
DEBUG_RANGES,
DEBUG_TYPES,
NSECTIONS
};
struct dwarf
{
FILE *f;
struct dwsection
{
long offset;
varuint32 len;
void *data;
} sections[NSECTIONS];
};
struct header
{
Dwarf_Sig8 type_signature;
Dwarf_Unsigned cu_header_length, typeoffset, next_cu_header_offset;
Dwarf_Half version_stamp, address_size, length_size, extension_size,
header_cu_type;
Dwarf_Off abbrev_offset;
};
static const char *const names[NSECTIONS] =
{
[DEBUG_EMPTY] = "",
[DEBUG_ABBREV] = ".debug_abbrev",
[DEBUG_ARANGES] = ".debug_aranges",
[DEBUG_FRAME] = ".debug_frame",
[DEBUG_INFO] = ".debug_info",
[DEBUG_LINE] = ".debug_line",
[DEBUG_LOC] = ".debug_loc",
[DEBUG_MACINFO] = ".debug_macinfo",
[DEBUG_PUBNAMES] = ".debug_pubnames",
[DEBUG_PUBTYPES] = ".debug_pubtypes",
[DEBUG_RANGES] = ".debug_ranges",
[DEBUG_STR] = ".debug_str",
[DEBUG_TYPES] = ".debug_types"
};
static void errcb(const Dwarf_Error e, const Dwarf_Ptr args)
{
}
static int get_section_info(void *const obj, const Dwarf_Half section_index,
Dwarf_Obj_Access_Section *const return_section, int *const error)
{
const struct dwarf *const dw = obj;
if (section_index >= sizeof dw->sections / sizeof *dw->sections)
{
fprintf(stderr, "%s: invalid section_index: %hd\n", __func__,
section_index);
return DW_DLV_ERROR;
}
const struct dwsection *const s = &dw->sections[section_index];
if (!names[section_index] && !s->len)
return DW_DLV_NO_ENTRY;
*return_section = (const Dwarf_Obj_Access_Section)
{
.size = s->len,
.addr = s->offset,
.name = names[section_index]
};
return 0;
}
static Dwarf_Endianness get_byte_order(void *const obj)
{
return DW_OBJECT_LSB;
}
static Dwarf_Small get_length_size(void *const obj)
{
return sizeof (uint32_t);
}
static Dwarf_Small get_pointer_size(void *const obj)
{
return sizeof (uint32_t);
}
static Dwarf_Unsigned get_section_count(void *const obj)
{
const struct dwarf *const dw = obj;
Dwarf_Unsigned ret = 0;
for (size_t i = 0; i < sizeof dw->sections / sizeof *dw->sections; i++)
if (dw->sections[i].offset)
ret++;
return ret;
}
static int load_section(void *const obj, const Dwarf_Half section_index,
Dwarf_Small **const return_data, int *const error)
{
struct dwarf *const dw = obj;
FILE *const f = dw->f;
Dwarf_Small *p = NULL;
if (section_index >= NSECTIONS)
{
fprintf(stderr, "%s: invalid section_index %hd\n", __func__,
section_index);
goto failure;
}
struct dwsection *const s = &dw->sections[section_index];
const char *const name = names[section_index];
if (!s->offset)
{
fprintf(stderr, "%s: unavailable section: %s\n", __func__, name);
goto failure;
}
else if (!s->len)
{
fprintf(stderr, "%s: unexpected zero length for section: %s\n",
__func__, name);
goto failure;
}
else if (!(p = malloc(s->len)))
{
fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
goto failure;
}
else if (fseek(f, s->offset, SEEK_SET))
{
fprintf(stderr, "%s: fseek(3): %s\n", __func__, strerror(errno));
goto failure;
}
else if (!fread(p, s->len, 1, f))
{
fprintf(stderr, "%s: fread(3) failed, feof=%d, ferror=%d\n", __func__,
feof(f), ferror(f));
goto failure;
}
s->data = p;
*return_data = p;
return DW_DLV_OK;
failure:
free(p);
return DW_DLV_ERROR;
}
static int check_header(FILE *const f)
{
uint8_t magic[sizeof "asm"], version[sizeof (uint32_t)];
static const uint8_t exp[sizeof version] = {1, 0, 0, 0};
if (!fread(magic, sizeof magic, 1, f))
{
fprintf(stderr, "%s: fread(3) magic failed, feof=%d, ferror=%d\n",
__func__, feof(f), ferror(f));
return -1;
}
else if (memcmp(magic, "\0asm", sizeof magic))
{
fprintf(stderr, "%s: invalid magic number\n", __func__);
return -1;
}
else if (!fread(version, sizeof version, 1, f))
{
fprintf(stderr, "%s: fread(3) version failed, feof=%d, ferror=%d\n",
__func__, feof(f), ferror(f));
return -1;
}
else if (memcmp(version, exp, sizeof version))
{
fprintf(stderr, "%s: invalid version number\n", __func__);
return -1;
}
return 0;
}
static char *read_name(FILE *const f)
{
varuint32 name_len;
char *name = malloc(1);
if (!name)
{
fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
goto failure;
}
else if (read_varuint32(f, &name_len))
{
fprintf(stderr, "%s: read_varuint32 failed\n", __func__);
goto failure;
}
for (varuint32 i = 0; i < name_len; i++)
{
uint8_t c;
const size_t r = fread(&c, sizeof c, 1, f);
if (!r)
{
fprintf(stderr, "%s: fread(3) failed, ferror(3)=%d, feof(3)=%d\n",
__func__, ferror(f), feof(f));
goto failure;
}
char *const newname = realloc(name, i + 2);
if (!newname)
{
fprintf(stderr, "%s: realloc(3): %s\n", __func__, strerror(errno));
goto failure;
}
name = newname;
name[i] = c;
}
name[name_len] = '\0';
return name;
failure:
free(name);
return NULL;
}
struct section
{
varuint7 code;
varuint32 len;
char *name;
};
static int parse_header(FILE *const f, struct section *const s)
{
varuint7 code;
varuint32 payload_len;
char *name = NULL;
long before, after;
if (read_varuint7(f, &code))
{
/* Exceptionally, this might not be a fatal error. */
if (!feof(f))
fprintf(stderr, "%s: read_varuint7 failed\n", __func__);
goto failure;
}
else if (read_varuint32(f, &payload_len))
{
fprintf(stderr, "%s: read_varuint32 failed\n", __func__);
goto failure;
}
else if ((before = ftell(f)) < 0)
{
fprintf(stderr, "%s: ftell(3) before: %s\n", __func__, strerror(errno));
goto failure;
}
else if (!code && !(name = read_name(f)))
{
fprintf(stderr, "%s: read_name failed\n", __func__);
goto failure;
}
else if ((after = ftell(f)) < 0)
{
fprintf(stderr, "%s: ftell(3) after: %s\n", __func__, strerror(errno));
goto failure;
}
const unsigned long sz = after - before;
if (payload_len < sz)
{
fprintf(stderr, "%s: expected payload_len >= %lu, got %lu\n",
__func__, sz, (unsigned long)payload_len);
goto failure;
}
payload_len -= sz;
*s = (const struct section)
{
.code = code,
.len = payload_len,
.name = name
};
return 0;
failure:
free(name);
return -1;
}
static int skip(FILE *const f, const struct section *const s)
{
const int ret = fseek(f, s->len, SEEK_CUR);
if (ret)
fprintf(stderr, "%s: fseek(3): %s\n", __func__, strerror(errno));
return ret;
}
static struct dwsection *getdws(struct dwarf *const dw,
const char *const name)
{
for (size_t i = 0; i < sizeof names / sizeof *names; i++)
if (!strcmp(name, names[i]))
return &dw->sections[i];
return NULL;
}
static int read_section(FILE *const f, struct dwarf *const dw)
{
int ret = -1;
struct section s = {0};
struct dwsection *dws;
long offset;
if (parse_header(f, &s))
{
/* Exceptionally, this might not be a fatal error. */
if (!feof(f))
fprintf(stderr, "%s: parse_header failed\n", __func__);
goto end;
}
else if (s.code != SECTION_CUSTOM || !(dws = getdws(dw, s.name)))
{
if (skip(f, &s))
{
fprintf(stderr, "%s: skip failed\n", __func__);
goto end;
}
}
else if ((offset = ftell(f)) < 0)
{
fprintf(stderr, "%s: ftell(3): %s\n", __func__, strerror(errno));
goto end;
}
else
{
*dws = (const struct dwsection)
{
.len = s.len,
.offset = offset
};
if (skip(f, &s))
{
fprintf(stderr, "%s: skip failed\n", __func__);
goto end;
}
}
ret = 0;
end:
free(s.name);
return ret;
}
static int parse(struct dwarf *const dw)
{
FILE *const f = dw->f;
if (check_header(f))
{
fprintf(stderr, "%s: check_magic failed\n", __func__);
return -1;
}
for (;;)
if (read_section(f, dw))
{
if (feof(f))
break;
fprintf(stderr, "%s: read_section failed\n", __func__);
return -1;
}
return 0;
}
static bool
isformstring(const Dwarf_Half form)
{
switch (form)
{
case DW_FORM_string:
case DW_FORM_GNU_strp_alt:
case DW_FORM_GNU_str_index:
case DW_FORM_strx:
case DW_FORM_strx1:
case DW_FORM_strx2:
case DW_FORM_strx3:
case DW_FORM_strx4:
case DW_FORM_strp:
return true;
default:
break;
}
return false;
}
static int read_attr(const Dwarf_Attribute a, const unsigned level,
Dwarf_Error *const e)
{
Dwarf_Half num, form;
const char *atname, *fname;
char *s = NULL;
int res = dwarf_whatattr(a, &num, e);
if (res)
{
fprintf(stderr, "%s: dwarf_whatattr failed with %d\n", __func__, res);
return -1;
}
else if ((res = dwarf_whatform(a, &form, e)))
{
fprintf(stderr, "%s: dwarf_whatform failed with %d\n", __func__, res);
return -1;
}
else if ((res = dwarf_get_AT_name(num, &atname)))
{
fprintf(stderr, "%s: dwarf_get_AT_name failed with %d\n", __func__,
res);
return -1;
}
else if ((res = dwarf_get_FORM_name(form, &fname)))
{
fprintf(stderr, "%s: dwarf_get_FORM_name failed with %d\n", __func__,
res);
return -1;
}
else if (isformstring(form) && (res = dwarf_formstring(a, &s, e)))
{
fprintf(stderr, "%s: dwarf_formstring failed with %d\n", __func__, res);
return -1;
}
#if 0
for (unsigned i = 0; i < level; i++)
putchar('\t');
printf("atname=%s, fname=%s", atname, fname);
if (s)
printf(", s=%s", s);
putchar('\n');
#endif
return 0;
}
static int print_die(const Dwarf_Debug dbg, const Dwarf_Die die,
const unsigned level, Dwarf_Error *const e)
{
Dwarf_Half tag;
Dwarf_Attribute *attrs = NULL;
Dwarf_Signed nattr = 0;
const char *name;
int ret = -1, r = dwarf_tag(die, &tag, e);
if (r)
{
fprintf(stderr, "%s: dwarf_tag failed with %d\n", __func__, r);
goto end;
}
else if ((r = dwarf_get_TAG_name(tag, &name)))
name = "<bogus tag>";
if ((r = dwarf_attrlist(die, &attrs, &nattr, e)))
{
fprintf(stderr, "%s: dwarf_attrlist failed with %d\n", __func__, r);
goto end;
}
for (Dwarf_Signed i = 0; i < nattr; i++)
if (read_attr(attrs[i], level, e))
{
fprintf(stderr, "%s: failed to read attribute %lld\n", __func__, i);
goto end;
}
ret = 0;
end:
for (Dwarf_Signed i = 0; i < nattr; i++)
dwarf_dealloc_attribute(attrs[i]);
dwarf_dealloc(dbg, attrs, DW_DLA_LIST);
return ret;
}
static int read_children(const Dwarf_Debug dbg, const Dwarf_Die die,
const unsigned level, Dwarf_Error *const e)
{
int ret = -1;
Dwarf_Die child;
int r;
if (print_die(dbg, die, level, e))
{
fprintf(stderr, "%s: failed to print die\n", __func__);
return -1;
}
else if (!(r = dwarf_child(die, &child, e)))
{
ret = read_children(dbg, child, level + 1, e);
dwarf_dealloc_die(child);
return ret;
}
else if (r == DW_DLV_ERROR)
{
fprintf(stderr, "%s: dwarf_child failed\n", __func__);
return -1;
}
Dwarf_Die d = die;
for (;;)
{
Dwarf_Die sibling;
if (!(r = dwarf_siblingof(dbg, d, &sibling, e)))
{
ret = read_children(dbg, sibling, level, e);
if (ret)
return ret;
}
else if (r == DW_DLV_ERROR)
{
fprintf(stderr, "%s: dwarf_siblingof failed\n", __func__);
return -1;
}
else if (r == DW_DLV_NO_ENTRY)
break;
d = sibling;
}
return 0;
}
static int read_header(const Dwarf_Debug dbg, struct header *const h,
Dwarf_Error *const e)
{
Dwarf_Die die = 0;
int ret = -1, r = dwarf_next_cu_header_d(dbg, 1, &h->cu_header_length,
&h->version_stamp, &h->abbrev_offset, &h->address_size,
&h->length_size, &h->extension_size, &h->type_signature,
&h->typeoffset, &h->next_cu_header_offset, &h->header_cu_type, e);
if (r != DW_DLV_OK)
{
fprintf(stderr, "%s: dwarf_next_cu_header_d failed with %d\n",
__func__, r);
return -1;
}
else if ((r = dwarf_siblingof(dbg, NULL, &die, e)))
{
fprintf(stderr, "%s: dwarf_siblingof failed with %d\n", __func__, r);
goto end;
}
else if (read_children(dbg, die, 0, e))
{
fprintf(stderr, "%s: failed to read children\n", __func__);
goto end;
}
ret = 0;
end:
dwarf_dealloc_die(die);
return ret;
}
static int init_client(const char *const tgt)
{
int ret = -1, fd = -1;
char *hostdup = NULL, *portdup = NULL;
const char *sep = strchr(tgt, ':'), *host, *port;
struct addrinfo *pai = NULL;
unsigned long portv;
if (!sep)
{
host = tgt;
port = "3333";
}
else
{
if (!*(sep + 1))
{
fprintf(stderr, "%s: missing port number\n", __func__);
goto end;
}
else if (sep == tgt)
host = "localhost";
else if (!(hostdup = strndup(tgt, sep - tgt)))
{
perror("strndup(3)");
goto end;
}
else
host = hostdup;
if (!(portdup = strdup(sep + 1)))
{
fprintf(stderr, "%s: failed to duplicate string: %s\n", __func__,
strerror(errno));
goto end;
}
else
port = portdup;
}
char *end;
int error;
const struct addrinfo ai =
{
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM
};
errno = 0;
portv = strtoul(port, &end, 10);
if (errno || *end || portv > USHRT_MAX)
{
fprintf(stderr, "%s: invalid port number: %s\n", __func__, port);
goto end;
}
else if ((error = getaddrinfo(host, port, &ai, &pai)))
{
fprintf(stderr, "%s: getaddrinfo(3) %s:%s: %s\n", __func__,
host, port, gai_strerror(error));
goto end;
}
for (const struct addrinfo *a = pai; a; a = a->ai_next)
{
if ((fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) < 0)
{
perror("socket(2)");
continue;
}
else if (connect(fd, a->ai_addr, a->ai_addrlen))
{
fprintf(stderr, "%s: connect(2) %s:%s: %s\n", __func__,
host, port, strerror(errno));
goto end;
}
break;
}
ret = fd;
end:
if (ret < 0 && fd >= 0 && close(fd))
perror("close(2)");
if (pai)
freeaddrinfo(pai);
free(hostdup);
free(portdup);
return ret;
}
struct verb
{
const char *verb;
int (*f)(void);
};
static int help(void);
static const struct verb verbs[] =
{
{"?", help},
{"help", help}
};
static int help(void)
{
return -1;
}
static int process(const char *const l)
{
return 0;
}
static int run(const int fd)
{
int ret = -1;
char *l;
size_t n = 0;
ssize_t r;
printf("(wadb) ");
fflush(stdout);
if ((r = getline(&l, &n, stdin) < 0))
{
if (feof(stdin))
{
putchar('\n');
return 1;
}
else
perror("getline(3)");
goto end;
}
*strchr(l, '\n') = '\0';
if (process(l))
goto end;
ret = 0;
end:
free(l);
return ret;
}
int main(int argc, char *argv[])
{
static const Dwarf_Obj_Access_Methods methods =
{
.get_section_info = get_section_info,
.get_byte_order = get_byte_order,
.get_length_size = get_length_size,
.get_pointer_size = get_pointer_size,
.get_section_count = get_section_count,
.load_section = load_section
};
int ret = EXIT_FAILURE, result, fd = -1;
struct dwarf dw = {0};
Dwarf_Obj_Access_Interface aitf = {.object = &dw, .methods = &methods};
Dwarf_Debug dbg = 0;
Dwarf_Error error = 0;
struct header h;
if (argc < 2)
{
fprintf(stderr, "%s [-h <host:port>] <wasm-file> [args ...]\n", *argv);
goto end;
}
const char *hostport = "localhost:3333";
int opt;
while ((opt = getopt(argc, argv, "h:")) != -1)
switch (opt)
{
case 'h':
hostport = optarg;
break;
}
const char *const path = argv[optind];
if (!(dw.f = fopen(path, "rb")))
{
fprintf(stderr, "fopen(3) %s: %s\n", path, strerror(errno));
goto end;
}
else if (parse(&dw))
{
fprintf(stderr, "failed to parse %s\n", path);
goto end;
}
else if ((result = dwarf_object_init(&aitf, errcb, &dw, &dbg, &error))
!= DW_DLV_OK)
{
fprintf(stderr, "dwarf_object_init failed with %d: %s\n", result,
dwarf_errmsg(error));
goto end;
}
else if (read_header(dbg, &h, &error))
{
fprintf(stderr, "failed to read header\n");
goto end;
}
else if ((fd = init_client(hostport)) < 0)
{
fprintf(stderr, "failed to init client\n");
goto end;
}
while (!(result = run(fd)))
;
if (result < 0)
goto end;
ret = EXIT_SUCCESS;
end:
if (fd >= 0 && close(fd))
{
perror("close(2)");
ret = EXIT_FAILURE;
}
for (size_t i = 0; i < sizeof dw.sections / sizeof *dw.sections; i++)
free(dw.sections[i].data);
if (dbg && dwarf_object_finish(dbg, &error))
{
fprintf(stderr, "dwarf_object_finish: %s\n", dwarf_errmsg(error));
ret = EXIT_FAILURE;
}
if (dw.f && fclose(dw.f))
{
fprintf(stderr, "fclose(3) %s: %s\n", path, strerror(errno));
ret = EXIT_FAILURE;
}
return ret;
}
|