Skip to content

Add Android support for choc_DetectDebugger #99

@manol-o

Description

@manol-o

Hi,

I am using this code quite a while and I had no problems.
Would be cool if it could be added to develop:


#elif CHOC_ANDROID

#include <sys/ptrace.h>
#include <unistd.h>
#include <fcntl.h>
#include <string>
#include <android/log.h>

inline bool choc::isDebuggerActive()
{
    // Method 1: Check TracerPid in /proc/self/status (similar to Linux)
    auto readTracerPid = []() -> int
    {
        int tracerPid = 0;
        int fd = open("/proc/self/status", O_RDONLY);
        if (fd == -1)
            return 0;

        char buf[512];
        ssize_t n = read(fd, buf, sizeof(buf)-1);
        close(fd);
        if (n <= 0)
            return 0;

        buf[n] = '\0';
        std::string status(buf);
        std::string tracerPidStr = "TracerPid:";
        size_t pos = status.find(tracerPidStr);

        if (pos != std::string::npos)
        {
            size_t lineEnd = status.find('\n', pos);
            std::string line = status.substr(pos, lineEnd - pos);
            try
            {
                tracerPid = std::stoi(line.substr(tracerPidStr.length()));
            }
            catch (...) {}
        }
        return tracerPid;
    };

    return readTracerPid() > 0;
}

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions