c - "Segmentation fault (core dumped)"代表 : "No such file or directory" for libioP. h、printf-parse.h、vfprintf-internal.c 等

标签 c segmentation-fault gdb coredump core-file

核心转储文件中的示例错误:

1289    vfprintf-internal.c: No such file or directory.
111 printf-parse.h: No such file or directory.
948 libioP.h: No such file or directory.
948 libioP.h: No such file or directory.

我正在开发 fast_malloc() 实现,但是当我用自己的实现重写 malloc()free() 时,会因未知原因出现段错误,但不是在此之前(意思是,如果我调用 fast_malloc() 就可以了,但如果我希望能够调用 malloc() 来获取我的实现,似乎被打破)。

为什么会出现段错误?

在打印任何内容之前的示例输出,包括 main() 开头的打印语句,以及我的 fast_malloc() 内的一些调试打印:

Segmentation fault (core dumped)

我已打开核心转储 as I explain here

因此,gdb path/to/my/executable core 显示了以下一些core 文件信息。请注意,每次运行可能会导致“没有这样的文件或目录”中缺少哪个文件的不同语句。

  1. 一次运行:
Reading symbols from build/fast_malloc_unit_tests...

warning: core file may not match specified executable file.
[New LWP 1257155]
Core was generated by `build/fast_malloc_unit_tests'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007fd50fc7ba01 in __vfprintf_internal (s=0x7fd50fdee6a0 <_IO_2_1_stdout_>, 
    format=0x5622fd1b8010 "DEBUG: %s():\n", ap=ap@entry=0x7ffec28300a0, 
    mode_flags=mode_flags@entry=0) at vfprintf-internal.c:1289
1289    vfprintf-internal.c: No such file or directory.
(gdb) bt
#0  0x00007fd50fc7ba01 in __vfprintf_internal (s=0x7fd50fdee6a0 <_IO_2_1_stdout_>, 
    format=0x5622fd1b8010 "DEBUG: %s():\n", ap=ap@entry=0x7ffec28300a0, 
    mode_flags=mode_flags@entry=0) at vfprintf-internal.c:1289
#1  0x00007fd50fc66ebf in __printf (format=<optimized out>) at printf.c:33
#2  0x00005622fd1b53eb in fast_malloc (num_bytes=1024) at src/fast_malloc.c:225
#3  0x00005622fd1b5b66 in malloc (num_bytes=1024) at src/fast_malloc.c:496
#4  0x00007fd50fc86e84 in __GI__IO_file_doallocate (fp=0x7fd50fdee6a0 <_IO_2_1_stdout_>)
    at filedoalloc.c:101
#5  0x00007fd50fc97050 in __GI__IO_doallocbuf (fp=fp@entry=0x7fd50fdee6a0 <_IO_2_1_stdout_>)
    at libioP.h:948
#6  0x00007fd50fc960b0 in _IO_new_file_overflow (f=0x7fd50fdee6a0 <_IO_2_1_stdout_>, ch=-1)
    at fileops.c:745
#7  0x00007fd50fc94835 in _IO_new_file_xsputn (n=7, data=<optimized out>, f=<optimized out>)
    at libioP.h:948
#8  _IO_new_file_xsputn (f=0x7fd50fdee6a0 <_IO_2_1_stdout_>, data=<optimized out>, n=7)
    at fileops.c:1197
#9  0x00007fd50fc7baf2 in __vfprintf_internal (s=0x7fd50fdee6a0 <_IO_2_1_stdout_>, 
    format=0x5622fd1b8010 "DEBUG: %s():\n", ap=ap@entry=0x7ffec28308e0, 
    mode_flags=mode_flags@entry=0) at ../libio/libioP.h:948
#10 0x00007fd50fc66ebf in __printf (format=<optimized out>) at printf.c:33
#11 0x00005622fd1b53eb in fast_malloc (num_bytes=1024) at src/fast_malloc.c:225
#12 0x00005622fd1b5b66 in malloc (num_bytes=1024) at src/fast_malloc.c:496
--Type <RET> for more, q to quit, c to continue without paging--q
Quit
(gdb) q

  • 另一个:
  • Reading symbols from build/fast_malloc_unit_tests...
    
    warning: core file may not match specified executable file.
    [New LWP 1257787]
    Core was generated by `build/fast_malloc_unit_tests'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  0x00007f20b0bbba80 in __find_specmb (
        format=0x5644c516d108 "DEBUG:   block_map_i = %zu (num_bytes requested to allocate = %zu; smallest user block size large enough = %zu)\n") at printf-parse.h:111
    111 printf-parse.h: No such file or directory.
    (gdb) bt
    #0  0x00007f20b0bbba80 in __find_specmb (
        format=0x5644c516d108 "DEBUG:   block_map_i = %zu (num_bytes requested to allocate = %zu; smallest user block size large enough = %zu)\n") at printf-parse.h:111
    #1  __vfprintf_internal (s=0x7f20b0d2e6a0 <_IO_2_1_stdout_>, 
        format=0x5644c516d108 "DEBUG:   block_map_i = %zu (num_bytes requested to allocate = %zu; smallest user block size large enough = %zu)\n", ap=ap@entry=0x7ffe7f6ea580, mode_flags=mode_flags@entry=0)
        at vfprintf-internal.c:1365
    #2  0x00007f20b0ba6ebf in __printf (format=<optimized out>) at printf.c:33
    #3  0x00005644c516a47d in fast_malloc (num_bytes=1024) at src/fast_malloc.c:244
    #4  0x00005644c516ab4e in malloc (num_bytes=1024) at src/fast_malloc.c:496
    #5  0x00007f20b0bc6e84 in __GI__IO_file_doallocate (fp=0x7f20b0d2e6a0 <_IO_2_1_stdout_>)
        at filedoalloc.c:101
    #6  0x00007f20b0bd7050 in __GI__IO_doallocbuf (fp=fp@entry=0x7f20b0d2e6a0 <_IO_2_1_stdout_>)
        at libioP.h:948
    #7  0x00007f20b0bd60b0 in _IO_new_file_overflow (f=0x7f20b0d2e6a0 <_IO_2_1_stdout_>, ch=-1)
        at fileops.c:745
    #8  0x00007f20b0bd4835 in _IO_new_file_xsputn (n=23, data=<optimized out>, f=<optimized out>)
        at libioP.h:948
    #9  _IO_new_file_xsputn (f=0x7f20b0d2e6a0 <_IO_2_1_stdout_>, data=<optimized out>, n=23)
        at fileops.c:1197
    #10 0x00007f20b0bbbaf2 in __vfprintf_internal (s=0x7f20b0d2e6a0 <_IO_2_1_stdout_>, 
        format=0x5644c516d108 "DEBUG:   block_map_i = %zu (num_bytes requested to allocate = %zu; smallest--Type <RET> for more, q to quit, c to continue without paging--q
    Quit
    (gdb) q
    
    
  • 另一个:
  • Reading symbols from build/fast_malloc_unit_tests...
    
    warning: core file may not match specified executable file.
    [New LWP 1258037]
    Core was generated by `build/fast_malloc_unit_tests'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  0x00007f901ef65e4d in __GI__IO_file_doallocate (fp=0x7f901f0cd6a0 <_IO_2_1_stdout_>)
        at libioP.h:948
    948 libioP.h: No such file or directory.
    (gdb) q
    
  • 另一个
  • Reading symbols from build/fast_malloc_unit_tests...
    
    warning: core file may not match specified executable file.
    [New LWP 1258336]
    Core was generated by `build/fast_malloc_unit_tests'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  0x00007f5e4b551a80 in __find_specmb (
        format=0x562fac6d7108 "DEBUG:   block_map_i = %zu (num_bytes requested to allocate = %zu; smallest user block size large enough = %zu)\n") at printf-parse.h:111
    111 printf-parse.h: No such file or directory.
    (gdb) q
    

    我目前的 gcc 构建选项:

    -Wall -Wextra -Werror -O0 -ggdb -std=c11 -save-temps=obj -DDEBUG
    

    可能与我拥有的这个 DEBUG_PRINTF() 宏有关,我在 fast_malloc() 中调用它。

    #ifdef DEBUG
        /// Debug printf function.
        /// See: https://stackoverflow.com/a/1941336/4561887
        #define DEBUG_PRINTF(...) printf("DEBUG: "__VA_ARGS__)
    #else
        #define DEBUG_PRINTF(...) \
            do                    \
            {                     \
            } while (0)
    #endif
    

    为什么malloc()会在程序启动之前被调用?我在任何地方都不会调用它。但是,请注意,您可以看到 malloc() 被调用,调用了 1024 字节,如运行 1 和 2 中的堆栈跟踪所示(尽管每次运行都会发生这种情况,这些是我已经粘贴得足够多的内容,您可以看到它在)。

    我的 malloc()free() 覆盖如下所示:

    inline void* malloc(size_t num_bytes)
    {
        return fast_malloc(num_bytes);
    }
    
    inline void free(void* ptr)
    {
        fast_free(ptr);
    }
    

    我的单线程程序是否在启动时神秘地调用了 malloc() ,而没有以某种方式调用它?是否发生了一些奇怪的程序初始化事情?我的 fast_malloc() 实现目前不是线程安全的,因此如果 Linux 在某种程序初始化或其他操作期间执行一些奇怪的多线程 malloc() 调用,这可能是损坏的原因,同样,覆盖 malloc()fast_malloc() 还不是线程安全的。

    它似乎与 malloc() 内的打印有关。 malloc() 内的打印是否被禁止?

    这是来自核心转储的最近堆栈跟踪的底部(第一次调用位于最底部):

    #127471 0x00005626d43dca28 in malloc (num_bytes=1024) at src/fast_malloc.c:494
    #127472 0x00007faa222a7e84 in __GI__IO_file_doallocate (fp=0x7faa2240f6a0 <_IO_2_1_stdout_>) at filedoalloc.c:101
    #127473 0x00007faa222b8050 in __GI__IO_doallocbuf (fp=fp@entry=0x7faa2240f6a0 <_IO_2_1_stdout_>) at libioP.h:948
    #127474 0x00007faa222b70b0 in _IO_new_file_overflow (f=0x7faa2240f6a0 <_IO_2_1_stdout_>, ch=-1) at fileops.c:745
    #127475 0x00007faa222b5835 in _IO_new_file_xsputn (n=13, data=<optimized out>, f=<optimized out>) at libioP.h:948
    #127476 _IO_new_file_xsputn (f=0x7faa2240f6a0 <_IO_2_1_stdout_>, data=<optimized out>, n=13) at fileops.c:1197
    #127477 0x00007faa222aa678 in __GI__IO_puts (str=0x5626d43df227 '=' <repeats 13 times>) at libioP.h:948
    #127478 0x00005626d43dca28 in malloc (num_bytes=1024) at src/fast_malloc.c:494
    #127479 0x00007faa222a7e84 in __GI__IO_file_doallocate (fp=0x7faa2240f6a0 <_IO_2_1_stdout_>) at filedoalloc.c:101
    #127480 0x00007faa222b8050 in __GI__IO_doallocbuf (fp=fp@entry=0x7faa2240f6a0 <_IO_2_1_stdout_>) at libioP.h:948
    #127481 0x00007faa222b70b0 in _IO_new_file_overflow (f=0x7faa2240f6a0 <_IO_2_1_stdout_>, ch=-1) at fileops.c:745
    #127482 0x00007faa222b5835 in _IO_new_file_xsputn (n=13, data=<optimized out>, f=<optimized out>) at libioP.h:948
    #127483 _IO_new_file_xsputn (f=0x7faa2240f6a0 <_IO_2_1_stdout_>, data=<optimized out>, n=13) at fileops.c:1197
    #127484 0x00007faa222aa678 in __GI__IO_puts (str=0x5626d43df227 '=' <repeats 13 times>) at libioP.h:948
    #127485 0x00005626d43dca28 in malloc (num_bytes=1024) at src/fast_malloc.c:494
    #127486 0x00007faa222a7e84 in __GI__IO_file_doallocate (fp=0x7faa2240f6a0 <_IO_2_1_stdout_>) at filedoalloc.c:101
    #127487 0x00007faa222b8050 in __GI__IO_doallocbuf (fp=fp@entry=0x7faa2240f6a0 <_IO_2_1_stdout_>) at libioP.h:948
    #127488 0x00007faa222b70b0 in _IO_new_file_overflow (f=0x7faa2240f6a0 <_IO_2_1_stdout_>, ch=-1) at fileops.c:745
    #127489 0x00007faa222b5835 in _IO_new_file_xsputn (n=49, data=<optimized out>, f=<optimized out>) at libioP.h:948
    #127490 _IO_new_file_xsputn (f=0x7faa2240f6a0 <_IO_2_1_stdout_>, data=<optimized out>, n=49) at fileops.c:1197
    #127491 0x00007faa222aa678 in __GI__IO_puts (str=0x5626d43df238 "Running UNIT tests for the \"fast_malloc\" module.\n") at libioP.h:948
    #127492 0x00005626d43dca98 in main () at src/fast_malloc_unit_tests.c:35
    (gdb) 
    

    什么是 __GI__IO_puts_IO_new_file_xsputn 以及您升级时的其他函数调用?他们在其他线程中调用吗?他们是否在幕后调用 malloc() ?看来 __GI__IO_file_doallocate 是...

    最佳答案

    您正在 malloc 实现中调用 printf。这不会有好结果。

    在堆栈跟踪中,您可以清楚地看到 printf 本身调用 malloc

    如果您的 malloc 在操作其数据结构期间未准备好被调用,它将崩溃(可能就是这里发生的情况)。

    或者,当malloc调用printf时,您也可能以无限递归结束,printf又调用malloc,后者又调用printf

    TL;DR:当实现像 malloc 这样低级的东西时,您必须坚持使用本身不分配任何东西的低级函数,或者直接使用系统调用。

    Why is malloc() getting called before the program starts anyway?

    因为例如低级函数动态加载器在自身初始化时需要分配内存。

    您的 malloc必须在进程生命周期的早期就起作用;早于main

    Is printing inside malloc() forbidden?

    一切可能分配内存的东西都是被禁止的。

    实际上,您只需要调用异步信号安全例程,因为非异步信号安全例程可能分配,如果现在不分配,将来也会分配。

    关于c - "Segmentation fault (core dumped)"代表 : "No such file or directory" for libioP. h、printf-parse.h、vfprintf-internal.c 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68188903/

    相关文章:

    c++ - 没有指针时如何修复 'Segmentation Error'?

    c++ - 处理大型 C 数组时的内存问题

    将十六进制char数组转换为C中的u8数组

    c - write系统调用直接把数据写到磁盘?

    c - slurm sbatch 标准 IO 重定向

    assembly - 我如何知道汇编指令被命中的次数

    c++ - 使用多个源文件时,GDB 不会中断某些代码行

    c - 为数组的动态数组释放内存

    c - 访问结构中的指针

    c++ - 如何在 GDB 中编写一个引用指针的表达式?