/* Program used to start the malloc logging on another program */ #include #include #include #include #include using namespace std; static void usage() { fprintf (stderr,"mapmalloc version %s\n",PACKAGE_REV); fprintf (stderr,"mapmalloc [ --prefix prefix-du-fichier-log ] programme argument...\n"); exit (-1); } int main (int argc, char *argv[]) { int ret = -1; if (argc < 2){ usage(); }else{ int start; string prefix; for (start=1; start < argc; start++){ const char *arg = argv[start]; const char *narg = start < argc-1 ? argv[start+1] : ""; if (strcmp(argv[start],"--prefix")==0){ start++; prefix = narg; }else if (arg[0] == '-'){ usage(); }else{ break; } } if (start >= argc){ usage(); }else{ if (prefix.size() == 0){ const char *prog = strrchr(argv[start],'/'); if (prog != NULL){ prefix = prog+1; }else{ prefix = argv[start] + string(".mapmalloc"); } } string tmp = "MAPMALLOCPREFIX=" + prefix; putenv ((char*)tmp.c_str()); putenv ("LD_PRELOAD=/usr/lib/mapmalloc/btmalloc.so"); int tb[2]; if (pipe(tb)!=-1){ dup2 (tb[0],976); close (tb[0]); write (tb[1],prefix.c_str(),prefix.size()); close (tb[1]); } execvp (argv[start],argv+start); } } return ret; }