Skip to content

Commit 3da4f37

Browse files
authored
cgen: improve generated source compatibility with latest Alpine (lacking libexecinfo-dev and execinfo.h) and the prebuilt tcc (#16743)
1 parent e519bdf commit 3da4f37

File tree

2 files changed

+55
-52
lines changed

2 files changed

+55
-52
lines changed

vlib/builtin/builtin_nix.c.v

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -75,52 +75,53 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
7575
$if tinyc {
7676
C.tcc_backtrace(c'Backtrace')
7777
return false
78-
}
79-
buffer := [100]voidptr{}
80-
nr_ptrs := C.backtrace(&buffer[0], 100)
81-
if nr_ptrs < 2 {
82-
eprintln('C.backtrace returned less than 2 frames')
83-
return false
84-
}
85-
nr_actual_frames := nr_ptrs - skipframes
86-
mut sframes := []string{}
87-
//////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)
88-
csymbols := C.backtrace_symbols(voidptr(&buffer[skipframes]), nr_actual_frames)
89-
for i in 0 .. nr_actual_frames {
90-
sframes << unsafe { tos2(&u8(csymbols[i])) }
91-
}
92-
for sframe in sframes {
93-
executable := sframe.all_before('(')
94-
addr := sframe.all_after('[').all_before(']')
95-
beforeaddr := sframe.all_before('[')
96-
cmd := 'addr2line -e ${executable} ${addr}'
97-
// taken from os, to avoid depending on the os module inside builtin.v
98-
f := C.popen(&char(cmd.str), c'r')
99-
if f == unsafe { nil } {
100-
eprintln(sframe)
101-
continue
102-
}
103-
buf := [1000]u8{}
104-
mut output := ''
105-
unsafe {
106-
bp := &buf[0]
107-
for C.fgets(&char(bp), 1000, f) != 0 {
108-
output += tos(bp, vstrlen(bp))
109-
}
78+
} $else {
79+
buffer := [100]voidptr{}
80+
nr_ptrs := C.backtrace(&buffer[0], 100)
81+
if nr_ptrs < 2 {
82+
eprintln('C.backtrace returned less than 2 frames')
83+
return false
11084
}
111-
output = output.trim_space() + ':'
112-
if C.pclose(f) != 0 {
113-
eprintln(sframe)
114-
continue
85+
nr_actual_frames := nr_ptrs - skipframes
86+
mut sframes := []string{}
87+
//////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)
88+
csymbols := C.backtrace_symbols(voidptr(&buffer[skipframes]), nr_actual_frames)
89+
for i in 0 .. nr_actual_frames {
90+
sframes << unsafe { tos2(&u8(csymbols[i])) }
11591
}
116-
if output in ['??:0:', '??:?:'] {
117-
output = ''
92+
for sframe in sframes {
93+
executable := sframe.all_before('(')
94+
addr := sframe.all_after('[').all_before(']')
95+
beforeaddr := sframe.all_before('[')
96+
cmd := 'addr2line -e ${executable} ${addr}'
97+
// taken from os, to avoid depending on the os module inside builtin.v
98+
f := C.popen(&char(cmd.str), c'r')
99+
if f == unsafe { nil } {
100+
eprintln(sframe)
101+
continue
102+
}
103+
buf := [1000]u8{}
104+
mut output := ''
105+
unsafe {
106+
bp := &buf[0]
107+
for C.fgets(&char(bp), 1000, f) != 0 {
108+
output += tos(bp, vstrlen(bp))
109+
}
110+
}
111+
output = output.trim_space() + ':'
112+
if C.pclose(f) != 0 {
113+
eprintln(sframe)
114+
continue
115+
}
116+
if output in ['??:0:', '??:?:'] {
117+
output = ''
118+
}
119+
// See http://wiki.dwarfstd.org/index.php?title=Path_Discriminators
120+
// Note: it is shortened here to just d. , just so that it fits, and so
121+
// that the common error file:lineno: line format is enforced.
122+
output = output.replace(' (discriminator', ': (d.')
123+
eprintln('${output:-55s} | ${addr:14s} | ${beforeaddr}')
118124
}
119-
// See http://wiki.dwarfstd.org/index.php?title=Path_Discriminators
120-
// Note: it is shortened here to just d. , just so that it fits, and so
121-
// that the common error file:lineno: line format is enforced.
122-
output = output.replace(' (discriminator', ': (d.')
123-
eprintln('${output:-55s} | ${addr:14s} | ${beforeaddr}')
124125
}
125126
}
126127
}

vlib/v/gen/c/cheaders.v

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ const c_common_macros = '
318318
#define EMPTY_STRUCT_INITIALIZATION 0
319319
#endif
320320
321+
#ifndef _WIN32
322+
#if defined __has_include
323+
#if __has_include (<execinfo.h>)
324+
#include <execinfo.h>
325+
#else
326+
// On linux: int backtrace(void **__array, int __size);
327+
// On BSD: size_t backtrace(void **, size_t);
328+
#endif
329+
#endif
330+
#endif
331+
321332
#ifdef __TINYC__
322333
#define _Atomic volatile
323334
#undef EMPTY_STRUCT_DECLARATION
@@ -333,7 +344,6 @@ const c_common_macros = '
333344
#define TCCSKIP(x)
334345
// #include <byteswap.h>
335346
#ifndef _WIN32
336-
#include <execinfo.h>
337347
int tcc_backtrace(const char *fmt, ...);
338348
#endif
339349
#endif
@@ -501,14 +511,6 @@ typedef int (*qsort_callback_func)(const void*, const void*);
501511
#include <stdlib.h>
502512
#include <string.h>
503513
504-
#ifndef _WIN32
505-
#if defined __has_include
506-
#if __has_include (<execinfo.h>)
507-
#include <execinfo.h>
508-
#endif
509-
#endif
510-
#endif
511-
512514
#include <stdarg.h> // for va_list
513515
514516
//================================== GLOBALS =================================*/

0 commit comments

Comments
 (0)