Add support for several platforms to obtain the real thread ID.

Already had the pthread ID which is not the same.  The most obvious enhancement
is in the "core show threads" output. As stated in the utils header, if the
platform isn't supported -1 is reported (instead of the process ID previously).


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@298137 65c4cc65-6c06-0410-ace0-fbb531ad65f3
10-digiumphones
Jeff Peeler 15 years ago
parent cc541e6852
commit 78bd0de1a9

852
configure vendored

File diff suppressed because it is too large Load Diff

@ -774,6 +774,9 @@ AC_LINK_IFELSE(
#)
#fi
# for FreeBSD thr_self
AC_CHECK_HEADERS([sys/thr.h])
AC_MSG_CHECKING(for compiler atomic operations)
AC_LINK_IFELSE(
AC_LANG_PROGRAM([], [int foo1; int foo2 = __sync_fetch_and_add(&foo1, 1);]),

@ -915,6 +915,9 @@
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
/* Define to 1 if you have the <sys/thr.h> header file. */
#undef HAVE_SYS_THR_H
/* Define to 1 if you have the <sys/time.h> header file. */
#undef HAVE_SYS_TIME_H

@ -758,4 +758,11 @@ int ast_str_to_eid(struct ast_eid *eid, const char *s);
*/
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2);
/*!
* \brief Get current thread ID
* \param None
* \return the ID if platform is supported, else -1
*/
int ast_get_tid(void);
#endif /* _ASTERISK_UTILS_H */

@ -379,6 +379,7 @@ struct thread_list_t {
AST_RWLIST_ENTRY(thread_list_t) list;
char *name;
pthread_t id;
int lwp;
};
static AST_RWLIST_HEAD_STATIC(thread_list, thread_list_t);
@ -390,6 +391,7 @@ void ast_register_thread(char *name)
if (!new)
return;
new->id = pthread_self();
new->lwp = ast_get_tid();
new->name = name; /* steal the allocated memory for the thread name */
AST_RWLIST_WRLOCK(&thread_list);
AST_RWLIST_INSERT_HEAD(&thread_list, new, list);
@ -516,7 +518,7 @@ static char *handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cl
AST_RWLIST_RDLOCK(&thread_list);
AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
ast_cli(a->fd, "%p %s\n", (void *)cur->id, cur->name);
ast_cli(a->fd, "%p %d %s\n", (void *)cur->id, cur->lwp, cur->name);
count++;
}
AST_RWLIST_UNLOCK(&thread_list);

@ -58,15 +58,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#define MAX_BACKTRACE_FRAMES 20
#endif
#if defined(__linux__) && !defined(__NR_gettid)
#include <asm/unistd.h>
#endif
#if defined(__linux__) && defined(__NR_gettid)
#define GETTID() syscall(__NR_gettid)
#else
#define GETTID() getpid()
#endif
static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
static char queue_log_name[256] = QUEUELOG;
@ -128,7 +119,7 @@ struct logmsg {
enum logmsgtypes type;
int level;
int line;
long process_id;
int lwp;
AST_DECLARE_STRING_FIELDS(
AST_STRING_FIELD(date);
AST_STRING_FIELD(file);
@ -882,8 +873,8 @@ static void ast_log_vsyslog(struct logmsg *msg)
return;
}
snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: %s",
levels[msg->level], msg->process_id, msg->file, msg->line, msg->function, msg->message);
snprintf(buf, sizeof(buf), "%s[%d]: %s:%d in %s: %s",
levels[msg->level], msg->lwp, msg->file, msg->line, msg->function, msg->message);
term_strip(buf, buf, strlen(buf) + 1);
syslog(syslog_level, "%s", buf);
@ -928,10 +919,10 @@ static void logger_print_normal(struct logmsg *logmsg)
/* Turn the numerical line number into a string */
snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
/* Build string to print out */
snprintf(buf, sizeof(buf), "[%s] %s[%ld]: %s:%s %s: %s",
snprintf(buf, sizeof(buf), "[%s] %s[%d]: %s:%s %s: %s",
logmsg->date,
term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
logmsg->process_id,
logmsg->lwp,
term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
@ -948,8 +939,8 @@ static void logger_print_normal(struct logmsg *logmsg)
}
/* Print out to the file */
res = fprintf(chan->fileptr, "[%s] %s[%ld] %s: %s",
logmsg->date, logmsg->level_name, logmsg->process_id, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
res = fprintf(chan->fileptr, "[%s] %s[%d] %s: %s",
logmsg->date, logmsg->level_name, logmsg->lwp, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
if (errno == ENOMEM || errno == ENOSPC)
@ -1173,7 +1164,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
ast_string_field_set(logmsg, level_name, levels[level]);
ast_string_field_set(logmsg, file, file);
ast_string_field_set(logmsg, function, function);
logmsg->process_id = (long) GETTID();
logmsg->lwp = ast_get_tid();
/* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
if (logthread != AST_PTHREADT_NULL) {

@ -34,6 +34,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <fcntl.h>
#endif
#include <sys/syscall.h>
#if defined(__APPLE__)
#include <mach/mach.h>
#elif defined(HAVE_SYS_THR_H)
#include <sys/thr.h>
#endif
#include "asterisk/network.h"
#define AST_API_MODULE /* ensure that inlinable API functions will be built in lock.h if required */
@ -2081,3 +2088,21 @@ int _ast_asprintf(char **ret, const char *file, int lineno, const char *func, co
return res;
}
#endif
int ast_get_tid(void)
{
int ret = -1;
#if defined (__linux) && defined(SYS_gettid)
ret = syscall(SYS_gettid); /* available since Linux 1.4.11 */
#elif defined(__sun)
ret = pthread_self();
#elif defined(__APPLE__)
ret = mach_thread_self();
mach_port_deallocate(mach_task_self(), ret);
#elif defined(__FreeBSD__) && defined(HAVE_SYS_THR_H)
long lwpid;
thr_self(&lwpid); /* available since sys/thr.h creation 2003 */
ret = lwpid;
#endif
return ret;
}

Loading…
Cancel
Save