Changeset 0a0ef57
- Timestamp:
- 05/28/15 08:00:26 (9 years ago)
- Branches:
- master
- Children:
- 134408c
- Parents:
- 0ec59c5
- git-author:
- Hal Finkel <hfinkel@…> (05/28/15 08:00:26)
- git-committer:
- Hal Finkel <hfinkel@…> (05/28/15 08:00:26)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Makefile
r0ec59c5 r0a0ef57 4 4 UNW_HOME = ${HOME}/install/libunwind 5 5 CPPFLAGS = -I$(UNW_HOME)/include 6 LDFLAGS = -L$(UNW_HOME)/lib64 -Wl,-rpath,$(UNW_HOME)/lib64 -lunwind -lpthread 6 LDFLAGS = -L$(UNW_HOME)/lib64 -Wl,-rpath,$(UNW_HOME)/lib64 -lunwind -lpthread -ldl 7 7 8 8 all: memlog.so memlog.a -
memlog.c
r0ec59c5 r0a0ef57 8 8 #include <string.h> 9 9 10 #include <sys/time.h> 11 #include <sys/resource.h> 10 12 #include <sys/types.h> 11 13 #include <sys/stat.h> … … 32 34 snprintf(log_name, PATH_MAX, "%s.%d.memory.log_file", u.nodename, getpid()); 33 35 log_file = fopen(log_name, "w"); 36 if (!log_file) 37 fprintf(stderr, "fopen failed for '%s': %m\n", log_name); 34 38 } 35 39 … … 44 48 45 49 static void print_context(unw_context_t *context) { 50 struct rusage usage; 51 if (getrusage(RUSAGE_SELF, &usage)) { 52 fprintf(stderr, "getrusage failed: %m\n"); 53 return; 54 } 55 56 fprintf(log_file, "\t%ld.%06ld %ld", usage.ru_utime.tv_sec, 57 usage.ru_utime.tv_usec, usage.ru_maxrss); 58 59 int r; 46 60 unw_cursor_t cursor; 47 if (unw_init_local(&cursor, context)) 48 return; 49 50 while (unw_step(&cursor) > 0) { 51 unw_proc_info_t pip; 52 if (unw_get_proc_info(&cursor, &pip)) 53 return; 54 61 if ((r = unw_init_local(&cursor, context))) { 62 fprintf(stderr, "unw_init_local failed: %s [%d]\n", 63 unw_strerror(r), r); 64 return; 65 } 66 67 while ((r = unw_step(&cursor)) > 0) { 55 68 unw_word_t off; 56 69 char proc_name[PATH_MAX]; … … 58 71 off = 0; 59 72 strcpy(proc_name, "?"); 73 } 74 75 unw_proc_info_t pip; 76 if ((r = unw_get_proc_info(&cursor, &pip))) { 77 // unw_get_proc_info is not supported on some platforms (ppc and ppc64, 78 // for example), so we need to try harder... 79 if (r == -UNW_EINVAL) { 80 unw_word_t pc; 81 if ((r = unw_get_reg(&cursor, UNW_REG_IP, &pc))) { 82 fprintf(stderr, "unw_get_reg UNW_REG_IP failed: %s [%d]\n", 83 unw_strerror(r), r); 84 return; 85 } 86 87 if ((r = unw_get_proc_info_by_ip(unw_local_addr_space, pc, &pip, NULL))) { 88 fprintf(stderr, "unw_get_proc_info_by_ip failed: %s [%d]\n", 89 unw_strerror(r), r); 90 return; 91 } 92 } else { 93 fprintf(stderr, "unw_get_proc_info failed: %s [%d]\n", 94 unw_strerror(r), r); 95 return; 96 } 60 97 } 61 98 … … 71 108 (void *) (pip.start_ip + off)); 72 109 } 110 111 if (r < 0) 112 fprintf(stderr, "unw_step failed: %s [%d]\n", 113 unw_strerror(r), r); 73 114 } 74 115
Note: See TracChangeset
for help on using the changeset viewer.