获得启动和程序段的C / C ++结束 [英] Get start and end of process segments C/C++

查看:111
本文介绍了获得启动和程序段的C / C ++结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到启动和下面的过程段的结束地址:code,数据,堆栈环境。我理解它是如何位于内存中,但不知道如何使用API​​调用或别的东西来得到它。我已经找到了如何让使用此code一些细分的启动

 的#include<&stdio.h中GT;INT temp_data = 100;
静态INT temp_bss;无效print_addr(无效)
{
        INT local_var = 100;
        为int * code_segment_address =(INT *)及print_addr;
        为int * data_segment_address =安培; temp_data;
        为int * bss_address =安培; temp_bss;
        为int * stack_segment_address =安培; local_var;        的printf(各个环节的\\ n地址:);
        的printf(\\ n \\ t code段:%P,code_segment_address);
        的printf(\\ n \\ TDATA段:%P,data_segment_address);
        的printf(\\ n \\ TBSS:%P,bss_address);
        的printf(\\ n \\ tStack段:%P \\ N,stack_segment_address);}诠释的main()
{
        print_addr();
        返回0;
}

但我不知道如何找到每个段结束。我有唯一的想法是,一​​个段的端部是另一个段的开始。
请解释我怎么可以这样使用C和Linux API。


解决方案

我不知道该数据或堆段是明确界定和独特的(使用动态库特别是在多线程应用程序,或者干脆在应用程序中包括 libc.so )。换句话说,没有更多的任何明确的开始和文本,数据或堆段的结束,因为今天的方法有许多这样的段。所以你的问题甚至没有在一般情况下的感觉。

大多数的malloc 实现使用的的mmap(2)则munmap SBRK

您应该阅读有关 PROC(5)。特别是,您的应用程序可以读取的/ proc /自/图(或的/ proc / 1234 /图的过程PID 1234)或的/ proc /自/ smaps ;尝试执行cat / proc /自/图,并考虑使用的上的fopen(3) 的/ proc /自/地图(然后在一个循环与fgets 的readline ,最后赶紧 FCLOSE )。也许提供dladdr(3)可能是相关的。

您也可以阅读你的程序,例如的 ELF 头的的/ proc /自/ EXE 。另请参见 readelf(1)和的 objdump的(1)&安培; 的execve(2)&安培; 精灵(5)&安培; ld.so(8)&安培; libelf的。阅读也莱文的链接器和放大器;装载机书和 Drepper的文章:如何写共享库

另请参阅这个答案以一个相关的问题(也的这个问题)。注意,最近的Linux系统具有 ASLR 的,所以两个类似进程的地址布局运行在同一个节目在相同的环境是不同的。

也尝试 strace的(1)一些简单的命令或者你的程序。你会明白更多的相关系统调用(2) 。另请参阅高级Linux编程

I need to get start and end address of following process segments : code, data,stack, environment. I understand how is it located in memory, but don't know how to get it using api calls or something else. I have found how to get start of some segments using this code

#include <stdio.h>

int temp_data = 100;
static int temp_bss;

void print_addr ( void )
{
        int local_var = 100;
        int *code_segment_address = ( int* ) &print_addr;
        int *data_segment_address = &temp_data;
        int *bss_address = &temp_bss;
        int *stack_segment_address = &local_var;

        printf ( "\nAddress of various segments:" );
        printf ( "\n\tCode Segment : %p" , code_segment_address );
        printf ( "\n\tData Segment : %p" , data_segment_address );
        printf ( "\n\tBSS : %p" , bss_address );
        printf ( "\n\tStack Segment : %p\n" , stack_segment_address );

}

int main ( )
{
        print_addr ();
        return 0;
}

But I don't know how to find end of each segment. I have only idea is that the end of one segment is the start of another segment. Please explain how can I do this using C and linux API.

解决方案

I'm not sure that the data or the heap segment is well defined and unique (in particular in multi-threaded applications, or simply in applications using dynamic libraries, including libc.so). In other words, there is no more any well defined start and end of text, data, or heap segment, because today a process has many such segments. So your question don't even make sense in the general case.

Most malloc implementations use mmap(2) and munmap much more than sbrk

You should read more about proc(5). In particular, your application could read /proc/self/maps (or /proc/1234/maps for process of pid 1234) or /proc/self/smaps; try cat /proc/self/maps and consider using fopen(3) on "/proc/self/maps" (then a loop on fgets or readline, and finally and quickly fclose). Perhaps dladdr(3) might be relevant.

You could also read the ELF headers of your program, e.g. of /proc/self/exe. See also readelf(1) and objdump(1) & execve(2) & elf(5) & ld.so(8) & libelf. Read also Levine's Linkers & Loaders book and Drepper's paper: How To Write Shared Libraries.

See also this answer to a related question (and also that question). Notice that recent Linux systems have ASLR, so the address layout of two similar processes running the same program in the same environment would be different.

Try also to strace(1) some simple command or your program. You'll understand a bit more the relevant syscalls(2). Read also Advanced Linux Programming

这篇关于获得启动和程序段的C / C ++结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆