Repository: dotnet/runtime
File: src/coreclr/debug/createdump/crashinfounix.cpp (C++)

Code before:
    if (statusFile == nullptr)
    {
        printf_error("GetStatus fopen(%s) FAILED %s (%d)\n", statusPath, strerror(errno), errno);
        return false;
    }

    *ppid = -1;

    char *line = nullptr;
    size_t lineLen = 0;
    ssize_t read;
    while ((read = getline(&line, &lineLen, statusFile)) != -1)
    {
        if (strncmp("PPid:\t", line, 6) == 0)
        {
            *ppid = atoll(line + 6);
        }
        else if (strncmp("Tgid:\t", line, 6) == 0)
        {
            *tgid = atoll(line + 6);
        }
        else if (strncmp("Name:\t", line, 6) == 0)
        {
            if (name != nullptr)
            {
                // Try reading the executable name from the /proc/<pid>/exe link.
                char exePath[128];
                int chars = snprintf(exePath, sizeof(exePath), "/proc/%d/exe", pid);
                if (chars > 0 && (size_t)chars < sizeof(exePath))
                {
                    struct stat sb;
                    if (lstat(exePath, &sb) != -1) 
                    {
                        ssize_t bufSize = sb.st_size == 0 ? 4096 : sb.st_size + 1;
                        char *buf = (char*) malloc(bufSize);
                        if (buf != NULL) 
                        {
                            ssize_t nbytes = readlink(exePath, buf, bufSize);
                            if (nbytes != -1) 
                            {
                                buf[nbytes] = '\0'; 
                                char* executableName = strrchr(buf, '/');
                                *name = strdup((executableName != nullptr) ? (executableName + 1) : buf);
                                free(buf);
                                continue;
                            }
                            free(buf);
                        }
                    }
                }
                

Review comment (refactor):
Since this method is named 'GetStatus', I think it is surprising behavior that it would return a name that didn't come from parsing /proc/pid/status. The behavior could be a little more easily understood if you refactored to use a new method something like:

```c++
bool GetProcessInfo(pid_t pid, pid_t* ppid, pid_t* tgid, std::string* name)
{
    if(!GetStatus(pid, ppid, tgid, name))
    {
        return false;
    }
    
    // Try reading the executable name from the /proc/<pid>/exe link. Prefer this name to the
    // one reported by status if it is available because the status name is often truncated
    <all the code you have here>
    
    return true;
}
```

Then you could replace the existing call to GetStatus() with GetProcessInfo().

Code after:
    {
        printf_error("GetStatus fopen(%s) FAILED %s (%d)\n", statusPath, strerror(errno), errno);
        return false;
    }

    *ppid = -1;

    char *line = nullptr;
    size_t lineLen = 0;
    ssize_t read;
    while ((read = getline(&line, &lineLen, statusFile)) != -1)
    {
        if (strncmp("PPid:\t", line, 6) == 0)
        {
            *ppid = atoll(line + 6);
        }
        else if (strncmp("Tgid:\t", line, 6) == 0)
        {
            *tgid = atoll(line + 6);
        }
        else if (strncmp("Name:\t", line, 6) == 0)
        {
            if (name != nullptr)
            {
                char* n = strchr(line + 6, '\n');
                if (n != nullptr)
                {
                    *n = '\0';
                }
                *name = line + 6;
            }
        }
    }

    free(line);
    fclose(statusFile);
    return true;
}

bool
GetProcessInfo(pid_t pid, pid_t* ppid, pid_t* tgid, std::string* name)
{
    if(!GetStatus(pid, ppid, tgid, name))
    {
        return false;
    }

    // Try reading the executable name from the /proc/<pid>/exe link. Prefer this name to the
    // one reported by status if it is available because the status name is often truncated
    char exePath[128];
    int chars = snprintf(exePath, sizeof(exePath), "/proc/%d/exe", pid);