Skip to content

Commit 82d8dc7

Browse files
authored
Handle debug info without a filename in asm2wasm (#1249)
* support debug info without a filename in asm2wasm input (which can happen if llvm doesn't know the file, only the line)
1 parent 42acc22 commit 82d8dc7

14 files changed

+69
-13
lines changed

src/asm2wasm.h

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,43 @@ struct Asm2WasmPreProcessor {
262262
char* out = copy;
263263
std::string DEBUGINFO_INTRINSIC = EMSCRIPTEN_DEBUGINFO.str;
264264
auto DEBUGINFO_INTRINSIC_SIZE = DEBUGINFO_INTRINSIC.size();
265+
const char* UNKNOWN_FILE = "(unknown)";
265266
bool seenUseAsm = false;
266267
while (input[0]) {
267268
if (out + ADD_FACTOR >= end) {
268269
Fatal() << "error in handling debug info";
269270
}
270271
if (startsWith(input, "//@line")) {
271272
char* linePos = input + 8;
272-
char* lineEnd = strchr(input + 8, ' ');
273-
char* filePos = strchr(lineEnd, '"') + 1;
274-
char* fileEnd = strchr(filePos, '"');
275-
input = fileEnd + 1;
273+
char* lineEnd = strpbrk(input + 8, " \n");
274+
if (!lineEnd) {
275+
// comment goes to end of input
276+
break;
277+
}
278+
input = lineEnd + 1;
279+
std::string file;
280+
if (*lineEnd == ' ') {
281+
// we have a file
282+
char* filePos = strpbrk(input, "\"\n");
283+
if (!filePos) {
284+
// goes to end of input
285+
break;
286+
}
287+
if (*filePos == '"') {
288+
char* fileEnd = strpbrk(filePos + 1, "\"\n");
289+
input = fileEnd + 1;
290+
*fileEnd = 0;
291+
file = filePos + 1;
292+
} else {
293+
file = UNKNOWN_FILE;
294+
input = filePos + 1;
295+
}
296+
} else {
297+
// no file, we found \n
298+
file = UNKNOWN_FILE;
299+
}
276300
*lineEnd = 0;
277-
*fileEnd = 0;
278-
std::string line = linePos, file = filePos;
301+
std::string line = linePos;
279302
auto iter = debugInfoFileIndices.find(file);
280303
if (iter == debugInfoFileIndices.end()) {
281304
Index index = debugInfoFileNames.size();

test/debugInfo.asm.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ function () {
8181
switch_reach($p) | 0;
8282
return ($rc$0|0); //@line 59950 "/tmp/emscripten_test_binaryen2_28hnAe/src.c"
8383
}
84-
return { add: add, ret: ret, opts: opts, fib: fib, switch_reach: switch_reach };
84+
function nofile() {
85+
nofile(); //@line 1337
86+
}
87+
return { add: add, ret: ret, opts: opts, fib: fib, switch_reach: switch_reach, nofile: nofile };
8588
}
8689

test/debugInfo.fromasm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(export "opts" (func $opts))
1111
(export "fib" (func $fib))
1212
(export "switch_reach" (func $switch_reach))
13+
(export "nofile" (func $nofile))
1314
(func $add (; 0 ;) (param $0 i32) (param $1 i32) (result i32)
1415
;;@ tests/other_file.cpp:314159:0
1516
(i32.add
@@ -188,4 +189,8 @@
188189
;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0
189190
(get_local $1)
190191
)
192+
(func $nofile (; 5 ;)
193+
;;@ (unknown):1337:0
194+
(call $nofile)
195+
)
191196
)

test/debugInfo.fromasm.clamp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(export "opts" (func $opts))
1111
(export "fib" (func $fib))
1212
(export "switch_reach" (func $switch_reach))
13+
(export "nofile" (func $nofile))
1314
(func $add (; 0 ;) (param $0 i32) (param $1 i32) (result i32)
1415
;;@ tests/other_file.cpp:314159:0
1516
(i32.add
@@ -188,4 +189,8 @@
188189
;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0
189190
(get_local $1)
190191
)
192+
(func $nofile (; 5 ;)
193+
;;@ (unknown):1337:0
194+
(call $nofile)
195+
)
191196
)

test/debugInfo.fromasm.clamp.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/debugInfo.fromasm.clamp.no-opts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(export "opts" (func $opts))
1111
(export "fib" (func $fib))
1212
(export "switch_reach" (func $switch_reach))
13+
(export "nofile" (func $nofile))
1314
(func $add (; 0 ;) (param $x i32) (param $y i32) (result i32)
1415
;;@ tests/hello_world.c:5:0
1516
(set_local $x
@@ -282,4 +283,8 @@
282283
(get_local $$rc$0)
283284
)
284285
)
286+
(func $nofile (; 6 ;)
287+
;;@ (unknown):1337:0
288+
(call $nofile)
289+
)
285290
)

test/debugInfo.fromasm.clamp.no-opts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/debugInfo.fromasm.imprecise

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
(export "opts" (func $opts))
1010
(export "fib" (func $fib))
1111
(export "switch_reach" (func $switch_reach))
12+
(export "nofile" (func $nofile))
1213
(func $add (; 0 ;) (param $0 i32) (param $1 i32) (result i32)
1314
;;@ tests/other_file.cpp:314159:0
1415
(i32.add
@@ -180,4 +181,8 @@
180181
;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0
181182
(get_local $1)
182183
)
184+
(func $nofile (; 5 ;)
185+
;;@ (unknown):1337:0
186+
(call $nofile)
187+
)
183188
)

test/debugInfo.fromasm.imprecise.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/debugInfo.fromasm.imprecise.no-opts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(export "opts" (func $opts))
1111
(export "fib" (func $fib))
1212
(export "switch_reach" (func $switch_reach))
13+
(export "nofile" (func $nofile))
1314
(func $add (; 0 ;) (param $x i32) (param $y i32) (result i32)
1415
;;@ tests/hello_world.c:5:0
1516
(set_local $x
@@ -270,4 +271,8 @@
270271
(get_local $$rc$0)
271272
)
272273
)
274+
(func $nofile (; 5 ;)
275+
;;@ (unknown):1337:0
276+
(call $nofile)
277+
)
273278
)

0 commit comments

Comments
 (0)