Skip to content

Commit 107ffcc

Browse files
yinjipingclaude
andcommitted
fix: prepare ebpf worker threads for crash capture
Route C/eBPF worker creation through monitored thread helpers so covered threads install crash-monitor preparation before running. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d42537e commit 107ffcc

5 files changed

Lines changed: 127 additions & 64 deletions

File tree

agent/src/ebpf/user/profile/java/jvm_symbol_collect.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -871,22 +871,16 @@ static int thread_pool_add_task(symbol_collect_thread_pool_t * pool,
871871
pool->thread_index = pool->thread_count;
872872

873873
if ((ret =
874-
pthread_create(&thread, NULL, &worker_thread, pool)) < 0) {
874+
create_monitored_pthread("java-sym-wk", &thread,
875+
worker_thread, pool,
876+
true)) != ETR_OK) {
875877
ebpf_warning(JAVA_LOG_TAG
876878
"Create worker thread failed with '%s(%d)'\n",
877879
strerror(errno), errno);
878880
pthread_mutex_unlock(&pool->lock);
879881
return -2;
880882
}
881883

882-
if (pthread_detach(thread) != 0) {
883-
ebpf_warning(JAVA_LOG_TAG
884-
"Failed to detach thread with '%s(%d)'\n",
885-
strerror(errno), errno);
886-
pthread_mutex_unlock(&pool->lock);
887-
return -1;
888-
}
889-
890884
task_thread_t *new_threads = realloc(pool->threads,
891885
(++pool->thread_count) *
892886
sizeof(task_thread_t));

agent/src/ebpf/user/socket.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,11 +2362,12 @@ static int dispatch_workers_setup(struct bpf_tracer *tracer,
23622362

23632363
pthread_mutex_init(&tracer->queues[i].mutex, NULL);
23642364
pthread_cond_init(&tracer->queues[i].cond, NULL);
2365-
ret =
2366-
pthread_create(&tracer->dispatch_workers[i], NULL,
2367-
(void *)&process_data,
2368-
(void *)&tracer->queues[i]);
2369-
if (ret) {
2365+
ret = create_monitored_work_thread(name,
2366+
&tracer->dispatch_workers[i],
2367+
process_data,
2368+
(void *)&tracer->queues[i],
2369+
true);
2370+
if (ret != ETR_OK) {
23702371
ebpf_info
23712372
("<%s> process_data, pthread_create is error:%s\n",
23722373
__func__, strerror(errno));
@@ -2729,10 +2730,10 @@ int running_socket_tracer(tracer_callback_t handle,
27292730

27302731
if ((ret = sockopt_register(&datadump_sockopts)) != ETR_OK)
27312732
return ret;
2732-
ret =
2733-
pthread_create(&proc_events_pthread, NULL,
2734-
(void *)&process_events_handle_main, (void *)tracer);
2735-
if (ret) {
2733+
ret = create_monitored_work_thread("proc-events", &proc_events_pthread,
2734+
process_events_handle_main, (void *)tracer,
2735+
true);
2736+
if (ret != ETR_OK) {
27362737
ebpf_warning
27372738
("proc_events_pthread, pthread_create is error:%s\n",
27382739
strerror(errno));

agent/src/ebpf/user/tracer.c

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -294,32 +294,17 @@ int enable_tracer_reader_work(const char *prefix_name, int idx,
294294
{
295295
int ret;
296296
char name[TASK_COMM_LEN];
297+
297298
snprintf(name, sizeof(name), "%s-%d", prefix_name, idx);
298-
ret = pthread_create(&tracer->perf_workers[idx], NULL, fn,
299-
(void *)(uint64_t) idx);
300-
if (ret) {
299+
ret = create_monitored_pthread(name, &tracer->perf_workers[idx],
300+
(pthread_thread_func_t)fn,
301+
(void *)(uint64_t)idx, true);
302+
if (ret != ETR_OK) {
301303
ebpf_warning("tracer reader(%s), pthread_create "
302304
"is error:%s\n", name, strerror(errno));
303305
return ETR_INVAL;
304306
}
305307

306-
/* set thread name */
307-
pthread_setname_np(tracer->perf_workers[idx], name);
308-
309-
/*
310-
* Separating threads is to automatically release
311-
* resources after pthread_exit(), without being
312-
* blocked or stuck.
313-
*/
314-
ret = pthread_detach(tracer->perf_workers[idx]);
315-
if (ret != 0) {
316-
ebpf_warning("Error detaching thread, error:%s\n",
317-
strerror(errno));
318-
return ETR_INVAL;
319-
} else {
320-
ebpf_info("thread %s, detached successful.", name);
321-
}
322-
323308
return ETR_OK;
324309
}
325310

@@ -1729,12 +1714,18 @@ static void period_process_main(__unused void *arg)
17291714
pthread_t threads[sys_cpus_count];
17301715
int i;
17311716
for (i = 0; i < sys_cpus_count; i++) {
1732-
if (cpu_online[i])
1733-
if (pthread_create
1734-
(&threads[i], NULL, kick_kern_push_data,
1735-
(void *)(uintptr_t) i) != 0) {
1736-
ebpf_warning("pthread_create failed");
1737-
}
1717+
int ret;
1718+
char name[NAME_LEN];
1719+
1720+
if (!cpu_online[i])
1721+
continue;
1722+
1723+
snprintf(name, sizeof(name), "kick-kern.%d", i);
1724+
ret = create_monitored_pthread(name, &threads[i],
1725+
kick_kern_push_data,
1726+
(void *)(uintptr_t)i, true);
1727+
if (ret != ETR_OK)
1728+
ebpf_warning("pthread_create failed");
17381729
}
17391730

17401731
// Only this unique identifier can be adapted to the kernel
@@ -2279,8 +2270,9 @@ int bpf_tracer_init(const char *log_file, bool is_stdout)
22792270
if ((err = sockopt_register(&match_pids_sockopts)) != ETR_OK)
22802271
return err;
22812272

2282-
err = pthread_create(&ctrl_pthread, NULL, (void *)&ctrl_main, NULL);
2283-
if (err) {
2273+
err = create_monitored_work_thread("ctrl-main", &ctrl_pthread,
2274+
ctrl_main, NULL, true);
2275+
if (err != ETR_OK) {
22842276
ebpf_info("<%s> ctrl_pthread, pthread_create is error:%s\n",
22852277
__func__, strerror(errno));
22862278
return ETR_INVAL;
@@ -2315,10 +2307,9 @@ int bpf_tracer_init(const char *log_file, bool is_stdout)
23152307
SYS_TIME_UPDATE_PERIOD))
23162308
return ETR_INVAL;
23172309

2318-
err =
2319-
pthread_create(&cpus_kick_pthread, NULL,
2320-
(void *)&period_process_main, NULL);
2321-
if (err) {
2310+
err = create_monitored_work_thread("period-process", &cpus_kick_pthread,
2311+
period_process_main, NULL, true);
2312+
if (err != ETR_OK) {
23222313
ebpf_info
23232314
("<%s> cpus_kick_pthread, pthread_create is error:%s\n",
23242315
__func__, strerror(errno));

agent/src/ebpf/user/utils.c

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

16741742
static inline int compare(const void *a, const void *b)
16751743
{

agent/src/ebpf/user/utils.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,16 @@ void format_port_ranges(uint16_t *ports, size_t size, char *ret_str, int str_sz)
342342
* @return 32-bit hash value computed over the input data.
343343
*/
344344
uint32_t murmurhash(const void *key, size_t len, uint32_t seed);
345-
#if !defined(AARCH64_MUSL) && !defined(JAVA_AGENT_ATTACH_TOOL)
345+
#if !defined(JAVA_AGENT_ATTACH_TOOL)
346+
typedef void (*work_thread_func_t)(void *);
347+
typedef void *(*pthread_thread_func_t)(void *);
348+
349+
int create_monitored_work_thread(const char *name, pthread_t *t,
350+
work_thread_func_t fn, void *arg,
351+
bool detached);
352+
int create_monitored_pthread(const char *name, pthread_t *t,
353+
pthread_thread_func_t fn, void *arg,
354+
bool detached);
346355
int create_work_thread(const char *name, pthread_t *t, void *fn, void *arg);
347-
#endif /* !defined(AARCH64_MUSL) && !defined(JAVA_AGENT_ATTACH_TOOL) */
356+
#endif /* !defined(JAVA_AGENT_ATTACH_TOOL) */
348357
#endif /* DF_COMMON_H */

0 commit comments

Comments
 (0)