Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script: Support native C/C++ shared object scripting #1205

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
9 changes: 6 additions & 3 deletions cmds/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ static int run_script_for_rstack(struct uftrace_data *handle, struct uftrace_tas

int command_script(int argc, char *argv[], struct uftrace_opts *opts)
{
int ret;
int ret, i;
struct uftrace_data handle;
struct uftrace_task_reader *task;
struct uftrace_script_info info = {
.name = opts->script_file,
.version = UFTRACE_VERSION,
};
char *cmds = NULL;

if (!SCRIPT_ENABLED) {
pr_warn("script command is not supported due to missing libpython2.7.so\n");
Expand Down Expand Up @@ -193,7 +194,9 @@ int command_script(int argc, char *argv[], struct uftrace_opts *opts)

fstack_setup_filters(opts, &handle);

strv_copy(&info.cmds, argc, argv);
for (i = 0; i < argc; i++)
cmds = strjoin(cmds, argv[i], "\n");
info.cmds = cmds;

/* initialize script */
if (script_init(&info, opts->patt_type) < 0) {
Expand All @@ -218,7 +221,7 @@ int command_script(int argc, char *argv[], struct uftrace_opts *opts)

close_data_file(opts, &handle);

strv_free(&info.cmds);
free(cmds);

return ret;
}
4 changes: 1 addition & 3 deletions libmcount/mcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -1754,12 +1754,10 @@ static void mcount_script_init(enum uftrace_pattern_type patt_type)

cmds_str = getenv("UFTRACE_ARGS");
if (cmds_str)
strv_split(&info.cmds, cmds_str, "\n");
info.cmds = cmds_str;

if (script_init(&info, patt_type) < 0)
script_str = NULL;

strv_free(&info.cmds);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion utils/script-luajit.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ static int luajit_uftrace_begin(struct uftrace_script_info *info)
{
int i;
char *s;
struct strv sv = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use STRV_INIT.

0,
};

dllua_getglobal(L, "uftrace_begin");
if (dllua_isnil(L, -1)) {
Expand All @@ -238,11 +241,15 @@ static int luajit_uftrace_begin(struct uftrace_script_info *info)
dllua_settable(L, -3);
dllua_pushstring(L, "cmds");
dllua_newtable(L);
strv_for_each(&info->cmds, s, i) {

if (info->cmds)
strv_split(&sv, info->cmds, "\n");
strv_for_each(&sv, s, i) {
dllua_pushinteger(L, i + 1);
dllua_pushstring(L, s);
dllua_settable(L, -3);
}

dllua_settable(L, -3);
if (dllua_pcall(L, 1, 0, 0) != 0) {
pr_dbg("uftrace_begin failed: %s\n", dllua_tostring(L, -1));
Expand Down
10 changes: 8 additions & 2 deletions utils/script-python.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ int python_uftrace_begin(struct uftrace_script_info *info)
PyObject *ctx;
int i;
char *s;
struct strv sv = {
0,
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.


if (unlikely(!pFuncBegin))
return -1;
Expand All @@ -582,9 +585,12 @@ int python_uftrace_begin(struct uftrace_script_info *info)
insert_dict_bool(dict, "record", info->record);
insert_dict_string(dict, "version", info->version);

cmds = __PyTuple_New(info->cmds.nr);
if (info->cmds)
strv_split(&sv, info->cmds, "\n");

cmds = __PyTuple_New(sv.nr);

strv_for_each(&info->cmds, s, i)
strv_for_each(&sv, s, i)
insert_tuple_string(cmds, i, s);

__PyDict_SetItemString(dict, "cmds", cmds);
Expand Down
17 changes: 13 additions & 4 deletions utils/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,18 @@ static int script_init_for_testing(struct uftrace_script_info *info,
{
int i;
char *name;
struct strv sv = {
0,
};

if (info->cmds)
strv_split(&sv, info->cmds, "\n");

strv_for_each(&info->cmds, name, i)
strv_for_each(&sv, name, i)
script_add_filter(name, ptype);

strv_free(&sv);

return 0;
}

Expand Down Expand Up @@ -193,17 +201,18 @@ static int setup_testing_script(struct uftrace_script_info *info)

fprintf(fp, "# uftrace script testing\n");

strv_append(&info->cmds, "abc");
strv_append(&info->cmds, "x*z");
info->cmds = xstrdup("abc\nx*z");

fclose(fp);
return 0;
}

static int cleanup_testing_script(struct uftrace_script_info *info)
{
char *cmds = (char *)info->cmds;

unlink(SCRIPT_FILE);
strv_free(&info->cmds);
free(cmds);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion utils/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct uftrace_script_info {
char *name;
char *version;
bool record;
struct strv cmds;
const char *cmds;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not expose this to users. Native scripts can have a separate header with your change. Maybe argc + argv[] are more appropriate for native scripts.

};

/* context information passed to script */
Expand Down