Hey Jon,

I think we should give debuggers a chance to announce themselves by
providing an entry point they can call (in the inferior) which sets a
flag.  A set flag plus a tracer PID would then be a sufficient
indicator.  The remaining code should also stay but some additional
code can be added:

On Sat, Jun 1, 2024 at 12:22 PM Jonathan Wakely <jwak...@redhat.com> wrote:
> +_GLIBCXX_WEAK_DEFINITION
> +bool
> +std::is_debugger_present() noexcept
> +{
> +#if _GLIBCXX_HOSTED
> +# if _GLIBCXX_USE_PROC_SELF_STATUS
> +  const string_view prefix = "TracerPid:\t";
> +  ifstream in("/proc/self/status");
> +  string line;
> +  while (std::getline(in, line))
> +    {
> +      if (!line.starts_with(prefix))
> +       continue;
> +
> +      string_view tracer = line;
> +      tracer.remove_prefix(prefix.size());
> +      if (tracer.size() == 1 && tracer[0] == '0') [[likely]]
> +       return false; // Not being traced.
> +

Here add something like:

   if (debugger_announced)
     return true;


> +      in.close();
> +      string_view cmd;
> +      string proc_dir = "/proc/" + string(tracer) + '/';
> +      in.open(proc_dir + "comm"); // since Linux 2.6.33
> +      if (std::getline(in, line)) [[likely]]
> +       cmd = line;
> +      else
> +       {
> +         in.close();
> +         in.open(proc_dir + "cmdline");
> +         if (std::getline(in, line))
> +           cmd = line.c_str(); // Only up to first '\0'
> +         else
> +           return false;
> +       }
> +
> +      for (auto i : {"gdb", "lldb"}) // known debuggers
> +       if (cmd.ends_with(i))
> +         return true;
> +
> +      // We found the TracerPid line, no need to do any more work.
> +      return false;
> +    }
> +# endif

And then add

namespace {
  bool debugger_announced = false;
}
auto debugger_attached()
{
  debugger_announced = true;
  return std::breakpoint;
}

I suggest to also return the breakpoint function to allow debuggers to
do something clever with it (e.g., set a breakpoint on the entry
instead of having to catch the fallout of the instruction that is
issued in the function (there might be a difference).


With the function any debugger not covered by the existing test can
make itself known.

Reply via email to