函数堆栈,共享库,打印出被调用函数【笔记】

函数堆栈,共享库,打印出被调用函数,

此文转自Linux man手册,仅做学习笔记使用

DL_ITERATE_PHDR(3)                                       Linux Programmer's Manual                                      DL_ITERATE_PHDR(3)

NAME
       dl_iterate_phdr - walk through list of shared objects

SYNOPSIS
       #define _GNU_SOURCE         /* See feature_test_macros(7) */
       #include <link.h>

       int dl_iterate_phdr(
                 int (*callback) (struct dl_phdr_info *info,
                                  size_t size, void *data),
                 void *data);

DESCRIPTION
       The dl_iterate_phdr() function allows an application to inquire at run time to find out which shared objects it has loaded.

       The  dl_iterate_phdr()  function walks through the list of an application's shared objects and calls the function callback once for
       each object, until either all shared objects have been processed or callback returns a nonzero value.

       Each call to callback receives three arguments: info, which is a pointer to a structure containing  information  about  the  shared
       object;  size, which is the size of the structure pointed to by info; and data, which is a copy of whatever value was passed by the
       calling program as the second argument (also named data) in the call to dl_iterate_phdr().

       The info argument is a structure of the following type:

           struct dl_phdr_info {               ElfW(Addr)        dlpi_addr;  /* Base address of object */               const char       *dlpi_name;  /* (Null-terminated) name of                                                object */               const ElfW(Phdr) *dlpi_phdr;  /* Pointer to array of                                                ELF program headers                                                for this object */               ElfW(Half)        dlpi_phnum; /* # of items in dlpi_phdr */           };

       (The ElfW() macro definition turns its argument into the name of an ELF data type suitable  for  the  hardware  architecture.   For       example, on a 32-bit platform, ElfW(Addr) yields the data type name Elf32_Addr.  Further information on these types can be found in       the <elf.h> and <link.h> header files.)

       The dlpi_addr field indicates the base address of the shared object (i.e., the difference between the virtual memory address of the       shared object and the offset of that object in the file from which it was loaded).  The dlpi_name field is a null-terminated string       giving the pathname from which the shared object was loaded.

       To understand the meaning of the dlpi_phdr and dlpi_phnum fields, we need to be aware that an ELF shared object consists of a  num‐       ber  of  segments, each of which has a corresponding program header describing the segment.  The dlpi_phdr field is a pointer to an

       array of the program headers for this shared object.  The dlpi_phnum field indicates the size of this array.

       These program headers are structures of the following form:

           typedef struct {               Elf32_Word  p_type;    /* Segment type */               Elf32_Off   p_offset;  /* Segment file offset */               Elf32_Addr  p_vaddr;   /* Segment virtual address */               Elf32_Addr  p_paddr;   /* Segment physical address */               Elf32_Word  p_filesz;  /* Segment size in file */               Elf32_Word  p_memsz;   /* Segment size in memory */               Elf32_Word  p_flags;   /* Segment flags */               Elf32_Word  p_align;   /* Segment alignment */           } Elf32_Phdr;

       Note that we can calculate the location of a particular program header, x, in virtual memory using the formula:

         addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;

RETURN VALUE       The dl_iterate_phdr() function returns whatever value was returned by the last call to callback.

VERSIONS       dl_iterate_phdr() has been supported in glibc since version 2.2.4.

ATTRIBUTES       For an explanation of the terms used in this section, see attributes(7).

       ┌──────────────────┬───────────────┬─────────┐       │Interface         │ Attribute     │ Value   │       ├──────────────────┼───────────────┼─────────┤       │dl_iterate_phdr() │ Thread safety │ MT-Safe │       └──────────────────┴───────────────┴─────────┘

CONFORMING TO       The dl_iterate_phdr() function is Linux-specific and should be avoided in portable applications.

EXAMPLE       The following program displays a list of pathnames of the shared objects it has loaded.  For each shared object, the program  lists       the virtual addresses at which the object's ELF segments are loaded.

       #define _GNU_SOURCE       #include <link.h>       #include <stdlib.h>      #include <stdio.h>

       static int       callback(struct dl_phdr_info *info, size_t size, void *data)       {           int j;

           printf("name=%s (%d segments)\n", info->dlpi_name,               info->dlpi_phnum);

           for (j = 0; j < info->dlpi_phnum; j++)                printf("\t\t header %2d: address=%10p\n", j,                    (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));           return 0;       }

       int       main(int argc, char *argv[])       {           dl_iterate_phdr(callback, NULL);

           exit(EXIT_SUCCESS);       }

SEE ALSO       ldd(1), objdump(1), readelf(1), dlopen(3), elf(5), ld.so(8)

       Executable and Linking Format Specification, available at various locations online.

GNU                                                             2007-05-18                                              DL_ITERATE_PHDR(3)

 

时间: 2024-10-27 17:49:14

函数堆栈,共享库,打印出被调用函数【笔记】的相关文章

2012-08-02 15:07 VC++ 往输出窗口打印调试信息调用函数

VC++提供了一个叫输出窗口的窗口,在调试程序和生成是可以看到输出信息,这个信息如果是MFC程序可以用TRACE宏来打印,在控制台程序里就没有了.所以我们直接调用API来实现上面的功能. 首先在程序中引入头文件windows.h或winbase.h 调用函数有两种版本 ANSI和UNICODE OutputDebugStringA OutputDebugStringW 自动版本 OutputDebugString 输出方法 OutputDebugString(_T("字符串")); O

linux 静态库、共享库

一.什么是库   本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行.由于windows和linux的本质不同,因此二者库的二进制是不兼容的.   Linux操作系统支持的函数库分为静态库和动态库,动态库又称共享库.Linux系统有几个重要的目录存放相应的函数库,如/lib    /usr/lib.   二.静态函数库.动态函数库   A.  这类库的名字一般是libxxx.a:利用静态函数库编译成的文件比较大,因为整个函数库的所有数据都被整合进目标代码中,他的优点就显而易见了

c-小白关于调用函数的问题

问题描述 小白关于调用函数的问题 请问下例如要测试多组数据的大小排列可以在int main 一点一点比较 也可以在外面创建个函数比较后调用 请问这两种方法哪个方法在运行的时候更加省时间呢? 求指教谢谢了 解决方案 时间基本上没啥明显区别 正常的带参数的函数,一般有以下几步: 1. 用push指令将参数入栈,如果是基本类型,有几个参数就需要几条push指令 2. call指令进入被调用函数,先保存IP寄存器的值,再将函数入口地址存入IP 3. 被调用函数将返回地址和基址EBP压入栈,并分配栈空间需

调用函数-C++/C#的函数内嵌问题

问题描述 C++/C#的函数内嵌问题 int a(){ int c=1; int d=2; int e=3; return c+d+e;}int b(){ int c=a(); int d=a(); int e=a();}上面是简单例子 函数a里有很多对象,函数b里也有很多对象与a重复,假设这种情况不可避免,对象有很多修改很不方便.现在我想调用函数b,却发现调用函数b必须调用函数a,于是我想把函数a整合到函数b里面去,这样我以后只需要调用一个函数就行了因为如果我调用函数b时的函数b需要调用很多个

如何通过函数指针调用函数(实现代码)_C 语言

说明:指针可以不但可以指向一个整形,浮点型,字符型,字符串型的变量,也可以指向相应的数组,而且还可以指向一个函数. 一个函数在编译的时候会被分配给一个入口地址.这个函数入口地址称为函数的指针.可以用一个指针变量指向函数,然后通过该指针变量调用此函数. 定义指向函数的指针变量的方法是: 复制代码 代码如下: int (*p) (int ,int ); int[指针变量p指向的函数的类型] (*p)[p是指向函数的指针变量] ( int,int )[p所指向的形参类型]; 与函数的原型进行比较 复制

PHP 打印调用函数入口地址(堆栈),方便调式

今天网站出现一个BUG,然后直接在数据库类里面写日志,看是哪条SQL出了问题,SQL语句到是找到了,但是不知道这条SQL语句来自何处,于是就想啊,如果能有一个办法,查看当前正在运行的这个方法是被哪个方法调用的,以及上一个方法又是哪个方法调用的,以此类推,找到入口地址多好啊.不过以前自己也想过,也在网上去搜过,就是没有找到相关的办法,今天一同事(前辈)说JAVA里面这种调试用得很普遍,叫这种堆栈调试,我是就在网上搜PHP堆栈.居然被我找到了,然后把自己的办法写出来. //调用堆栈函数,查找调用函数

函数调用-如何调用函数打印出数组

问题描述 如何调用函数打印出数组 我已经编写了函数itob(n,s,d),将整数n转换为以b为底的数,并将转换结果以字符形式保存到字符串s中.请问既然函数的返回值不能是数组,那么如何在main 函数中调用这个itob函数打印出字符串s?(刚入门的新手,很多函数没有学) 解决方案 void main() { char buf[50]; int num=100; itob(num,buf,8);//你设计的函数形参s应该是个指针,这样在函数内部转换完,在main函数就可以打印. printf("%s

深析静态链接库和动态链接库相同函数覆盖及库调用顺序问题

注意:编译器为gcc,若使用g++,请在库里面加上extern "C"     两个静态库    首先测试静态链接库,大概的代码如下:    liba.c #include <stdio.h>  #include <stdlib.h>  #include "libA.h"  void libA() {          common();  }     void common()  {          printf("libA c

02_JNI中Java代码调用C代码,Android中使用log库打印日志,javah命令的使用,Android.mk文件的编写,交叉编译

 1  编写以下案例(下面的三个按钮都调用了底层的C语言): 项目案例的代码结构如下: 2 编写DataProvider的代码: package com.example.ndkpassdata;   public class DataProvider {         /**      * 计算x和y的加法  apktools      *      * @param x      * @param y      * @return      */     public native in