[Interest] Detect if app is running under Qt Creator's debugger

Alexander Dyagilev alervdvcw at gmail.com
Tue Apr 23 17:38:59 CEST 2024


David, thank you :)

By the way, it would be perfect to have a cross platform code. Windows-only
one is OK for me for now, but in case I switch to Linux (or macOS)...


On Tue, Apr 23, 2024 at 10:22 PM David M. Cotter <dave at kjams.com> wrote:

> // Returns true if the current process is being debugged (either
> // running under the debugger or has a debugger attached post facto).
> bool    CFDebuggerAttached()
> {
>         bool                            debuggedB(false);
>
>         #if defined(kDEBUG) || DEBUG
>         #if !defined(_HELPERTOOL_)// && !_YAAF_
>
>         #if OPT_WINOS
>         {
>                 debuggedB = IsDebuggerPresent();
>         }
>         #else // OPT_MACOS
>         {
>                 int                 junk;
>                 int                 mib[4];
>                 struct kinfo_proc   info;
>                 size_t              size;
>
>                 // Initialize the flags so that, if sysctl fails for some
> bizarre
>                 // reason, we get a predictable result.
>
>                 info.kp_proc.p_flag = 0;
>
>                 // Initialize mib, which tells sysctl the info we want, in
> this case
>                 // we're looking for information about a specific process
> ID.
>
>                 mib[0] = CTL_KERN;
>                 mib[1] = KERN_PROC;
>                 mib[2] = KERN_PROC_PID;
>                 mib[3] = getpid();
>
>                 // Call sysctl.
>
>                 size = sizeof(info);
>                 junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info,
> &size, NULL, 0);
>                 assert(junk == 0);
>
>                 // We're being debugged if the P_TRACED flag is set.
>
>                 debuggedB = ( (info.kp_proc.p_flag & P_TRACED) != 0 );
>         }
>         #endif  //      OPT_MACOS
>         #endif  //      !defined(_HELPERTOOL_) && !_YAAF_
>         #endif  //      DEBUG
>
>     return debuggedB;
> }
>
>
> > On Apr 23, 2024, at 7:33 AM, Alexander Dyagilev <alervdvcw at gmail.com>
> wrote:
> >
> > Hello,
> >
> > Is there a simple way to detect it?
> >
> > What I would like to achieve is to perform some debug check similar to
> one  Q_ASSERT does, but instead I want it to just print to console in case
> the app is NOT running under debugger and fall back to Q_ASSERT behavior
> otherwise (so I can see the stack trace then).
> > _______________________________________________
> > Interest mailing list
> > Interest at qt-project.org
> > https://lists.qt-project.org/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20240423/dc86fc79/attachment.htm>


More information about the Interest mailing list