Colderleo's blog Colderleo's blog
首页
Linux
C++
Python
前端
工具软件
mysql
索引
关于
GitHub (opens new window)

Colder Leo

热爱代码的打工人
首页
Linux
C++
Python
前端
工具软件
mysql
索引
关于
GitHub (opens new window)
  • bug定位的一些情形
  • c++性能调优,可能的情况
  • total-编程知识点集锦
  • hpc_common.hpp
  • memory order 内存模型
  • 类型推导之auto-template-decltype
  • 完美转发forward源码分析
  • 左值和右值,右值引用、重载 std-move,引用折叠
  • cmake用法
  • alignas、alignof、sizeof实现内存对齐分配
  • 通过宏定义控制debug打印
  • 程序耗时性能测试
  • 线程池开源项目阅读
  • C++类中包含没有默认构造函数的成员
  • C++可变参数模板
  • C++属性继承 public protected private
  • C++智能指针
  • C++导出so的方法,以及extern C 注意事项
  • 四种spin lock
  • condition_variable和unique_lock
  • dpdk、kernel bypass
  • 智能网卡solarflare、Mellanox、X10等
  • 汇编寄存器和常见指令
  • c++ 类的静态成员变量未定义
  • C++获取类成员函数地址
  • preload示例
    • preload示例
  • C++异常安全和RAII
  • C++11单例模式
  • C++歪门邪道
  • boost-program-option用法
  • c++17通过模板获取类成员的个数
  • 通过模板实现结构体成员拷贝-按成员名匹配
  • STL学习路径
  • boost库安装使用方法
  • C++文件读写
  • linux下socket通信demo,server和client
  • makefile写法
  • RxCpp
  • C++
gaoliu
2022-03-18
目录

preload示例

# preload示例

参考csapp

preload_send.c

#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <fcntl.h>
// #include <sys/socket.h>

int send(int s, const void * msg, int len, unsigned int falgs) {
    // static int [1024];
    static int index = 0;
    // static psend = NULL;
    typedef int (*sendfunc_t)(int, const void *, int, unsigned int);
    // static int (*psend)(int, const void *, int, unsigned int) = NULL;
    static sendfunc_t psend = NULL;

    if(!psend) {
        psend = (sendfunc_t)dlsym(RTLD_NEXT, "send");
        if(!psend) {
            printf("dlsym send fail\n");
            exit(0);
        }

        int flags = fcntl(s, F_GETFL, 0);
        printf("flags&O_NONBLOCK=%x, O_NONBLOCK=%x, flags=%x, len=%d\n", flags&O_NONBLOCK, O_NONBLOCK, flags, len);

    }

    int ret = len;
    ret = psend(s, msg, len, falgs);
    printf("ret=%d, len=%d\n", ret, len);
    index++;
    return ret;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

编译 (注:用g++编译会进行name mangling,符号名不是send)

gcc -o preload_send.so --shared -fPIC   preload_send.c
1

运行

LD_PRELOAD=./preload_send.so   ./program
1
编辑 (opens new window)
上次更新: 2023/05/07, 17:27:54
C++获取类成员函数地址
C++异常安全和RAII

← C++获取类成员函数地址 C++异常安全和RAII→

最近更新
01
通过模板实现结构体成员拷贝-按成员名匹配
05-07
02
c++17通过模板获取类成员的个数
05-01
03
avx-sse切换惩罚
04-30
更多文章>
Theme by Vdoing | Copyright © 2019-2023 Colder Leo | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×