Changeset 0a0ef57


Ignore:
Timestamp:
05/28/15 08:00:26 (9 years ago)
Author:
Hal Finkel <hfinkel@…>
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)
Message:

unw_get_proc_info does not work on ppc/ppc64; add getrusage info

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Makefile

    r0ec59c5 r0a0ef57  
    44UNW_HOME = ${HOME}/install/libunwind 
    55CPPFLAGS = -I$(UNW_HOME)/include 
    6 LDFLAGS = -L$(UNW_HOME)/lib64 -Wl,-rpath,$(UNW_HOME)/lib64 -lunwind -lpthread 
     6LDFLAGS = -L$(UNW_HOME)/lib64 -Wl,-rpath,$(UNW_HOME)/lib64 -lunwind -lpthread -ldl 
    77 
    88all: memlog.so memlog.a 
  • memlog.c

    r0ec59c5 r0a0ef57  
    88#include <string.h> 
    99 
     10#include <sys/time.h> 
     11#include <sys/resource.h> 
    1012#include <sys/types.h> 
    1113#include <sys/stat.h> 
     
    3234  snprintf(log_name, PATH_MAX, "%s.%d.memory.log_file", u.nodename, getpid()); 
    3335  log_file = fopen(log_name, "w"); 
     36  if (!log_file) 
     37    fprintf(stderr, "fopen failed for '%s': %m\n", log_name); 
    3438} 
    3539 
     
    4448 
    4549static 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; 
    4660  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) { 
    5568    unw_word_t off; 
    5669    char proc_name[PATH_MAX]; 
     
    5871      off = 0; 
    5972      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      } 
    6097    } 
    6198 
     
    71108            (void *) (pip.start_ip + off)); 
    72109  } 
     110 
     111  if (r < 0) 
     112    fprintf(stderr, "unw_step failed: %s [%d]\n", 
     113            unw_strerror(r), r); 
    73114} 
    74115 
Note: See TracChangeset for help on using the changeset viewer.