[2212] | 1 | #ifdef XIOS_MEMTRACK |
---|
| 2 | |
---|
| 3 | #ifndef __ADDR2LINE_HPP__ |
---|
| 4 | #define __ADDR2LINE_HPP__ |
---|
| 5 | |
---|
| 6 | #include <sys/types.h> |
---|
| 7 | #include<sys/wait.h> |
---|
| 8 | #include<sys/prctl.h> |
---|
| 9 | #include <unistd.h> |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <stdio.h> |
---|
| 12 | #include <errno.h> |
---|
| 13 | #include <string.h> |
---|
| 14 | #include <string> |
---|
| 15 | #include <iostream> |
---|
| 16 | #include <fstream> |
---|
| 17 | |
---|
| 18 | namespace xios |
---|
| 19 | { |
---|
| 20 | class CAddr2line |
---|
| 21 | { |
---|
| 22 | public: |
---|
| 23 | |
---|
| 24 | CAddr2line(void) ; |
---|
| 25 | ~CAddr2line() ; |
---|
| 26 | |
---|
| 27 | void write(const std::string& str) ; |
---|
| 28 | void read(std::string& str) ; |
---|
| 29 | |
---|
| 30 | private: |
---|
| 31 | pid_t child_pid; |
---|
| 32 | int from_child, to_child; |
---|
| 33 | } ; |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | CAddr2line::CAddr2line(void) |
---|
| 37 | { |
---|
| 38 | pid_t p; |
---|
| 39 | int pipe_stdin[2], pipe_stdout[2]; |
---|
| 40 | char execfile[1000] ; |
---|
| 41 | ssize_t size=readlink("/proc/self/exe",execfile,1000) ; |
---|
| 42 | execfile[size]='\0' ; |
---|
| 43 | std::string cmdline = "addr2line -e "; |
---|
| 44 | cmdline=cmdline+execfile ; |
---|
| 45 | |
---|
| 46 | int err ; |
---|
| 47 | err=pipe(pipe_stdin) ; |
---|
| 48 | // if (err) return -1; // make an exception// |
---|
| 49 | err=pipe(pipe_stdout) ; |
---|
| 50 | // if (err) return -1; // make an exception// |
---|
| 51 | |
---|
| 52 | p = fork(); |
---|
| 53 | |
---|
| 54 | if(p < 0) return ; // make an exception// |
---|
| 55 | if(p == 0) |
---|
| 56 | { /* child */ |
---|
| 57 | close(pipe_stdin[1]); |
---|
| 58 | dup2(pipe_stdin[0], 0); |
---|
| 59 | close(pipe_stdout[0]); |
---|
| 60 | dup2(pipe_stdout[1], 1); |
---|
| 61 | execl("/bin/sh", "sh", "-c", cmdline.c_str(), NULL); |
---|
| 62 | std::cout<<"child exit !!!"<<std::endl ; |
---|
| 63 | perror("execl"); exit(99); |
---|
| 64 | } |
---|
| 65 | child_pid = p; |
---|
| 66 | to_child = pipe_stdin[1]; |
---|
| 67 | from_child = pipe_stdout[0]; |
---|
| 68 | close(pipe_stdin[0]); |
---|
| 69 | close(pipe_stdout[1]); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void CAddr2line::write(const std::string& str) |
---|
| 73 | { |
---|
| 74 | std::string strTmp=str+'\n' ; |
---|
| 75 | ::write(to_child, strTmp.data(), strTmp.size()) ; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void CAddr2line::read(std::string& str) |
---|
| 79 | { |
---|
| 80 | const int maxSize=10000 ; |
---|
| 81 | char buffer[maxSize] ; |
---|
| 82 | |
---|
| 83 | int a ; |
---|
| 84 | for(int i=0;i<maxSize;i++) |
---|
| 85 | { |
---|
| 86 | a = ::read(from_child,&buffer[i],1) ; |
---|
| 87 | if (a==-1) std::cout<<::strerror(errno)<<std::endl ; |
---|
| 88 | if(buffer[i]=='\n') |
---|
| 89 | { |
---|
| 90 | buffer[i]='\0' ; |
---|
| 91 | break ; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | str=buffer ; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | CAddr2line::~CAddr2line() |
---|
| 98 | { |
---|
| 99 | |
---|
| 100 | ::close(from_child) ; |
---|
| 101 | ::close(to_child) ; |
---|
| 102 | waitpid(child_pid, NULL, 0) ; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | #endif |
---|
| 108 | |
---|
| 109 | #endif |
---|