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

简介:

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

此文转自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)





复制代码
相关文章
|
5月前
|
存储
函数调用与返回的相关指令
理解函数调用与返回的相关汇编指令及递归函数
|
6月前
|
Linux C++
【代码片段】Linux C++打印当前函数调用堆栈
【代码片段】Linux C++打印当前函数调用堆栈
172 0
|
6月前
|
存储 编译器 C语言
|
C++ API 数据建模
Windbg查看调用堆栈(k*)
https://www.52pojie.cn/thread-664189-1-1.html       无论是分析程序崩溃原因,还是解决程序hang问题,我们最常查看的就是程序调用堆栈。
1876 0
|
Linux C语言
函数堆栈,共享库,打印出被调用函数【笔记】
函数堆栈,共享库,打印出被调用函数, 此文转自Linux man手册,仅做学习笔记使用 DL_ITERATE_PHDR(3) Linux Programmer's Manual ...
871 0
|
存储 Linux 程序员
函数调用中堆栈的个人理解【转】
转自:http://www.cnblogs.com/MyLove-Summer/p/5034209.html 这是我的第一篇博客,由于公司项目需要,将暂时告别C语言一段时间。所以在此记录一下自己之前学习C语言的一些心得体会,希望可以分享给大家,也可以记录下自己学习过程中遇到的问题以及存在的疑惑(其实就是自己学习过程中不解的地方)。
1200 0