@@ -1639,37 +1639,105 @@ u32 djb2_32bit(const char *str)
16391639 return hash ; // 32-bit output
16401640}
16411641
1642- #if !defined(AARCH64_MUSL ) && !defined(JAVA_AGENT_ATTACH_TOOL )
1643- int create_work_thread (const char * name , pthread_t * t , void * fn , void * arg )
1642+ #if !defined(JAVA_AGENT_ATTACH_TOOL )
1643+ #include "crash_monitor.h"
1644+
1645+ enum monitored_thread_kind {
1646+ MONITORED_THREAD_WORK = 0 ,
1647+ MONITORED_THREAD_PTHREAD = 1 ,
1648+ };
1649+
1650+ struct monitored_thread_args {
1651+ enum monitored_thread_kind kind ;
1652+ work_thread_func_t work_fn ;
1653+ pthread_thread_func_t pthread_fn ;
1654+ void * arg ;
1655+ };
1656+
1657+ static void * monitored_thread_main (void * arg )
1658+ {
1659+ struct monitored_thread_args * thread_args = arg ;
1660+ void * ret = NULL ;
1661+
1662+ if (thread_args == NULL )
1663+ return NULL ;
1664+
1665+ (void )crash_monitor_prepare_thread ();
1666+ if (thread_args -> kind == MONITORED_THREAD_WORK ) {
1667+ if (thread_args -> work_fn != NULL )
1668+ thread_args -> work_fn (thread_args -> arg );
1669+ } else if (thread_args -> pthread_fn != NULL ) {
1670+ ret = thread_args -> pthread_fn (thread_args -> arg );
1671+ }
1672+
1673+ free (thread_args );
1674+ return ret ;
1675+ }
1676+
1677+ static int create_monitored_thread_common (const char * name , pthread_t * t ,
1678+ work_thread_func_t work_fn ,
1679+ pthread_thread_func_t pthread_fn ,
1680+ void * arg , bool detached )
16441681{
16451682 int ret ;
1646- ret = pthread_create (t , NULL , fn , arg );
1683+ struct monitored_thread_args * thread_args ;
1684+
1685+ thread_args = calloc (1 , sizeof (* thread_args ));
1686+ if (thread_args == NULL ) {
1687+ ebpf_warning ("worker name %s alloc failed:%s\n" ,
1688+ name , strerror (errno ));
1689+ return ETR_NOMEM ;
1690+ }
1691+
1692+ thread_args -> kind = (pthread_fn != NULL ) ?
1693+ MONITORED_THREAD_PTHREAD : MONITORED_THREAD_WORK ;
1694+ thread_args -> work_fn = work_fn ;
1695+ thread_args -> pthread_fn = pthread_fn ;
1696+ thread_args -> arg = arg ;
1697+
1698+ ret = pthread_create (t , NULL , monitored_thread_main , thread_args );
16471699 if (ret ) {
16481700 ebpf_warning ("worker name %s is error:%s\n" ,
16491701 name , strerror (errno ));
1702+ free (thread_args );
16501703 return ETR_INVAL ;
16511704 }
16521705
1653- /* set thread name */
16541706 pthread_setname_np (* t , name );
1707+ if (!detached )
1708+ return ETR_OK ;
16551709
1656- /*
1657- * Separating threads is to automatically release
1658- * resources after pthread_exit(), without being
1659- * blocked or stuck.
1660- */
16611710 ret = pthread_detach (* t );
16621711 if (ret != 0 ) {
16631712 ebpf_warning ("Error detaching thread, error:%s\n" ,
16641713 strerror (errno ));
16651714 return ETR_INVAL ;
1666- } else {
1667- ebpf_info ("thread %s, detached successful." , name );
16681715 }
16691716
1717+ ebpf_info ("thread %s, detached successful." , name );
16701718 return ETR_OK ;
16711719}
1672- #endif /* !defined(AARCH64_MUSL) && !defined(JAVA_AGENT_ATTACH_TOOL) */
1720+
1721+ int create_monitored_work_thread (const char * name , pthread_t * t ,
1722+ work_thread_func_t fn , void * arg ,
1723+ bool detached )
1724+ {
1725+ return create_monitored_thread_common (name , t , fn , NULL , arg , detached );
1726+ }
1727+
1728+ int create_monitored_pthread (const char * name , pthread_t * t ,
1729+ pthread_thread_func_t fn , void * arg ,
1730+ bool detached )
1731+ {
1732+ return create_monitored_thread_common (name , t , NULL , fn , arg , detached );
1733+ }
1734+
1735+ int create_work_thread (const char * name , pthread_t * t , void * fn , void * arg )
1736+ {
1737+ return create_monitored_work_thread (name , t ,
1738+ (work_thread_func_t )fn , arg , true);
1739+ }
1740+ #endif /* !defined(JAVA_AGENT_ATTACH_TOOL) */
16731741
16741742static inline int compare (const void * a , const void * b )
16751743{
0 commit comments