Changeset 1bd82e0


Ignore:
Timestamp:
05/29/15 07:53:00 (9 years ago)
Author:
Hal Finkel <hfinkel@…>
Branches:
master
Children:
e1b15ec
Parents:
966f5de
git-author:
Hal Finkel <hfinkel@…> (05/29/15 07:53:00)
git-committer:
Hal Finkel <hfinkel@…> (05/29/15 07:53:00)
Message:

adjust for c++, use caching of dladdr, fixup for short names when making dot

Files:
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • Makefile

    r493cb97 r1bd82e0  
    1 CC = /soft/compilers/bgclang/wbin/bgclang 
    2 CFLAGS = -std=gnu99 -O3 -g 
     1CXX = /soft/compilers/bgclang/wbin/bgclang++ 
     2CXXFLAGS = -std=gnu++11 -O3 -g 
    33 
    44CPPFLAGS = 
     
    77all: libmemlog.so memlog_s.o 
    88 
    9 memlog_s.o: memlog.c 
    10         $(CC) $(CPPFLAGS) $(CFLAGS) -c -o memlog_s.o memlog.c 
     9memlog_s.o: memlog.cpp 
     10        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o memlog_s.o memlog.cpp 
    1111 
    12 libmemlog.so: memlog.c 
    13         $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -fPIC -shared -o libmemlog.so memlog.c 
     12libmemlog.so: memlog.cpp 
     13        $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -fPIC -shared -o libmemlog.so memlog.cpp 
    1414 
    1515clean: 
  • memlog.cpp

    r966f5de r1bd82e0  
    33#endif 
    44 
    5 #include <stdlib.h> 
    6 #include <stdio.h> 
     5#include <cstdlib> 
     6#include <cstdio> 
     7#include <cstring> 
     8 
     9#include <unordered_map> 
     10#include <utility> 
     11 
    712#include <limits.h> 
    8 #include <string.h> 
    9  
    1013#include <malloc.h> 
    1114#include <execinfo.h> 
     
    2225#include <dlfcn.h> 
    2326 
     27using namespace std; 
     28 
    2429// NOTE: When static linking, this depends on linker wrapping. 
    2530// Add to your LDFLAGS: 
     
    6873// dladdr is, relatively, quit slow. For this to work on a large application, 
    6974// we need to cache the lookup results. 
    70 static int dladdr_cached(void *addr, Dl_info *info) { 
    71   return dladdr(addr, info); 
     75static int dladdr_cached(void * addr, Dl_info *info) { 
     76  static unordered_map<void *, Dl_info> dladdr_cache; 
     77 
     78  auto I = dladdr_cache.find(addr); 
     79  if (I == dladdr_cache.end()) { 
     80    int r; 
     81    if (!(r = dladdr(addr, info))) 
     82      memset(info, 0, sizeof(Dl_info)); 
     83 
     84    dladdr_cache.insert(make_pair(addr, *info)); 
     85    return r; 
     86  } 
     87 
     88  memcpy(info, &I->second, sizeof(Dl_info)); 
     89  return 1; 
    7290} 
    7391 
     
    176194// glibc exports its underlying malloc implementation under the name 
    177195// __libc_malloc so that hooks like this can use it. 
     196extern "C" { 
    178197extern void *__libc_malloc(size_t size); 
    179198extern void *__libc_realloc(void *ptr, size_t size); 
     
    277296} 
    278297 
     298} // extern "C" 
     299 
  • memlog2dot

    rbdaf020 r1bd82e0  
    206206    chomp($func); 
    207207 
     208    # It sometimes happens that addr2line is a bit too smart: when debugging 
     209    # information is available, it might print a local alias for the 
     210    # function instead of the full name (for example, printing 'List' 
     211    # instead of 'Foo::List<int>::List(int, int const&)'). 
     212    if ($node->{'proc_name'} ne '?') { 
     213      my $proc_name = $node->{'proc_name'}; 
     214      $proc_name = `c++filt '$proc_name'`; 
     215      chomp($proc_name); 
     216 
     217      if (length($proc_name) > length($func)) { 
     218        $func = $proc_name; 
     219      } 
     220    } 
     221 
    208222    $ret .= $func . '\n'; 
    209223 
Note: See TracChangeset for help on using the changeset viewer.