Skip to content

Commit

Permalink
friendly to daemon
Browse files Browse the repository at this point in the history
Signed-off-by: Rongsong Shen <rshen@shenrs.eu>
  • Loading branch information
Rongsong Shen authored and Rongsong Shen committed Feb 7, 2024
1 parent 4a31fe0 commit b991904
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libmcount/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ extern uint64_t mcount_threshold; /* nsec */
extern unsigned mcount_minsize;
extern pthread_key_t mtd_key;
extern int shmem_bufsize;
extern int pfd;
extern int mcount_get_pfd(void);
extern int mcount_depth;
extern char *mcount_exename;
extern int page_size_in_kb;
Expand Down
36 changes: 23 additions & 13 deletions libmcount/mcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ pthread_key_t mtd_key = (pthread_key_t)-1;
/* thread local data to trace function execution */
TLS struct mcount_thread_data mtd;

/* pipe file descriptor to communite to uftrace */
int pfd = -1;

/* maximum depth of mcount rstack */
static int mcount_rstack_max = MCOUNT_RSTACK_MAX;

Expand Down Expand Up @@ -518,6 +515,7 @@ static void send_session_msg(struct mcount_thread_data *mtdp, const char *sess_i
},
};
int len = sizeof(msg) + msg.len;
int pfd = mcount_get_pfd();

if (pfd < 0)
return;
Expand All @@ -528,6 +526,8 @@ static void send_session_msg(struct mcount_thread_data *mtdp, const char *sess_i
if (!mcount_should_stop())
pr_err("write tid info failed");
}

close(pfd);
}

static void mcount_trace_finish(bool send_msg)
Expand All @@ -547,11 +547,6 @@ static void mcount_trace_finish(bool send_msg)
if (send_msg)
uftrace_send_message(UFTRACE_MSG_FINISH, NULL, 0);

if (pfd != -1) {
close(pfd);
pfd = -1;
}

trace_finished = true;
pr_dbg("mcount trace finished\n");

Expand Down Expand Up @@ -1802,9 +1797,28 @@ static void mcount_script_init(enum uftrace_pattern_type patt_type)
strv_free(&info.cmds);
}

static __used void mcount_startup(void)
int mcount_get_pfd(void)
{
char *dirname;
char *channel = NULL;
int pfd;

dirname = getenv("UFTRACE_DIR");
if (dirname == NULL)
dirname = UFTRACE_DIR_NAME;

xasprintf(&channel, "%s/%s", dirname, ".channel");
pfd = open(channel, O_WRONLY);
free(channel);

if (pfd >= 0)
lseek(pfd, (off_t)0, SEEK_END);

return pfd;
}

static __used void mcount_startup(void)
{
char *logfd_str;
char *debug_str;
char *bufsize_str;
Expand Down Expand Up @@ -1885,10 +1899,6 @@ static __used void mcount_startup(void)
if (dirname == NULL)
dirname = UFTRACE_DIR_NAME;

xasprintf(&channel, "%s/%s", dirname, ".channel");
pfd = open(channel, O_WRONLY);
free(channel);

if (getenv("UFTRACE_LIST_EVENT")) {
mcount_list_events();
exit(0);
Expand Down
2 changes: 2 additions & 0 deletions libmcount/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void uftrace_send_message(int type, void *data, size_t len)
.iov_len = len,
},
};
int pfd = mcount_get_pfd();

if (pfd < 0)
return;
Expand All @@ -84,6 +85,7 @@ void uftrace_send_message(int type, void *data, size_t len)
if (!mcount_should_stop())
pr_err("writing shmem name to pipe");
}
close(pfd);
}

void build_debug_domain(char *dbg_domain_str)
Expand Down
3 changes: 3 additions & 0 deletions libmcount/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static void send_dlopen_msg(struct mcount_thread_data *mtdp, const char *sess_id
},
};
int len = sizeof(msg) + msg.len;
int pfd = mcount_get_pfd();

if (pfd < 0)
return;
Expand All @@ -72,6 +73,8 @@ static void send_dlopen_msg(struct mcount_thread_data *mtdp, const char *sess_id
if (!mcount_should_stop())
pr_err("write tid info failed");
}

close(pfd);
}

static int dlopen_base_callback(struct dl_phdr_info *info, size_t size, void *arg)
Expand Down

0 comments on commit b991904

Please sign in to comment.