獲取執行緒名:自己封裝prctl,或使用pthread_getname_np
阿新 • • 發佈:2018-12-15
prctl(PR_GET_NAME, tname);
#include <sys/prctl.h> static char tname[16]; static char* get_current_threadname(){ prctl(PR_GET_NAME, tname); return tname; } //#define P_LOGI(fmt, ...) ALOGI("[%s ],tid:%u,%s,F:%s:%d,Fun:%s " fmt,(char*)get_current_threadname(), (unsigned int)gettid(),mark_label,(char*)__FILE__,__LINE__,(char*)__FUNCTION__,##__VA_ARGS__)
之前自己寫的bug無數的介面:一直不明白為什麼我自己寫的就會導致執行緒錯亂!!!
//------------------------------------------------------------------------------ //personal debug switch////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ #ifndef P_LOGI #define P_DEBUG 1 //0 turn off debug ,1 turn on debug #if P_DEBUG #include "utils/Log.h" //#ifndef _PTHREAD_H_ #include <pthread.h> //#define _PTHREAD_H_ //#endif /* *#ifndef _LOGHEADER_H_ *#include "../../../gecko/ipc/contentproc/logheader.h" *#define ENABLE_THREAD_NAME 1 // 0 disable thread name, 1 enable thread name *#endif */ #ifndef ENABLE_THREAD_NAME #define ENABLE_THREAD_NAME 1 // 0 disable thread name, 1 enable thread name #if ENABLE_THREAD_NAME //begin of ENABLE_THREAD_NAME //#define BUF_SIZE 1024 /* *static char* getNameByPid(pid_t pid,pid_t tid) { * char proc_pid_path[BUF_SIZE]; * char buf[BUF_SIZE]; * char *task_name = new char ; * sprintf(proc_pid_path, "/proc/%d/task/%d/stat", pid,tid); * FILE* fp = fopen(proc_pid_path, "r"); * if(NULL != fp){ * if( fgets(buf, BUF_SIZE-1, fp)== NULL ){ * fclose(fp); * } * fclose(fp); * //sscanf(buf, "%*s%[^\n]%*s", task_name); * sscanf(buf, "%*s%*[^(](%[^)]s", task_name); * return task_name; * } * else{ * delete task_name; * return (char*)"name-unknown"; * } *} */ #endif //end of ENABLE_THREAD_NAME 1 #endif //end of ifndef ENABLE_THREAD_NAME static char mark_label[64] = "pmain-Nuwa"; #define SEPARATOR_LINE "----------------------------------" #define LONNG_LINE = "------------------------------------------------------------------------------" #if ENABLE_THREAD_NAME //begin of ENABLE_THREAD_NAME #define P_LOGI(fmt, ...) ALOGI("[%s ],tid:%u,%s,F:%s:%d,Fun:%s " fmt,(char*)get_current_threadname(), (unsigned int)gettid(),mark_label,(char*)__FILE__,__LINE__,(char*)__FUNCTION__,##__VA_ARGS__) #else // log without thread name #define P_LOGI(fmt, ...) ALOGI("[%s],tid:%u,%s,F:%s:%d,Fun:%s " fmt,"", (unsigned int)gettid(),mark_label,(char*)__FILE__,__LINE__,(char*)__FUNCTION__,##__VA_ARGS__) #endif //end of ENABLE_THREAD_NAME #else #define P_LOGI(...) #endif //end of P_DBUG #endif //end of P_LOGI //------------------------------------------------------------------------------ /////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------
原因分析:以後寫。。。