glibc function to retrieve current executable name

2019-03-13 04:05发布

问题:

i'm wondering if there is a glibc function that i can use from gcc/g++ that will retrieve the current executable.

The purpose of this is to provide the -e argument to addr2line as shown in this answer

回答1:

In standard C and glibc, you have argv[0]:

int main (int argc, char *argv[])

the first element of the argv array is the program name.

However it's not necessarily enough on its own to determine where exactly the executable is. The argument is actually set by the program that ran your program - be it a shell or a window manager - and they aren't terribly helpful. If your program is in the path and you run the program simply with

your_program

at a bash shell, then "your_program" is all you will get in argv[0].

For the full executable path, linux has the /proc filesystem. Under /proc each running process gets its own "directory", named by its process id. The running process can also see its own subtree under /proc/self. One of the files that each process gets is /proc/[pid]/exe, which is a symbolic link to the actual executable the process is running.

So you can get the actual full executable path like this:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(void) {
    char exe[1024];
    int ret;

    ret = readlink("/proc/self/exe",exe,sizeof(exe)-1);
    if(ret ==-1) {
        fprintf(stderr,"ERRORRRRR\n");
        exit(1);
    }
    exe[ret] = 0;
    printf("I am %s\n",exe);
}

You may also be able to pass /proc/[pid]/exe directly to addr2line().



回答2:

You can access argv[0] without a reference to the actual variable, by using a saved pointer in glibc: https://sourceware.org/git/?p=glibc.git;a=blob;f=misc/init-misc.c;h=2a1b82710ec8b42b4dac6edb359d8920f902cd21;hb=HEAD

Example usage:

extern const char *__progname;

int print_progname()
{
    return puts(__progname);
}

argv[0] does not necessarily reflect the name that was used to invoke the program though. See man 2 execve and man 7 environ for more information.