Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[arch] Arch_DebuggerIsAttachedPosix: use TracerPid in /proc/self/status #3014

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

pmolodo
Copy link
Contributor

@pmolodo pmolodo commented Mar 23, 2024

Description of Change(s)

The old implemenation worked by trying to attach a ptrace to the current process, and if it failed, then we assumed it was being debugged.

The problem is that we have no easy way to check if an EPERM result from ptrace is because it's being debugged, or an actual permissions error - and permissions checking process is somewhat complex - see the "Ptrace access mode checking" section here:

https://man7.org/linux/man-pages/man2/ptrace.2.html

On a basic Ubuntu-22.04 test system, ptrace would return EPERM even when the process was not being debugged, resulting in a "false positive". The new TracerPid method was more accurate in this case.

Checking the /proc/self/status for TracerPid is also the same method that gperftools and even gdb itself use to check if a process is being debugged:

Fixes Issue(s)

  • ArchDebuggerIsAttached would return a false positive on some Linux distros (ie, Ubuntu-22.04)
  • I have verified that all unit tests pass with the proposed changes
  • I have submitted a signed Contributor License Agreement

Copy link
Collaborator

@nvmkuruc nvmkuruc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pxr/base/arch/debugger.cpp Outdated Show resolved Hide resolved
@jesschimein
Copy link
Contributor

Filed as internal issue #USD-9478

@jesschimein
Copy link
Contributor

/AzurePipelines run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

The old implemenation worked by trying to attach a ptrace to the current
process, and if it failed, then we assumed it was being debugged.

The problem is that we have no easy way to check if an EPERM result
from ptrace is because it's being debugged, or an actual permissions
error - and permissions checking process is somewhat complex - see the
"Ptrace access mode checking" section here:

https://man7.org/linux/man-pages/man2/ptrace.2.html

On a basic Ubuntu-22.04 test system, ptrace would return EPERM even when
the process was not being debugged, resulting in a "false positive".
The new TracerPid method was more accurate in this case.

Checking the /proc/self/status for TracerPid is also the same method
that gperftools and even gdb itself use to check if a process is being
debugged:

- https://github.com/gperftools/gperftools/blob/02adc8ceab39bbeac1f65e10bde577e1753094fa/src/heap-checker.cc#L183
- https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gdb/nat/linux-procfs.c;h=b17e3120792e0e0790271898212b69b0577847cc;hb=HEAD#l68
- https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gdb/testsuite/gdb.threads/siginfo-threads.c;h=22c6038206ba77fc3432da5ee30284c80641f305;hb=HEAD#l373
@gitamohr
Copy link
Contributor

I'm pulling this one in today.

// Do nothing
if (line.compare(0, field.size(), field) == 0 && line[field.size()] == ':') {
// We found our "field:" line
return line.substr(field.size() + 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be + 1 no? We want the text after the colon?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - good catch. I think the what happened was I initially thought that what followed the : was (possibly multiple) space characters - ie, "Field: value", and decided not to deal with whitespace stripping, added this comment:

// Note that the returned string will generally include leading whitespace

But I then realized what followed was always a tab character - ie, "Field:\tvalue", and I modified the code to just skip the tab character... but forgot to add an explanatory comment (or remove the old comment).

Probably what you've proposed (locale-independent whitespace stripping) is more robust, though.

static
int Arch_ReadTracerPid() {
try {
return std::stoi(Arch_ReadProcStatusField("TracerPid"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm going to rework this bit to do manual whitespace skipping and std::from_chars() to make it locale-independent.

@jesschimein
Copy link
Contributor

/AzurePipelines run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@pixar-oss pixar-oss merged commit 46b1bb8 into PixarAnimationStudios:dev Nov 26, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants