libcsdbg  1.28
C++ exception (and generic) stack trace debug library
const i8 * csdbg::util::exec_path ( )
static

Get the absolute path of the executable.

Returns
the path (heap allocated)
Exceptions
std::bad_alloc
csdbg::exception

Definition at line 87 of file util.cpp.

References unlikely.

Referenced by csdbg::streambuf::header(), csdbg::tracer::on_lib_load(), and csdbg::filebuf::unique_id().

88 {
89  /*
90  * The procfs filesystem maintains a directory for each process (/proc/pid)
91  * and a symlink therein (exe) that contains the absolute path of the process
92  * executable
93  */
94  i8 path[PATH_MAX + 1];
95  i32 len = snprintf(path, PATH_MAX + 1, "/proc/%d/exe", getpid());
96  if ( unlikely(len < 0) )
97  throw exception("snprintf failed with retval %d", len);
98 
99  i8 *retval = new i8[PATH_MAX + 1];
100 
101  /* Read the contents of the symlink */
102  len = readlink(path, retval, PATH_MAX);
103  if ( unlikely(len < 0) ) {
104  delete[] retval;
105  throw exception(
106  "failed to read symlink '%s' (errno %d - %s)",
107  path,
108  errno,
109  strerror(errno)
110  );
111  }
112 
113  retval[len] = '\0';
114  return retval;
115 }
char i8
8-bit signed integer
Definition: config.hpp:72
int i32
32-bit signed integer
Definition: config.hpp:82
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349

+ Here is the caller graph for this function: