LD_PRELOAD的用法

10 篇文章 0 订阅

LD_PRELOAD顾名思义,就是重新加载链接库。

一般用于链接库的重定向,例如linux测试lockdep的测试代码,就使用了此功能。

进入源码中的

cd linux-stable-rt-4.19/tools/lib/lockdep 
cat lockdep

#!/bin/bash

LD_PRELOAD="./liblockdep.so $LD_PRELOAD" "$@"

其实大概意思就是

LD_PRELOAD=./liblockdep.so.4.19.148 ./tests/AA

赋值给LD_PRELOAD指定重新加载的动态库,然后运行测试用例AA。
liblockdep.so.4.19.148是我们独立编译出来,用于测试的接口库。

另外,测试程序还使用了#include "xxx.c"的用法,巧妙的将需要的c源文件引用了进来,使编译脚本变得简单,当然,这需要控制源文件的目录不变的前提下。

// SPDX-License-Identifier: GPL-2.0
#include <linux/lockdep.h>
#include <stdlib.h>

/* Trivial API wrappers, we don't (yet) have RCU in user-space: */
#define hlist_for_each_entry_rcu        hlist_for_each_entry
#define hlist_add_head_rcu              hlist_add_head
#define hlist_del_rcu                   hlist_del
#define list_for_each_entry_rcu         list_for_each_entry
#define list_add_tail_rcu               list_add_tail

u32 prandom_u32(void)
{
        /* Used only by lock_pin_lock() which is dead code */
        abort();
}

static struct new_utsname *init_utsname(void)
{
        static struct new_utsname n = (struct new_utsname) {
                .release = "liblockdep",
                .version = LIBLOCKDEP_VERSION,
        };

        return &n;
}

#include "../../../kernel/locking/lockdep.c"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值