Index: ps/trunk/source/lib/sysdep/os/linux/ldbg.cpp =================================================================== --- ps/trunk/source/lib/sysdep/os/linux/ldbg.cpp +++ ps/trunk/source/lib/sysdep/os/linux/ldbg.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2020 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -33,6 +33,10 @@ #include "lib/sysdep/sysdep.h" #include "lib/debug.h" +#include +#include +#include + #if OS_ANDROID // Android NDK doesn't support backtrace() @@ -147,7 +151,30 @@ #endif -void debug_SetThreadName(char const* UNUSED(name)) +/* This is basically a reimplementation of the pthread_setname_np() glibc + * function, to make it work on every Linux distribution, no matter the libc + * used. + * + * The thread name limit is 16 (including the terminating null byte) on Linux, + * so we have to cut the provided name at 15 bytes. + * + * This API exists since Linux 2.6.33, on older kernels the user will just not + * get thread names. + */ +void debug_SetThreadName(const char* name) { - // Currently unimplemented + constexpr size_t MAX_THREAD_NAME_LEN = 15; + constexpr size_t MAX_FILE_PATH_LEN = 32; + + char pathname[MAX_FILE_PATH_LEN]; + pid_t tid = syscall(SYS_gettid); + snprintf(pathname, MAX_FILE_PATH_LEN, "/proc/self/task/%d/comm", tid); + + FILE* comm = fopen(pathname, "w"); + if (!comm) + return; + + std::string limited_name(name, std::min(strlen(name), MAX_THREAD_NAME_LEN)); + fputs(limited_name.c_str(), comm); + fclose(comm); } Index: ps/trunk/source/network/NetServer.cpp =================================================================== --- ps/trunk/source/network/NetServer.cpp +++ ps/trunk/source/network/NetServer.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2020 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -218,6 +218,8 @@ #if CONFIG2_MINIUPNPC void CNetServerWorker::SetupUPnP() { + debug_SetThreadName("UPnP"); + // Values we want to set. char psPort[6]; sprintf_s(psPort, ARRAY_SIZE(psPort), "%d", PS_DEFAULT_PORT);