gpt4 book ai didi

c++ - 如何在运行时查看 C 程序的内存布局?

转载 作者:太空狗 更新时间:2023-10-29 12:39:15 26 4
gpt4 key购买 nike

我想在 C 中查看我的程序的内存布局,以便我可以在运行时实际了解内存的所有不同部分,例如 BSS 或堆中的变化?

最佳答案

在 Linux 中,对于进程 PID,请查看 /proc/PID/maps/proc/PID/smaps 伪文件。 (进程本身可以使用/proc/self/maps/proc/self/smaps。)

它们的内容记录在 man 5 proc 中.


这是一个示例,说明如何将内容读入地址范围结构的链表。

mem-stats.h:

#ifndef   MEM_STATS_H
#define MEM_STATS_H
#include <stdlib.h>
#include <sys/types.h>

#define PERMS_READ 1U
#define PERMS_WRITE 2U
#define PERMS_EXEC 4U
#define PERMS_SHARED 8U
#define PERMS_PRIVATE 16U

typedef struct address_range address_range;
struct address_range {
struct address_range *next;
void *start;
size_t length;
unsigned long offset;
dev_t device;
ino_t inode;
unsigned char perms;
char name[];
};

address_range *mem_stats(pid_t);
void free_mem_stats(address_range *);

#endif /* MEM_STATS_H */

mem-stats.c:

#define _POSIX_C_SOURCE 200809L
#define _BSD_SOURCE
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "mem-stats.h"

void free_mem_stats(address_range *list)
{
while (list) {
address_range *curr = list;

list = list->next;

curr->next = NULL;
curr->length = 0;
curr->perms = 0U;
curr->name[0] = '\0';

free(curr);
}
}

address_range *mem_stats(pid_t pid)
{
address_range *list = NULL;
char *line = NULL;
size_t size = 0;
FILE *maps;

if (pid > 0) {
char namebuf[128];
int namelen;

namelen = snprintf(namebuf, sizeof namebuf, "/proc/%ld/maps", (long)pid);
if (namelen < 12) {
errno = EINVAL;
return NULL;
}

maps = fopen(namebuf, "r");
} else
maps = fopen("/proc/self/maps", "r");

if (!maps)
return NULL;

while (getline(&line, &size, maps) > 0) {
address_range *curr;
char perms[8];
unsigned int devmajor, devminor;
unsigned long addr_start, addr_end, offset, inode;
int name_start = 0;
int name_end = 0;

if (sscanf(line, "%lx-%lx %7s %lx %u:%u %lu %n%*[^\n]%n",
&addr_start, &addr_end, perms, &offset,
&devmajor, &devminor, &inode,
&name_start, &name_end) < 7) {
fclose(maps);
free(line);
free_mem_stats(list);
errno = EIO;
return NULL;
}

if (name_end <= name_start)
name_start = name_end = 0;

curr = malloc(sizeof (address_range) + (size_t)(name_end - name_start) + 1);
if (!curr) {
fclose(maps);
free(line);
free_mem_stats(list);
errno = ENOMEM;
return NULL;
}

if (name_end > name_start)
memcpy(curr->name, line + name_start, name_end - name_start);
curr->name[name_end - name_start] = '\0';

curr->start = (void *)addr_start;
curr->length = addr_end - addr_start;
curr->offset = offset;
curr->device = makedev(devmajor, devminor);
curr->inode = (ino_t)inode;

curr->perms = 0U;
if (strchr(perms, 'r'))
curr->perms |= PERMS_READ;
if (strchr(perms, 'w'))
curr->perms |= PERMS_WRITE;
if (strchr(perms, 'x'))
curr->perms |= PERMS_EXEC;
if (strchr(perms, 's'))
curr->perms |= PERMS_SHARED;
if (strchr(perms, 'p'))
curr->perms |= PERMS_PRIVATE;

curr->next = list;
list = curr;
}

free(line);

if (!feof(maps) || ferror(maps)) {
fclose(maps);
free_mem_stats(list);
errno = EIO;
return NULL;
}
if (fclose(maps)) {
free_mem_stats(list);
errno = EIO;
return NULL;
}

errno = 0;
return list;
}

使用上面的示例程序,example.c:

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "mem-stats.h"

int main(int argc, char *argv[])
{
int arg, pid;
char dummy;

if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
fprintf(stderr, " %s PID\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "You can use PID 0 as an alias for the command itself.\n");
fprintf(stderr, "\n");
return EXIT_SUCCESS;
}

for (arg = 1; arg < argc; arg++)
if (sscanf(argv[arg], " %i %c", &pid, &dummy) == 1) {
address_range *list, *curr;

if (!pid)
pid = getpid();

list = mem_stats((pid_t)pid);
if (!list) {
fprintf(stderr, "Cannot obtain memory usage of process %d: %s.\n", pid, strerror(errno));
return EXIT_FAILURE;
}

printf("Process %d:\n", pid);
for (curr = list; curr != NULL; curr = curr->next)
printf("\t%p .. %p: %s\n", curr->start, (void *)((char *)curr->start + curr->length), curr->name);
printf("\n");
fflush(stdout);

free_mem_stats(list);

} else {
fprintf(stderr, "%s: Invalid PID.\n", argv[arg]);
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

和一个 Makefile 使构建变得简单:

CC      := gcc
CFLAGS := -Wall -Wextra -O2 -fomit-frame-pointer
LDFLAGS :=
PROGS := example

.PHONY: all clean

all: clean $(PROGS)

clean:
rm -f *.o $(PROGS)

%.o: %.c
$(CC) $(CFLAGS) -c $^

example: mem-stats.o example.o
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@

请注意,上面 Makefile 中的三个缩进行必须使用制表符,而不是空格。似乎这里的编辑器将制表符转换为空格,因此您需要修复它,例如使用

sed -e 's|^  *|\t|' -i Makefile

如果您不修复缩进,并在 Makefile 中使用空格,您将看到类似于 *** missing separator 的错误消息。停止

一些编辑器会自动将 tab 按键转换为多个空格,因此您可能需要深入研究您使用的任何编辑器的编辑器设置。通常,编辑器会保持粘贴的制表符完好无损,因此您始终可以尝试从其他程序粘贴制表符。

要编译运行,保存以上文件并运行:

make
./example 0

打印示例程序本身使用的内存范围。例如,如果您想查看 PulseAudio 守护程序使用的内存范围,请运行:

./example $(ps -o pid= -C pulseaudio)

请注意,标准访问限制适用。普通用户只能看到以该用户身份运行的进程的内存范围;否则你需要 super 用户权限(sudo 或类似权限)。

关于c++ - 如何在运行时查看 C 程序的内存布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com