Skip to content

Commit

Permalink
uftrace: Apply dynamic (un)patching at runtime
Browse files Browse the repository at this point in the history
Add support for the '--patch' and '--unpatch' options in the client.
When used, the agent patches or unpatches symbols on the fly.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <[email protected]>
Signed-off-by: Clément Guidi <[email protected]>
  • Loading branch information
clementguidi and gpollo committed Jul 6, 2023
1 parent 06d2cdf commit fe06e08
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cmds/live.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ static int forward_options(struct uftrace_opts *opts)
}

/* provide a pattern type for options that need it */
if (opts->filter || opts->caller || opts->trigger) {
if (opts->filter || opts->caller || opts->trigger || opts->patch) {
status = forward_option(sfd, capabilities, UFTRACE_AGENT_OPT_PATTERN,
&opts->patt_type, sizeof(opts->patt_type));
if (status < 0)
Expand Down Expand Up @@ -389,6 +389,13 @@ static int forward_options(struct uftrace_opts *opts)
goto close;
}

if (opts->patch) {
status = forward_option(sfd, capabilities, UFTRACE_AGENT_OPT_PATCH, opts->patch,
strlen(opts->patch) + 1);
if (status < 0)
goto close;
}

close:
status_close = agent_message_send(sfd, UFTRACE_MSG_AGENT_CLOSE, NULL, 0);
if (status_close == 0) {
Expand Down
7 changes: 6 additions & 1 deletion libmcount/mcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static volatile bool agent_run = false;
#define MCOUNT_AGENT_CAPABILITIES \
(UFTRACE_AGENT_OPT_TRACE | UFTRACE_AGENT_OPT_DEPTH | UFTRACE_AGENT_OPT_THRESHOLD | \
UFTRACE_AGENT_OPT_PATTERN | UFTRACE_AGENT_OPT_FILTER | UFTRACE_AGENT_OPT_CALLER | \
UFTRACE_AGENT_OPT_TRIGGER)
UFTRACE_AGENT_OPT_TRIGGER | UFTRACE_AGENT_OPT_PATCH)

__weak void dynamic_return(void)
{
Expand Down Expand Up @@ -1988,6 +1988,11 @@ static int agent_apply_option(int opt, void *value, size_t size,
agent_setup_trigger(value, triggers);
break;

case UFTRACE_AGENT_OPT_PATCH:
pr_dbg3("apply patch '%s' (size=%d)\n", value, size);
mcount_dynamic_update(&mcount_sym_info, value, mcount_filter_setting.ptype);
break;

default:
ret = -1;
}
Expand Down
1 change: 1 addition & 0 deletions uftrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ enum uftrace_agent_opt {
UFTRACE_AGENT_OPT_FILTER = (1U << 4), /* tracing filters */
UFTRACE_AGENT_OPT_CALLER = (1U << 5), /* tracing caller filters */
UFTRACE_AGENT_OPT_TRIGGER = (1U << 6), /* tracing trigger actions */
UFTRACE_AGENT_OPT_PATCH = (1U << 7), /* patch string */
};

extern struct uftrace_session *first_session;
Expand Down

0 comments on commit fe06e08

Please sign in to comment.