gh-125434: Display thread name in faulthandler (#132016)

This commit is contained in:
Victor Stinner 2025-04-04 14:24:41 +02:00 committed by GitHub
parent 2ccd6aae4d
commit 37d47d4965
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 14 deletions

View file

@ -21,7 +21,7 @@
#define OFF(x) offsetof(PyTracebackObject, x)
#define PUTS(fd, str) (void)_Py_write_noraise(fd, str, (int)strlen(str))
#define PUTS(fd, str) (void)_Py_write_noraise(fd, str, strlen(str))
#define MAX_STRING_LENGTH 500
#define MAX_FRAME_DEPTH 100
@ -1054,6 +1054,27 @@ write_thread_id(int fd, PyThreadState *tstate, int is_current)
_Py_DumpHexadecimal(fd,
tstate->thread_id,
sizeof(unsigned long) * 2);
// Write the thread name
#if defined(HAVE_PTHREAD_GETNAME_NP) || defined(HAVE_PTHREAD_GET_NAME_NP)
char name[100];
pthread_t thread = (pthread_t)tstate->thread_id;
#ifdef HAVE_PTHREAD_GETNAME_NP
int rc = pthread_getname_np(thread, name, Py_ARRAY_LENGTH(name));
#else /* defined(HAVE_PTHREAD_GET_NAME_NP) */
int rc = 0; /* pthread_get_name_np() returns void */
pthread_get_name_np(thread, name, Py_ARRAY_LENGTH(name));
#endif
if (!rc) {
size_t len = strlen(name);
if (len) {
PUTS(fd, " [");
(void)_Py_write_noraise(fd, name, len);
PUTS(fd, "]");
}
}
#endif
PUTS(fd, " (most recent call first):\n");
}