mini-prog-mobile/(TZ,TPM,SmartPhone)
(Android) Process Memory Dump
파이s
2011. 11. 17. 15:53
울 회사에서 smart phone 점검 때 사용하는 프로세스 메모리 덤프 프로그램을 돌려보면, 3~4MB 정도의 로그 파일이 쌓입니다. 조금 의아해서 아주 간단한 테스트를 해보니.....어허....혹시 몰라 소스를 뒤져봤더니, /proc/maps 만을 쳐다보고 있더군요.
짤까 하다가...우연찮게 발견했습니다.
(출처! : http://www.rohitab.com/discuss/topic/37806-process-%20memory-dump-utility/)
짤까 하다가...우연찮게 발견했습니다.
/* procmem.c * dump the memory of a process to stdout * 2011-09-07 * written by X-N2O */ #define _LARGEFILE64_SOURCE #include <stdlib.h> #include <stdio.h> #include <sys/ptrace.h> #include <unistd.h> #include <fcntl.h> void dump_region(int fd, off64_t start, off64_t end) { char buf[4096]; lseek64(fd, start, SEEK_SET); while(start < end) { int rd; rd = read(fd, buf, 4096); write(STDOUT_FILENO, buf, rd); start += 4096; } } int main(int argc, char *argv[]) { FILE *maps; int mem; pid_t pid; char path[BUFSIZ]; if(argc < 2) { fprintf(stderr, "usage: %s pid\n", argv[0]); return EXIT_FAILURE; } pid = strtol(argv[1], NULL, 10); if(ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) { perror("ptrace"); return EXIT_FAILURE; } snprintf(path, sizeof(path), "/proc/%d/maps", pid); maps = fopen(path, "r"); snprintf(path, sizeof(path), "/proc/%d/mem", pid); mem = open(path, O_RDONLY); if(maps && mem != -1) { char buf[BUFSIZ + 1]; while(fgets(buf, BUFSIZ, maps)) { off64_t start, end; sscanf(buf, "%llx-%llx", &start, &end); dump_region(mem, start, end); } } ptrace(PTRACE_DETACH, pid, NULL, NULL); if(mem != -1) close(mem); if(maps) fclose(maps); return EXIT_SUCCESS; } |
(출처! : http://www.rohitab.com/discuss/topic/37806-process-%20memory-dump-utility/)