无法调试.so库使我的shell段错误[关闭]

问题描述 投票:-1回答:1

我试图在C中使用malloc,free和realloc函数(使用mmap)。

我使用以下命令行将它们包含在我的shell中(我正在使用sh):

export DYLD_LIBRARY_PATH=.
export DYLD_FORCE_FLAT_NAMESPACE=1
export DYLD_INSERT_LIBRARIES="./malloc.so:./free.so:./realloc.so"

这是我的一些malloc代码:

#include "../incs/malloc.h"

void        *malloc(size_t size)
{
    write(2, "\nMALLOC", 7);
    t_block     *res;

    write(2, "0", 1);

    res = NULL;

    if (!(glob))
    {
        write(2, "1", 1);
        // First call of malloc, need to init glob variable
        glob = init_glob();
    }
    write(2, "2", 1);
    res = get_block(size);
    write(2, "3", 1);

    if (!res)
    return (NULL);

    write(2, "4", 1);

    return (res->memory);
}

我在init_glob()函数的开头也有一个调试写入。

当我在shell中执行前面的命令行,并运行随机命令(例如ls)时,我得到的是:

MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01
MALLOC01Segmentation fault: 11

我真的不明白为什么它不起作用,如何调试这个。

它应该只写一次“MALLOC01”,十次转到我的init_glob函数。为什么这样循环?我如何在ls命令中看到它崩溃的位置?

提前致谢。

=====编辑=====

这是我的init_glob()函数:

#include "../incs/malloc.h"

/*
**  This function returns a t_glob.
**  It shall init the global variable of type t_glob, on the first time
**  malloc is called in a process.
*/
t_glob      *init_glob(void)
{
    write(2, "a", 1);
    t_glob      *res;

    res = NULL;

    write(2, "b", 1);
    res = (t_glob *)allocate_memory(sizeof(t_glob));

    write(2, "c", 1);

    res->tiny = NULL;
    res->small = NULL;
    res->large = NULL;
    write(2, "d", 1);

    return (res);
}

和我的allocate_memory()函数(但似乎该程序甚至没有去那里):

void        *allocate_memory(size_t size)
{
    void        *res;

    res = NULL;

    res = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);

    return (res);
}

我的t_glob结构是这样的原型:

typedef struct                  s_glob
{
    t_page                      *tiny;
    t_page                      *small;
    t_page                      *large;
    // size_t                   sizeof_block; // Avoid repeat of sizeof() call
    // size_t                   sizeof_page;
    // size_t                   getpagesize_result;
}                               t_glob;
c unix segmentation-fault malloc mmap
1个回答
1
投票

我真的不明白为什么它不起作用,如何调试这个。

调试这个的常用方法是让程序转储coreulimit -c unlimited),然后查看发生无限递归的调试器。

如果我猜测,我猜想当动态加载器试图解析从mallocinit_glob的调用时,这个动态符号解析本身需要动态内存并调用malloc

如果你提供MCVE,包括构建指令,你会得到一个更好的答案(更少猜测)。

© www.soinside.com 2019 - 2024. All rights reserved.