source: XIOS/dev/dev_ym/XIOS_COUPLING/src/addr2line.hpp @ 2230

Last change on this file since 2230 was 2212, checked in by ymipsl, 3 years ago

Revisiting Memory tracking :

  • MemTrack? has been improved
    • Not need anymore to use private external libaddr2line, fork addr2line process internaly and use bidrectionnale pipe to send stack adress et get in return the demangle stack name
    • Can use cxa_demangle in backup
  • Block memory leak report is output in separate file (xios_xxx.mem),memory block are ordonned in decreasing size.
  • Possibility to output only the n bigest bloc with : "memtrack_blocs" xios parameters
  • Possibility to output only bloc over a given size with : "memtrack_size" xios parameters
  • Implement new method to retrieve the memory consumed in a time interval very similarely to xios timer :

CMemTracker("xios").resume()
CMemTracker("xios").suspend() ;
etc....

YM

  • Property svn:executable set to *
File size: 2.2 KB
Line 
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
18namespace 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
Note: See TracBrowser for help on using the repository browser.