1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
* Python script binding for function entry and exit
*
* Copyright (C) 2017-2018, LG Electronics, Honggyu Kim <hong.gyu.kim@lge.com>
*
* Released under the GPL v2.
*/
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include "uftrace.h"
#include "utils/event.h"
#include "utils/filter.h"
#include "utils/fstack.h"
#include "utils/script.h"
#include "utils/symbol.h"
#include "utils/utils.h"
#include "version.h"
#include "libtraceevent/event-parse.h"
static int run_script_for_rstack(struct uftrace_data *handle, struct uftrace_task_reader *task,
struct uftrace_opts *opts)
{
struct uftrace_record *rstack = task->rstack;
struct uftrace_session_link *sessions = &handle->sessions;
struct uftrace_symbol *sym = NULL;
char *symname = NULL;
sym = task_find_sym(sessions, task, rstack);
symname = symbol_getname(sym, rstack->addr);
/* skip it if --no-libcall is given */
if (!opts->libcall && sym && sym->type == ST_PLT_FUNC)
goto out;
task->timestamp_last = task->timestamp;
task->timestamp = rstack->time;
if (rstack->type == UFTRACE_ENTRY) {
struct script_context sc_ctx = {
0,
};
struct uftrace_fstack *fstack;
struct uftrace_trigger tr = {
.flags = 0,
};
int depth;
int ret;
ret = fstack_entry(task, rstack, &tr);
if (ret < 0)
goto out;
/* display depth is set in fstack_entry() */
depth = task->display_depth;
fstack = fstack_get(task, task->stack_count - 1);
fstack_update(UFTRACE_ENTRY, task, fstack);
if (!script_match_filter(symname))
goto out;
sc_ctx.tid = task->tid;
sc_ctx.depth = depth; /* display depth */
sc_ctx.timestamp = rstack->time;
sc_ctx.address = rstack->addr;
sc_ctx.name = symname;
if (tr.flags & TRIGGER_FL_ARGUMENT && opts->show_args) {
sc_ctx.argbuf = task->args.data;
sc_ctx.arglen = task->args.len;
sc_ctx.argspec = task->args.args;
}
/* script hooking for function entry */
script_uftrace_entry(&sc_ctx);
}
else if (rstack->type == UFTRACE_EXIT) {
struct script_context sc_ctx = {
0,
};
struct uftrace_fstack *fstack;
/* function exit */
fstack = fstack_get(task, task->stack_count);
if (fstack_enabled && fstack && !(fstack->flags & FSTACK_FL_NORECORD)) {
int depth = fstack_update(UFTRACE_EXIT, task, fstack);
if (!script_match_filter(symname)) {
fstack_exit(task);
goto out;
}
/* display depth is set before passing rstack */
rstack->depth = depth;
/* setup context for script execution */
sc_ctx.tid = task->tid;
sc_ctx.depth = rstack->depth;
sc_ctx.timestamp = rstack->time;
sc_ctx.duration = fstack->total_time;
sc_ctx.address = rstack->addr;
sc_ctx.name = symname;
if (rstack->more && opts->show_args) {
sc_ctx.argbuf = task->args.data;
sc_ctx.arglen = task->args.len;
sc_ctx.argspec = task->args.args;
}
/* script hooking for function exit */
script_uftrace_exit(&sc_ctx);
}
fstack_exit(task);
}
else if (rstack->type == UFTRACE_EVENT) {
struct script_context sc_ctx = {
.tid = task->tid,
.depth = rstack->depth,
.timestamp = rstack->time,
.address = rstack->addr,
};
sc_ctx.name = event_get_name(handle, rstack->addr);
sc_ctx.argbuf = event_get_data_str(rstack->addr, task->args.data, false);
script_uftrace_event(&sc_ctx);
free(sc_ctx.name);
free(sc_ctx.argbuf);
}
else if (rstack->type == UFTRACE_LOST) {
/* Do nothing as of now */
}
out:
symbol_putname(sym, symname);
return 0;
}
int command_script(int argc, char *argv[], struct uftrace_opts *opts)
{
int ret;
struct uftrace_data handle;
struct uftrace_task_reader *task;
struct script_info info = {
.name = opts->script_file,
.version = UFTRACE_VERSION,
};
if (!SCRIPT_ENABLED) {
pr_warn("script command is not supported due to missing libpython2.7.so\n");
return -1;
}
if (!opts->script_file) {
pr_out("Usage: uftrace script (-S|--script) <script_file>\n");
return -1;
}
if (opts->record) {
char *script_file;
/* parse in-script record option - "uftrace_options" */
parse_script_opt(opts);
script_file = opts->script_file;
opts->script_file = NULL;
pr_dbg("start recording before running a script\n");
ret = command_record(argc, argv, opts);
if (ret < 0) {
pr_warn("cannot record data: %m\n");
return -1;
}
opts->script_file = script_file;
}
__fsetlocking(outfp, FSETLOCKING_BYCALLER);
__fsetlocking(logfp, FSETLOCKING_BYCALLER);
ret = open_data_file(opts, &handle);
if (ret < 0) {
pr_warn("cannot open record data: %s: %m\n", opts->dirname);
return -1;
}
fstack_setup_filters(opts, &handle);
strv_copy(&info.cmds, argc, argv);
/* initialize script */
if (script_init(&info, opts->patt_type) < 0) {
ret = -1;
goto out;
}
while (read_rstack(&handle, &task) == 0 && !uftrace_done) {
if (!fstack_check_opts(task, opts))
continue;
ret = run_script_for_rstack(&handle, task, opts);
if (ret)
break;
}
/* dtor for script support */
script_uftrace_end();
out:
script_finish();
close_data_file(opts, &handle);
strv_free(&info.cmds);
return ret;
}
|