gpt4 book ai didi

haskell - 使用 hs_init 分析共享 cabal 库时出现 GHC RTS 运行时错误

转载 作者:行者123 更新时间:2023-12-02 17:53:48 26 4
gpt4 key购买 nike

我有一个大型 C 项目,必须使用 gcc 编译。因此,我将主可执行文件链接到如下文件:

#include <HsFFI.h>

static void my_enter(void) __attribute__((constructor));
static void my_enter(void) {
static char *argv[] = { "Pointer.exe", 0 };
//static char *argv[] = { "Pointer.exe", "+RTS", "-N", "-p", "-s", "-h", "-i0.1", "-RTS", 0 };
static char **argv_ = argv;
static int argc = 1; // 8 for profiling
hs_init(&argc, &argv_);
//hs_init_with_rtsopts(&argc, &argv_);
}

static void my_exit(void) __attribute__((destructor));
static void my_exit(void) { hs_exit(); }

按预期工作 - GHC 运行时系统已初始化,我可以使用 FFI 从 C 调用 Haskell 代码。

然后,我尝试使用 "+RTS"、"-N"、"-p 启用分析(主要用于使用 Debug.Trace 进行堆栈跟踪)和代码覆盖率 (HPC)上面注释掉的行上的 ", "-s", "-h", "-i0.1", "-RTS" 标志。但是,我在初始化期间收到有关线程和分析的错误消息:

Pointer.exe: the flag -N requires the program to be built with -threaded
Pointer.exe: the flag -p requires the program to be built with -prof
Pointer.exe: Most RTS options are disabled. Use hs_init_with_rtsopts() to enable them.
Pointer.exe: newBoundTask: RTS is not initialised; call hs_init() first

我配置了 cabal 包:

"--enable-library-profiling"
"--enable-executable-profiling"
"--enable-shared"
"--enable-tests"
"--enable-coverage"

在运行作为 cabal 项目一部分编译的可执行文件时,它正确地为我提供了堆栈跟踪和代码覆盖率。

如果我尝试按照错误消息的建议使用 hs_init_with_rtsopts,我会在 GHC rts 初始化期间收到 SIGSEGV:

Using host libthread_db library "/usr/lib/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6a2d0ca in strlen () from /usr/lib/libc.so.6
(gdb) bt
#0 0x00007ffff6a2d0ca in strlen () from /usr/lib/libc.so.6
#1 0x00007ffff798c5f6 in copyArg (
arg=0x657372615062696c <error: Cannot access memory at address 0x657372615062696c>) at rts/RtsFlags.c:1684
#2 0x00007ffff798c679 in copyArgv (argc=8, argv=0x555555554cee) at rts/RtsFlags.c:1696
#3 0x00007ffff798dbe2 in setFullProgArgv (argc=<optimized out>, argv=<optimized out>) at rts/RtsFlags.c:1780
#4 0x00007ffff798e773 in hs_init_ghc (argc=0x555555756090 <argc>, argv=0x5555557560a0 <argv>, rts_config=...)
at rts/RtsStartup.c:162
#5 0x00007ffff798e7cc in hs_init_with_rtsopts (argc=<optimized out>, argv=<optimized out>)
at rts/RtsStartup.c:121
#6 0x0000555555554c7d in __libc_csu_init ()
#7 0x00007ffff69cc59f in __libc_start_main () from /usr/lib/libc.so.6
#8 0x0000555555554b29 in _start ()

那么如何从使用 gcc 编译的程序中启用运行时分析呢?

最佳答案

因此,段错误是由于问题中未包含的拼写错误造成的。鬼鬼祟祟的!

要启用线程和分析等功能,您必须将最终程序链接到适当的 RTS 风格。这是 GHC 的 -prof 标志的效果之一,也是其 -threaded 标志以及 -debug 等各种其他标志的唯一效果。

RTS 风格位于不同的库中,其名称格式为

libHSrts.a           libHSrts-ghc7.8.4.so        (vanilla)
libHSrts_debug.a libHSrts_debug-ghc7.8.4.so (debug)
libHSrts_thr.a libHSrts_thr-ghc7.8.4.so (threaded)
libHSrts_p.a - (profiling)
libHSrts_thr_p.a - (threaded+profiling)
libHSrts_l.a libHSrts_l-ghc7.8.4.so (eventlog)
...

左边是静态库;右侧是动态库,其库名称包含 GHC 版本,以便运行时动态加载器在安装了多个版本的 GHC 时更容易找到正确的版本。您可以在 ghc --info 中的“RTS 方式”下查看 GHC 安装的完整列表。

默认情况下没有安装动态分析库,但我认为拥有它们没有根本问题,您可以配置 GHC 构建系统来构建它们。 (它们现在不是特别有用,因为 ghci 不支持分析,但希望很快就会改变。)

关于haskell - 使用 hs_init 分析共享 cabal 库时出现 GHC RTS 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33416633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com