26 #include <linux/module.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
38 #include <asm/setup.h>
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
69 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
74 static int last_ftrace_enabled;
80 struct ftrace_ops *function_trace_op
__read_mostly = &ftrace_list_end;
97 static struct ftrace_ops *ftrace_global_list
__read_mostly = &ftrace_list_end;
98 static struct ftrace_ops *ftrace_control_list
__read_mostly = &ftrace_list_end;
99 static struct ftrace_ops *ftrace_ops_list
__read_mostly = &ftrace_list_end;
100 ftrace_func_t ftrace_trace_function
__read_mostly = ftrace_stub;
101 ftrace_func_t ftrace_pid_function
__read_mostly = ftrace_stub;
102 static struct ftrace_ops global_ops;
103 static struct ftrace_ops control_ops;
105 #if ARCH_SUPPORTS_FTRACE_OPS
110 static void ftrace_ops_no_ops(
unsigned long ip,
unsigned long parent_ip);
111 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
121 struct ftrace_ops *
ops;
126 for (ops = ftrace_ops_list;
127 ops != &ftrace_list_end; ops = ops->next)
145 ftrace_global_list_func(
unsigned long ip,
unsigned long parent_ip,
153 while (op != &ftrace_list_end) {
154 op->func(ip, parent_ip, op, regs);
160 static void ftrace_pid_func(
unsigned long ip,
unsigned long parent_ip,
161 struct ftrace_ops *op,
struct pt_regs *regs)
163 if (!test_tsk_trace_trace(
current))
166 ftrace_pid_function(ip, parent_ip, op, regs);
169 static void set_ftrace_pid_function(ftrace_func_t
func)
172 if (func != ftrace_pid_func)
173 ftrace_pid_function =
func;
184 ftrace_trace_function = ftrace_stub;
185 ftrace_pid_function = ftrace_stub;
188 static void control_ops_disable_all(
struct ftrace_ops *ops)
196 static int control_ops_alloc(
struct ftrace_ops *ops)
204 ops->disabled = disabled;
205 control_ops_disable_all(ops);
209 static void control_ops_free(
struct ftrace_ops *ops)
214 static void update_global_ops(
void)
223 if (ftrace_global_list == &ftrace_list_end ||
224 ftrace_global_list->next == &ftrace_list_end)
225 func = ftrace_global_list->func;
227 func = ftrace_global_list_func;
230 if (!list_empty(&ftrace_pids)) {
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
235 global_ops.func =
func;
238 static void update_ftrace_function(
void)
249 if (ftrace_ops_list == &ftrace_list_end ||
250 (ftrace_ops_list->next == &ftrace_list_end &&
251 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
252 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
255 if (ftrace_ops_list == &global_ops)
256 function_trace_op = ftrace_global_list;
258 function_trace_op = ftrace_ops_list;
259 func = ftrace_ops_list->func;
262 function_trace_op = &ftrace_list_end;
266 ftrace_trace_function =
func;
269 static void add_ftrace_ops(
struct ftrace_ops **
list,
struct ftrace_ops *ops)
281 static int remove_ftrace_ops(
struct ftrace_ops **
list,
struct ftrace_ops *ops)
283 struct ftrace_ops **
p;
289 if (*list == ops && ops->next == &ftrace_list_end) {
290 *list = &ftrace_list_end;
294 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
305 static void add_ftrace_list_ops(
struct ftrace_ops **list,
306 struct ftrace_ops *main_ops,
307 struct ftrace_ops *ops)
309 int first = *list == &ftrace_list_end;
310 add_ftrace_ops(list, ops);
312 add_ftrace_ops(&ftrace_ops_list, main_ops);
315 static int remove_ftrace_list_ops(
struct ftrace_ops **list,
316 struct ftrace_ops *main_ops,
317 struct ftrace_ops *ops)
319 int ret = remove_ftrace_ops(list, ops);
320 if (!ret && *list == &ftrace_list_end)
321 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
325 static int __register_ftrace_function(
struct ftrace_ops *ops)
333 if (
WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
340 #ifndef ARCH_SUPPORTS_FTRACE_SAVE_REGS
346 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
347 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
350 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
351 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
355 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
357 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
358 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
359 ops->flags |= FTRACE_OPS_FL_ENABLED;
360 }
else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
361 if (control_ops_alloc(ops))
363 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
365 add_ftrace_ops(&ftrace_ops_list, ops);
368 update_ftrace_function();
373 static int __unregister_ftrace_function(
struct ftrace_ops *ops)
380 if (
WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
386 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
387 ret = remove_ftrace_list_ops(&ftrace_global_list,
390 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
391 }
else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
392 ret = remove_ftrace_list_ops(&ftrace_control_list,
402 control_ops_free(ops);
405 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
411 update_ftrace_function();
417 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
423 static void ftrace_update_pid_func(
void)
426 if (ftrace_trace_function == ftrace_stub)
429 update_ftrace_function();
432 #ifdef CONFIG_FUNCTION_PROFILER
433 struct ftrace_profile {
437 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
438 unsigned long long time;
439 unsigned long long time_squared;
443 struct ftrace_profile_page {
444 struct ftrace_profile_page *
next;
446 struct ftrace_profile records[];
449 struct ftrace_profile_stat {
452 struct ftrace_profile_page *
pages;
453 struct ftrace_profile_page *
start;
457 #define PROFILE_RECORDS_SIZE \
458 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
460 #define PROFILES_PER_PAGE \
461 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
469 static DEFINE_PER_CPU(
struct ftrace_profile_stat, ftrace_profile_stats);
471 #define FTRACE_PROFILE_HASH_SIZE 1024
474 function_stat_next(
void *
v,
int idx)
476 struct ftrace_profile *rec =
v;
477 struct ftrace_profile_page *
pg;
479 pg = (
struct ftrace_profile_page *)((
unsigned long)rec &
PAGE_MASK);
485 if ((
void *)rec >= (
void *)&pg->records[pg->index]) {
489 rec = &pg->records[0];
499 struct ftrace_profile_stat *
stat =
502 if (!stat || !stat->start)
505 return function_stat_next(&stat->start->records[0], 0);
508 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
510 static int function_stat_cmp(
void *
p1,
void *p2)
512 struct ftrace_profile *
a =
p1;
513 struct ftrace_profile *
b = p2;
515 if (a->time < b->time)
517 if (a->time > b->time)
524 static int function_stat_cmp(
void *p1,
void *p2)
526 struct ftrace_profile *a =
p1;
527 struct ftrace_profile *b = p2;
529 if (a->counter < b->counter)
531 if (a->counter > b->counter)
538 static int function_stat_headers(
struct seq_file *
m)
540 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
544 "--- ---- --- ---\n");
552 static int function_stat_show(
struct seq_file *
m,
void *v)
554 struct ftrace_profile *rec =
v;
557 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
559 unsigned long long avg;
560 unsigned long long stddev;
571 seq_printf(m,
" %-30.30s %10lu", str, rec->counter);
573 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
576 do_div(avg, rec->counter);
579 if (rec->counter <= 1)
582 stddev = rec->time_squared - rec->counter * avg *
avg;
587 do_div(stddev, (rec->counter - 1) * 1000);
605 static void ftrace_profile_reset(
struct ftrace_profile_stat *stat)
607 struct ftrace_profile_page *
pg;
609 pg = stat->pages = stat->start;
612 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
618 FTRACE_PROFILE_HASH_SIZE *
sizeof(
struct hlist_head));
621 int ftrace_profile_pages_init(
struct ftrace_profile_stat *stat)
623 struct ftrace_profile_page *
pg;
636 #ifdef CONFIG_DYNAMIC_FTRACE
637 functions = ftrace_update_tot_cnt;
649 pg = stat->start = stat->pages;
653 for (i = 0; i <
pages; i++) {
665 unsigned long tmp = (
unsigned long)pg;
678 static int ftrace_profile_init_cpu(
int cpu)
680 struct ftrace_profile_stat *
stat;
683 stat = &
per_cpu(ftrace_profile_stats, cpu);
687 ftrace_profile_reset(stat);
695 size = FTRACE_PROFILE_HASH_SIZE;
702 if (!ftrace_profile_bits) {
705 for (;
size; size >>= 1)
706 ftrace_profile_bits++;
710 if (ftrace_profile_pages_init(stat) < 0) {
719 static int ftrace_profile_init(
void)
725 ret = ftrace_profile_init_cpu(cpu);
734 static struct ftrace_profile *
735 ftrace_find_profiled_func(
struct ftrace_profile_stat *stat,
unsigned long ip)
737 struct ftrace_profile *rec;
742 key = hash_long(ip, ftrace_profile_bits);
743 hhd = &stat->hash[
key];
745 if (hlist_empty(hhd))
748 hlist_for_each_entry_rcu(rec, n, hhd,
node) {
756 static void ftrace_add_profile(
struct ftrace_profile_stat *stat,
757 struct ftrace_profile *rec)
761 key = hash_long(rec->ip, ftrace_profile_bits);
762 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
768 static struct ftrace_profile *
769 ftrace_profile_alloc(
struct ftrace_profile_stat *stat,
unsigned long ip)
771 struct ftrace_profile *rec =
NULL;
781 rec = ftrace_find_profiled_func(stat, ip);
785 if (stat->pages->index == PROFILES_PER_PAGE) {
786 if (!stat->pages->next)
788 stat->pages = stat->pages->next;
791 rec = &stat->pages->records[stat->pages->index++];
793 ftrace_add_profile(stat, rec);
802 function_profile_call(
unsigned long ip,
unsigned long parent_ip,
803 struct ftrace_ops *ops,
struct pt_regs *regs)
805 struct ftrace_profile_stat *
stat;
806 struct ftrace_profile *rec;
809 if (!ftrace_profile_enabled)
815 if (!stat->hash || !ftrace_profile_enabled)
818 rec = ftrace_find_profiled_func(stat, ip);
820 rec = ftrace_profile_alloc(stat, ip);
830 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
839 struct ftrace_profile_stat *
stat;
840 unsigned long long calltime;
841 struct ftrace_profile *rec;
846 if (!stat->hash || !ftrace_profile_enabled)
858 index = trace->
depth;
862 current->ret_stack[index - 1].subtime += calltime;
864 if (
current->ret_stack[index].subtime < calltime)
870 rec = ftrace_find_profiled_func(stat, trace->
func);
872 rec->time += calltime;
873 rec->time_squared += calltime * calltime;
880 static int register_ftrace_profiler(
void)
882 return register_ftrace_graph(&profile_graph_return,
883 &profile_graph_entry);
886 static void unregister_ftrace_profiler(
void)
888 unregister_ftrace_graph();
891 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
892 .func = function_profile_call,
893 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
896 static int register_ftrace_profiler(
void)
901 static void unregister_ftrace_profiler(
void)
908 ftrace_profile_write(
struct file *filp,
const char __user *ubuf,
909 size_t cnt, loff_t *ppos)
921 if (ftrace_profile_enabled ^ val) {
923 ret = ftrace_profile_init();
929 ret = register_ftrace_profiler();
934 ftrace_profile_enabled = 1;
936 ftrace_profile_enabled = 0;
941 unregister_ftrace_profiler();
953 ftrace_profile_read(
struct file *filp,
char __user *ubuf,
954 size_t cnt, loff_t *ppos)
959 r =
sprintf(buf,
"%u\n", ftrace_profile_enabled);
965 .read = ftrace_profile_read,
966 .write = ftrace_profile_write,
973 .stat_start = function_stat_start,
974 .stat_next = function_stat_next,
975 .stat_cmp = function_stat_cmp,
976 .stat_headers = function_stat_headers,
977 .stat_show = function_stat_show
980 static __init void ftrace_profile_debugfs(
struct dentry *d_tracer)
982 struct ftrace_profile_stat *
stat;
989 stat = &
per_cpu(ftrace_profile_stats, cpu);
999 "Could not allocate stat file for cpu %d\n",
1003 stat->stat = function_stats;
1004 snprintf(name, 32,
"function%d", cpu);
1005 stat->stat.name =
name;
1009 "Could not register function stat for cpu %d\n",
1017 d_tracer,
NULL, &ftrace_profile_fops);
1020 "'function_profile_enabled' entry\n");
1024 static __init void ftrace_profile_debugfs(
struct dentry *d_tracer)
1031 #ifdef CONFIG_DYNAMIC_FTRACE
1033 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1034 # error Dynamic ftrace depends on MCOUNT_RECORD
1039 struct ftrace_func_probe {
1041 struct ftrace_probe_ops *ops;
1042 unsigned long flags;
1048 struct ftrace_func_entry {
1053 struct ftrace_hash {
1054 unsigned long size_bits;
1056 unsigned long count;
1066 static const struct hlist_head empty_buckets[1];
1067 static const struct ftrace_hash empty_hash = {
1068 .buckets = (
struct hlist_head *)empty_buckets,
1070 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1072 static struct ftrace_ops global_ops = {
1073 .func = ftrace_stub,
1074 .notrace_hash = EMPTY_HASH,
1075 .filter_hash = EMPTY_HASH,
1076 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
1081 struct ftrace_page {
1082 struct ftrace_page *
next;
1083 struct dyn_ftrace *records;
1088 static struct ftrace_page *ftrace_new_pgs;
1090 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1091 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1094 #define NR_TO_INIT 10000
1096 static struct ftrace_page *ftrace_pages_start;
1097 static struct ftrace_page *ftrace_pages;
1099 static bool ftrace_hash_empty(
struct ftrace_hash *
hash)
1101 return !hash || !hash->count;
1104 static struct ftrace_func_entry *
1105 ftrace_lookup_ip(
struct ftrace_hash *
hash,
unsigned long ip)
1108 struct ftrace_func_entry *
entry;
1112 if (ftrace_hash_empty(hash))
1115 if (hash->size_bits > 0)
1116 key = hash_long(ip, hash->size_bits);
1120 hhd = &hash->buckets[
key];
1122 hlist_for_each_entry_rcu(entry, n, hhd,
hlist) {
1123 if (entry->ip == ip)
1129 static void __add_hash_entry(
struct ftrace_hash *hash,
1130 struct ftrace_func_entry *entry)
1135 if (hash->size_bits)
1136 key = hash_long(entry->ip, hash->size_bits);
1140 hhd = &hash->buckets[
key];
1141 hlist_add_head(&entry->hlist, hhd);
1145 static int add_hash_entry(
struct ftrace_hash *hash,
unsigned long ip)
1147 struct ftrace_func_entry *
entry;
1154 __add_hash_entry(hash, entry);
1160 free_hash_entry(
struct ftrace_hash *hash,
1161 struct ftrace_func_entry *entry)
1163 hlist_del(&entry->hlist);
1169 remove_hash_entry(
struct ftrace_hash *hash,
1170 struct ftrace_func_entry *entry)
1172 hlist_del(&entry->hlist);
1176 static void ftrace_hash_clear(
struct ftrace_hash *hash)
1180 struct ftrace_func_entry *
entry;
1181 int size = 1 << hash->size_bits;
1187 for (i = 0; i <
size; i++) {
1188 hhd = &hash->buckets[
i];
1190 free_hash_entry(hash, entry);
1195 static
void free_ftrace_hash(
struct ftrace_hash *hash)
1197 if (!hash || hash == EMPTY_HASH)
1199 ftrace_hash_clear(hash);
1200 kfree(hash->buckets);
1204 static void __free_ftrace_hash_rcu(
struct rcu_head *rcu)
1206 struct ftrace_hash *
hash;
1209 free_ftrace_hash(hash);
1212 static void free_ftrace_hash_rcu(
struct ftrace_hash *hash)
1214 if (!hash || hash == EMPTY_HASH)
1221 free_ftrace_hash(ops->filter_hash);
1222 free_ftrace_hash(ops->notrace_hash);
1225 static struct ftrace_hash *alloc_ftrace_hash(
int size_bits)
1227 struct ftrace_hash *
hash;
1234 size = 1 << size_bits;
1235 hash->buckets = kcalloc(size,
sizeof(*hash->buckets),
GFP_KERNEL);
1237 if (!hash->buckets) {
1242 hash->size_bits = size_bits;
1247 static struct ftrace_hash *
1248 alloc_and_copy_ftrace_hash(
int size_bits,
struct ftrace_hash *hash)
1250 struct ftrace_func_entry *
entry;
1251 struct ftrace_hash *new_hash;
1257 new_hash = alloc_ftrace_hash(size_bits);
1262 if (ftrace_hash_empty(hash))
1265 size = 1 << hash->size_bits;
1266 for (i = 0; i <
size; i++) {
1268 ret = add_hash_entry(new_hash, entry->ip);
1279 free_ftrace_hash(new_hash);
1284 ftrace_hash_rec_disable(
struct ftrace_ops *ops,
int filter_hash);
1286 ftrace_hash_rec_enable(
struct ftrace_ops *ops,
int filter_hash);
1289 ftrace_hash_move(
struct ftrace_ops *ops,
int enable,
1290 struct ftrace_hash **
dst,
struct ftrace_hash *
src)
1292 struct ftrace_func_entry *
entry;
1295 struct ftrace_hash *old_hash;
1296 struct ftrace_hash *new_hash;
1298 int size = src->count;
1307 ftrace_hash_rec_disable(ops, enable);
1314 free_ftrace_hash_rcu(*dst);
1324 for (size /= 2;
size; size >>= 1)
1332 new_hash = alloc_ftrace_hash(bits);
1336 size = 1 << src->size_bits;
1337 for (i = 0; i <
size; i++) {
1338 hhd = &src->buckets[
i];
1341 key = hash_long(entry->ip, bits);
1344 remove_hash_entry(src, entry);
1345 __add_hash_entry(new_hash, entry);
1351 free_ftrace_hash_rcu(old_hash);
1360 ftrace_hash_rec_enable(ops, enable);
1378 ftrace_ops_test(
struct ftrace_ops *ops,
unsigned long ip)
1380 struct ftrace_hash *filter_hash;
1381 struct ftrace_hash *notrace_hash;
1387 if ((ftrace_hash_empty(filter_hash) ||
1388 ftrace_lookup_ip(filter_hash, ip)) &&
1389 (ftrace_hash_empty(notrace_hash) ||
1390 !ftrace_lookup_ip(notrace_hash, ip)))
1402 #define do_for_each_ftrace_rec(pg, rec) \
1403 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1405 for (_____i = 0; _____i < pg->index; _____i++) { \
1406 rec = &pg->records[_____i];
1408 #define while_for_each_ftrace_rec() \
1413 static int ftrace_cmp_recs(
const void *a,
const void *b)
1415 const struct dyn_ftrace *key =
a;
1416 const struct dyn_ftrace *rec =
b;
1418 if (key->flags < rec->ip)
1425 static unsigned long ftrace_location_range(
unsigned long start,
unsigned long end)
1427 struct ftrace_page *
pg;
1428 struct dyn_ftrace *rec;
1429 struct dyn_ftrace key;
1434 for (pg = ftrace_pages_start;
pg; pg = pg->next) {
1435 if (end < pg->records[0].ip ||
1438 rec =
bsearch(&key, pg->records, pg->index,
1439 sizeof(
struct dyn_ftrace),
1457 unsigned long ftrace_location(
unsigned long ip)
1459 return ftrace_location_range(ip, ip);
1472 int ftrace_text_reserved(
void *start,
void *end)
1476 ret = ftrace_location_range((
unsigned long)start,
1477 (
unsigned long)end);
1482 static void __ftrace_hash_rec_update(
struct ftrace_ops *ops,
1486 struct ftrace_hash *
hash;
1487 struct ftrace_hash *other_hash;
1488 struct ftrace_page *
pg;
1489 struct dyn_ftrace *rec;
1494 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1509 hash = ops->filter_hash;
1510 other_hash = ops->notrace_hash;
1511 if (ftrace_hash_empty(hash))
1515 hash = ops->notrace_hash;
1516 other_hash = ops->filter_hash;
1521 if (ftrace_hash_empty(hash))
1525 do_for_each_ftrace_rec(pg, rec) {
1526 int in_other_hash = 0;
1535 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1538 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1539 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1544 if (filter_hash && in_hash && !in_other_hash)
1546 else if (!filter_hash && in_hash &&
1547 (in_other_hash || ftrace_hash_empty(other_hash)))
1555 if (
FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1561 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1562 rec->flags |= FTRACE_FL_REGS;
1570 if (!all && count == hash->count)
1572 } while_for_each_ftrace_rec();
1575 static void ftrace_hash_rec_disable(
struct ftrace_ops *ops,
1578 __ftrace_hash_rec_update(ops, filter_hash, 0);
1581 static void ftrace_hash_rec_enable(
struct ftrace_ops *ops,
1584 __ftrace_hash_rec_update(ops, filter_hash, 1);
1587 static void print_ip_ins(
const char *
fmt,
unsigned char *p)
1609 void ftrace_bug(
int failed,
unsigned long ip)
1614 pr_info(
"ftrace faulted on modifying ");
1619 pr_info(
"ftrace failed to modify ");
1621 print_ip_ins(
" actual: ", (
unsigned char *)ip);
1626 pr_info(
"ftrace faulted on writing ");
1631 pr_info(
"ftrace faulted on unknown error ");
1636 static int ftrace_check_record(
struct dyn_ftrace *rec,
int enable,
int update)
1638 unsigned long flag = 0
UL;
1651 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1652 flag = FTRACE_FL_ENABLED;
1660 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1661 flag |= FTRACE_FL_REGS;
1664 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1665 return FTRACE_UPDATE_IGNORE;
1669 flag ^= rec->flags & FTRACE_FL_ENABLED;
1672 rec->flags |= FTRACE_FL_ENABLED;
1673 if (flag & FTRACE_FL_REGS) {
1674 if (rec->flags & FTRACE_FL_REGS)
1675 rec->flags |= FTRACE_FL_REGS_EN;
1677 rec->flags &= ~FTRACE_FL_REGS_EN;
1691 if (flag & FTRACE_FL_ENABLED)
1692 return FTRACE_UPDATE_MAKE_CALL;
1693 else if (rec->flags & FTRACE_FL_REGS_EN)
1694 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1696 return FTRACE_UPDATE_MODIFY_CALL;
1701 if (!(rec->flags & ~FTRACE_FL_MASK))
1705 rec->flags &= ~FTRACE_FL_ENABLED;
1708 return FTRACE_UPDATE_MAKE_NOP;
1719 int ftrace_update_record(
struct dyn_ftrace *rec,
int enable)
1721 return ftrace_check_record(rec, enable, 1);
1733 int ftrace_test_record(
struct dyn_ftrace *rec,
int enable)
1735 return ftrace_check_record(rec, enable, 0);
1739 __ftrace_replace_code(
struct dyn_ftrace *rec,
int enable)
1741 unsigned long ftrace_old_addr;
1742 unsigned long ftrace_addr;
1745 ret = ftrace_update_record(rec, enable);
1747 if (rec->flags & FTRACE_FL_REGS)
1748 ftrace_addr = (
unsigned long)FTRACE_REGS_ADDR;
1750 ftrace_addr = (
unsigned long)FTRACE_ADDR;
1753 case FTRACE_UPDATE_IGNORE:
1756 case FTRACE_UPDATE_MAKE_CALL:
1759 case FTRACE_UPDATE_MAKE_NOP:
1762 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1763 case FTRACE_UPDATE_MODIFY_CALL:
1764 if (rec->flags & FTRACE_FL_REGS)
1765 ftrace_old_addr = (
unsigned long)FTRACE_ADDR;
1767 ftrace_old_addr = (
unsigned long)FTRACE_REGS_ADDR;
1769 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1775 void __weak ftrace_replace_code(
int enable)
1777 struct dyn_ftrace *rec;
1778 struct ftrace_page *
pg;
1784 do_for_each_ftrace_rec(pg, rec) {
1785 failed = __ftrace_replace_code(rec, enable);
1787 ftrace_bug(failed, rec->ip);
1791 } while_for_each_ftrace_rec();
1794 struct ftrace_rec_iter {
1795 struct ftrace_page *
pg;
1808 struct ftrace_rec_iter *ftrace_rec_iter_start(
void)
1814 static struct ftrace_rec_iter ftrace_rec_iter;
1815 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1817 iter->pg = ftrace_pages_start;
1821 while (iter->pg && !iter->pg->index)
1822 iter->pg = iter->pg->next;
1836 struct ftrace_rec_iter *ftrace_rec_iter_next(
struct ftrace_rec_iter *iter)
1840 if (iter->index >= iter->pg->index) {
1841 iter->pg = iter->pg->next;
1845 while (iter->pg && !iter->pg->index)
1846 iter->pg = iter->pg->next;
1861 struct dyn_ftrace *ftrace_rec_iter_record(
struct ftrace_rec_iter *iter)
1863 return &iter->pg->records[iter->index];
1867 ftrace_code_disable(
struct module *
mod,
struct dyn_ftrace *rec)
1879 ftrace_bug(ret, ip);
1889 int __weak ftrace_arch_code_modify_prepare(
void)
1898 int __weak ftrace_arch_code_modify_post_process(
void)
1903 void ftrace_modify_all_code(
int command)
1905 if (command & FTRACE_UPDATE_CALLS)
1906 ftrace_replace_code(1);
1907 else if (command & FTRACE_DISABLE_CALLS)
1908 ftrace_replace_code(0);
1910 if (command & FTRACE_UPDATE_TRACE_FUNC)
1913 if (command & FTRACE_START_FUNC_RET)
1914 ftrace_enable_ftrace_graph_caller();
1915 else if (command & FTRACE_STOP_FUNC_RET)
1916 ftrace_disable_ftrace_graph_caller();
1919 static int __ftrace_modify_code(
void *
data)
1921 int *command =
data;
1923 ftrace_modify_all_code(*command);
1935 void ftrace_run_stop_machine(
int command)
1937 stop_machine(__ftrace_modify_code, &command,
NULL);
1947 void __weak arch_ftrace_update_code(
int command)
1949 ftrace_run_stop_machine(command);
1952 static void ftrace_run_update_code(
int command)
1956 ret = ftrace_arch_code_modify_prepare();
1964 function_trace_stop++;
1972 arch_ftrace_update_code(command);
1974 function_trace_stop--;
1976 ret = ftrace_arch_code_modify_post_process();
1980 static ftrace_func_t saved_ftrace_func;
1981 static int ftrace_start_up;
1982 static int global_start_up;
1984 static void ftrace_startup_enable(
int command)
1986 if (saved_ftrace_func != ftrace_trace_function) {
1987 saved_ftrace_func = ftrace_trace_function;
1988 command |= FTRACE_UPDATE_TRACE_FUNC;
1991 if (!command || !ftrace_enabled)
1994 ftrace_run_update_code(command);
1999 bool hash_enable =
true;
2005 command |= FTRACE_UPDATE_CALLS;
2008 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2011 if (global_start_up)
2012 hash_enable =
false;
2016 ops->
flags |= FTRACE_OPS_FL_ENABLED;
2018 ftrace_hash_rec_enable(ops, 1);
2020 ftrace_startup_enable(command);
2027 bool hash_disable =
true;
2040 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2045 if (global_start_up) {
2047 hash_disable =
false;
2052 ftrace_hash_rec_disable(ops, 1);
2054 if (ops != &global_ops || !global_start_up)
2055 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2057 command |= FTRACE_UPDATE_CALLS;
2059 if (saved_ftrace_func != ftrace_trace_function) {
2060 saved_ftrace_func = ftrace_trace_function;
2061 command |= FTRACE_UPDATE_TRACE_FUNC;
2064 if (!command || !ftrace_enabled)
2067 ftrace_run_update_code(command);
2076 saved_ftrace_func =
NULL;
2078 if (ftrace_start_up)
2079 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2088 if (ftrace_start_up)
2089 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2092 static cycle_t ftrace_update_time;
2093 static unsigned long ftrace_update_cnt;
2094 unsigned long ftrace_update_tot_cnt;
2096 static int ops_traces_mod(
struct ftrace_ops *ops)
2098 struct ftrace_hash *
hash;
2100 hash = ops->filter_hash;
2101 return ftrace_hash_empty(hash);
2104 static int ftrace_update_code(
struct module *mod)
2106 struct ftrace_page *
pg;
2107 struct dyn_ftrace *
p;
2109 unsigned long ref = 0;
2119 struct ftrace_ops *ops;
2121 for (ops = ftrace_ops_list;
2122 ops != &ftrace_list_end; ops = ops->next) {
2123 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2124 ops_traces_mod(ops))
2130 ftrace_update_cnt = 0;
2132 for (pg = ftrace_new_pgs;
pg; pg = pg->next) {
2134 for (i = 0; i < pg->index; i++) {
2139 p = &pg->records[
i];
2146 if (!ftrace_code_disable(mod, p))
2149 ftrace_update_cnt++;
2160 if (ftrace_start_up && ref) {
2161 int failed = __ftrace_replace_code(p, 1);
2163 ftrace_bug(failed, p->ip);
2168 ftrace_new_pgs =
NULL;
2171 ftrace_update_time = stop -
start;
2172 ftrace_update_tot_cnt += ftrace_update_cnt;
2177 static int ftrace_allocate_records(
struct ftrace_page *pg,
int count)
2214 static struct ftrace_page *
2215 ftrace_allocate_pages(
unsigned long num_to_init)
2217 struct ftrace_page *start_pg;
2218 struct ftrace_page *
pg;
2225 start_pg = pg = kzalloc(
sizeof(*pg),
GFP_KERNEL);
2235 cnt = ftrace_allocate_records(pg, num_to_init);
2255 free_pages((
unsigned long)pg->records, order);
2256 start_pg = pg->next;
2260 pr_info(
"ftrace: FAILED to allocate memory for functions\n");
2264 static int __init ftrace_dyn_table_alloc(
unsigned long num_to_init)
2269 pr_info(
"ftrace: No functions to be traced?\n");
2274 pr_info(
"ftrace: allocating %ld entries in %d pages\n",
2275 num_to_init, cnt + 1);
2280 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4)
2282 struct ftrace_iterator {
2285 struct ftrace_page *
pg;
2286 struct dyn_ftrace *
func;
2287 struct ftrace_func_probe *probe;
2289 struct ftrace_hash *
hash;
2290 struct ftrace_ops *ops;
2299 struct ftrace_iterator *iter = m->
private;
2307 hnd = &iter->probe->node;
2312 hhd = &ftrace_func_hash[iter->hidx];
2314 if (hlist_empty(hhd)) {
2338 static void *t_hash_start(
struct seq_file *m, loff_t *pos)
2340 struct ftrace_iterator *iter = m->
private;
2344 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2347 if (iter->func_pos > *pos)
2351 for (l = 0; l <= (*pos - iter->func_pos); ) {
2352 p = t_hash_next(m, &l);
2360 iter->flags |= FTRACE_ITER_HASH;
2366 t_hash_show(
struct seq_file *m,
struct ftrace_iterator *iter)
2368 struct ftrace_func_probe *rec;
2374 if (rec->ops->print)
2375 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2377 seq_printf(m,
"%ps:%ps", (
void *)rec->ip, (
void *)rec->ops->func);
2387 t_next(
struct seq_file *m,
void *v, loff_t *pos)
2389 struct ftrace_iterator *iter = m->
private;
2390 struct ftrace_ops *ops = iter->ops;
2391 struct dyn_ftrace *rec =
NULL;
2396 if (iter->flags & FTRACE_ITER_HASH)
2397 return t_hash_next(m, pos);
2400 iter->pos = iter->func_pos = *
pos;
2402 if (iter->flags & FTRACE_ITER_PRINTALL)
2403 return t_hash_start(m, pos);
2406 if (iter->idx >= iter->pg->index) {
2407 if (iter->pg->next) {
2408 iter->pg = iter->pg->next;
2413 rec = &iter->pg->records[iter->idx++];
2414 if (((iter->flags & FTRACE_ITER_FILTER) &&
2415 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2417 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2418 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2420 ((iter->flags & FTRACE_ITER_ENABLED) &&
2421 !(rec->flags & ~FTRACE_FL_MASK))) {
2429 return t_hash_start(m, pos);
2436 static void reset_iter_read(
struct ftrace_iterator *iter)
2440 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2443 static void *t_start(
struct seq_file *m, loff_t *pos)
2445 struct ftrace_iterator *iter = m->
private;
2446 struct ftrace_ops *ops = iter->ops;
2458 if (*pos < iter->pos)
2459 reset_iter_read(iter);
2466 if (iter->flags & FTRACE_ITER_FILTER &&
2467 ftrace_hash_empty(ops->filter_hash)) {
2469 return t_hash_start(m, pos);
2470 iter->flags |= FTRACE_ITER_PRINTALL;
2472 iter->flags &= ~FTRACE_ITER_HASH;
2476 if (iter->flags & FTRACE_ITER_HASH)
2477 return t_hash_start(m, pos);
2484 iter->pg = ftrace_pages_start;
2486 for (l = 0; l <= *
pos; ) {
2487 p = t_next(m, p, &l);
2493 return t_hash_start(m, pos);
2498 static void t_stop(
struct seq_file *m,
void *p)
2503 static int t_show(
struct seq_file *m,
void *v)
2505 struct ftrace_iterator *iter = m->
private;
2506 struct dyn_ftrace *rec;
2508 if (iter->flags & FTRACE_ITER_HASH)
2509 return t_hash_show(m, iter);
2511 if (iter->flags & FTRACE_ITER_PRINTALL) {
2512 seq_printf(m,
"#### all functions enabled ####\n");
2522 if (iter->flags & FTRACE_ITER_ENABLED)
2524 rec->flags & ~FTRACE_FL_MASK,
2525 rec->flags & FTRACE_FL_REGS ?
" R" :
"");
2541 struct ftrace_iterator *iter;
2548 iter->pg = ftrace_pages_start;
2549 iter->ops = &global_ops;
2552 return iter ? 0 : -
ENOMEM;
2556 ftrace_enabled_open(
struct inode *inode,
struct file *file)
2558 struct ftrace_iterator *iter;
2565 iter->pg = ftrace_pages_start;
2566 iter->flags = FTRACE_ITER_ENABLED;
2567 iter->ops = &global_ops;
2570 return iter ? 0 : -
ENOMEM;
2573 static void ftrace_filter_reset(
struct ftrace_hash *hash)
2576 ftrace_hash_clear(hash);
2598 struct inode *inode,
struct file *file)
2600 struct ftrace_iterator *iter;
2601 struct ftrace_hash *
hash;
2616 if (flag & FTRACE_ITER_NOTRACE)
2617 hash = ops->notrace_hash;
2619 hash = ops->filter_hash;
2640 ftrace_filter_reset(iter->hash);
2643 iter->pg = ftrace_pages_start;
2645 ret =
seq_open(file, &show_ftrace_seq_ops);
2651 free_ftrace_hash(iter->hash);
2663 ftrace_filter_open(
struct inode *inode,
struct file *file)
2666 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2671 ftrace_notrace_open(
struct inode *inode,
struct file *file)
2678 ftrace_regex_lseek(
struct file *file, loff_t
offset,
int origin)
2685 file->
f_pos = ret = 1;
2690 static int ftrace_match(
char *str,
char *
regex,
int len,
int type)
2697 if (
strcmp(str, regex) == 0)
2701 if (
strncmp(str, regex, len) == 0)
2710 if (slen >= len &&
memcmp(str + slen - len, regex, len) == 0)
2719 enter_record(
struct ftrace_hash *hash,
struct dyn_ftrace *rec,
int not)
2721 struct ftrace_func_entry *
entry;
2724 entry = ftrace_lookup_ip(hash, rec->ip);
2730 free_hash_entry(hash, entry);
2736 ret = add_hash_entry(hash, rec->ip);
2742 ftrace_match_record(
struct dyn_ftrace *rec,
char *mod,
2743 char *regex,
int len,
int type)
2752 if (!modname ||
strcmp(modname, mod))
2760 return ftrace_match(str, regex, len, type);
2764 match_records(
struct ftrace_hash *hash,
char *buff,
2765 int len,
char *mod,
int not)
2767 unsigned search_len = 0;
2768 struct ftrace_page *
pg;
2769 struct dyn_ftrace *rec;
2777 search_len =
strlen(search);
2785 do_for_each_ftrace_rec(pg, rec) {
2786 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2787 ret = enter_record(hash, rec, not);
2794 } while_for_each_ftrace_rec();
2802 ftrace_match_records(
struct ftrace_hash *hash,
char *buff,
int len)
2804 return match_records(hash, buff, len,
NULL, 0);
2808 ftrace_match_module_records(
struct ftrace_hash *hash,
char *buff,
char *mod)
2813 if (
strcmp(buff,
"*") == 0)
2817 if (
strcmp(buff,
"!") == 0 ||
strcmp(buff,
"!*") == 0) {
2822 return match_records(hash, buff,
strlen(buff), mod, not);
2831 ftrace_mod_callback(
struct ftrace_hash *hash,
2832 char *func,
char *
cmd,
char *
param,
int enable)
2849 mod =
strsep(¶m,
":");
2853 ret = ftrace_match_module_records(hash, func, mod);
2864 .func = ftrace_mod_callback,
2867 static int __init ftrace_mod_cmd_init(
void)
2869 return register_ftrace_command(&ftrace_mod_cmd);
2873 static void function_trace_probe_call(
unsigned long ip,
unsigned long parent_ip,
2876 struct ftrace_func_probe *
entry;
2883 hhd = &ftrace_func_hash[
key];
2885 if (hlist_empty(hhd))
2894 hlist_for_each_entry_rcu(entry, n, hhd,
node) {
2895 if (entry->ip == ip)
2896 entry->ops->func(ip, parent_ip, &entry->data);
2901 static struct ftrace_ops trace_probe_ops __read_mostly =
2903 .func = function_trace_probe_call,
2906 static int ftrace_probe_registered;
2908 static void __enable_ftrace_function_probe(
void)
2913 if (ftrace_probe_registered)
2922 if (i == FTRACE_FUNC_HASHSIZE)
2925 ret = __register_ftrace_function(&trace_probe_ops);
2929 ftrace_probe_registered = 1;
2932 static void __disable_ftrace_function_probe(
void)
2937 if (!ftrace_probe_registered)
2947 ret = __unregister_ftrace_function(&trace_probe_ops);
2951 ftrace_probe_registered = 0;
2955 static void ftrace_free_entry_rcu(
struct rcu_head *rhp)
2957 struct ftrace_func_probe *entry =
2960 if (entry->ops->free)
2961 entry->ops->free(&entry->data);
2967 register_ftrace_function_probe(
char *glob,
struct ftrace_probe_ops *ops,
2970 struct ftrace_func_probe *
entry;
2971 struct ftrace_page *
pg;
2972 struct dyn_ftrace *rec;
2990 do_for_each_ftrace_rec(pg, rec) {
2992 if (!ftrace_match_record(rec,
NULL, search, len, type))
3012 if (ops->callback) {
3013 if (ops->callback(rec->ip, &entry->data) < 0) {
3021 entry->ip = rec->ip;
3024 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3026 } while_for_each_ftrace_rec();
3027 __enable_ftrace_function_probe();
3036 PROBE_TEST_FUNC = 1,
3041 __unregister_ftrace_function_probe(
char *glob,
struct ftrace_probe_ops *ops,
3042 void *data,
int flags)
3044 struct ftrace_func_probe *
entry;
3071 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3074 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3081 if (!ftrace_match(str, glob, len, type))
3085 hlist_del(&entry->node);
3086 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3089 __disable_ftrace_function_probe();
3094 unregister_ftrace_function_probe(
char *glob,
struct ftrace_probe_ops *ops,
3097 __unregister_ftrace_function_probe(glob, ops, data,
3098 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3102 unregister_ftrace_function_probe_func(
char *glob,
struct ftrace_probe_ops *ops)
3104 __unregister_ftrace_function_probe(glob, ops,
NULL, PROBE_TEST_FUNC);
3107 void unregister_ftrace_function_probe_all(
char *glob)
3109 __unregister_ftrace_function_probe(glob,
NULL,
NULL, 0);
3127 list_add(&cmd->
list, &ftrace_commands);
3143 list_del_init(&p->
list);
3153 static int ftrace_process_regex(
struct ftrace_hash *hash,
3154 char *buff,
int len,
int enable)
3160 func =
strsep(&next,
":");
3163 ret = ftrace_match_records(hash, func, len);
3173 command =
strsep(&next,
":");
3178 ret = p->
func(hash, func, command, next, enable);
3189 ftrace_regex_write(
struct file *file,
const char __user *ubuf,
3190 size_t cnt, loff_t *ppos,
int enable)
3192 struct ftrace_iterator *iter;
3211 parser = &iter->parser;
3214 if (read >= 0 && trace_parser_loaded(parser) &&
3215 !trace_parser_cont(parser)) {
3216 ret = ftrace_process_regex(iter->hash, parser->
buffer,
3217 parser->
idx, enable);
3218 trace_parser_clear(parser);
3231 ftrace_filter_write(
struct file *file,
const char __user *ubuf,
3232 size_t cnt, loff_t *ppos)
3234 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3238 ftrace_notrace_write(
struct file *file,
const char __user *ubuf,
3239 size_t cnt, loff_t *ppos)
3241 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3245 ftrace_match_addr(
struct ftrace_hash *hash,
unsigned long ip,
int remove)
3247 struct ftrace_func_entry *
entry;
3249 if (!ftrace_location(ip))
3253 entry = ftrace_lookup_ip(hash, ip);
3256 free_hash_entry(hash, entry);
3260 return add_hash_entry(hash, ip);
3264 ftrace_set_hash(
struct ftrace_ops *ops,
unsigned char *
buf,
int len,
3265 unsigned long ip,
int remove,
int reset,
int enable)
3267 struct ftrace_hash **orig_hash;
3268 struct ftrace_hash *
hash;
3272 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3279 orig_hash = &ops->filter_hash;
3281 orig_hash = &ops->notrace_hash;
3289 ftrace_filter_reset(hash);
3290 if (buf && !ftrace_match_records(hash, buf, len)) {
3292 goto out_regex_unlock;
3295 ret = ftrace_match_addr(hash, ip,
remove);
3297 goto out_regex_unlock;
3301 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3302 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3304 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3311 free_ftrace_hash(hash);
3316 ftrace_set_addr(
struct ftrace_ops *ops,
unsigned long ip,
int remove,
3317 int reset,
int enable)
3319 return ftrace_set_hash(ops, 0, 0, ip,
remove, reset, enable);
3333 int remove,
int reset)
3335 return ftrace_set_addr(ops, ip,
remove, reset, 1);
3340 ftrace_set_regex(
struct ftrace_ops *ops,
unsigned char *buf,
int len,
3341 int reset,
int enable)
3343 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3359 return ftrace_set_regex(ops, buf, len, reset, 1);
3377 return ftrace_set_regex(ops, buf, len, reset, 0);
3390 void ftrace_set_global_filter(
unsigned char *buf,
int len,
int reset)
3392 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3407 void ftrace_set_global_notrace(
unsigned char *buf,
int len,
int reset)
3409 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3416 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3417 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE]
__initdata;
3418 static char ftrace_filter_buf[FTRACE_FILTER_SIZE]
__initdata;
3420 static int __init set_ftrace_notrace(
char *str)
3422 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3425 __setup(
"ftrace_notrace=", set_ftrace_notrace);
3427 static int __init set_ftrace_filter(
char *str)
3429 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3432 __setup(
"ftrace_filter=", set_ftrace_filter);
3434 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3435 static char ftrace_graph_buf[FTRACE_FILTER_SIZE]
__initdata;
3436 static int ftrace_set_func(
unsigned long *array,
int *idx,
char *
buffer);
3438 static int __init set_graph_function(
char *str)
3440 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3443 __setup(
"ftrace_graph_filter=", set_graph_function);
3445 static void __init set_ftrace_early_graph(
char *buf)
3451 func =
strsep(&buf,
",");
3453 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3457 "traceable\n", func);
3468 func =
strsep(&buf,
",");
3469 ftrace_set_regex(ops, func,
strlen(func), 0, enable);
3473 static void __init set_ftrace_early_filters(
void)
3475 if (ftrace_filter_buf[0])
3477 if (ftrace_notrace_buf[0])
3479 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3480 if (ftrace_graph_buf[0])
3481 set_ftrace_early_graph(ftrace_graph_buf);
3485 int ftrace_regex_release(
struct inode *inode,
struct file *file)
3488 struct ftrace_iterator *iter;
3489 struct ftrace_hash **orig_hash;
3502 parser = &iter->parser;
3503 if (trace_parser_loaded(parser)) {
3505 ftrace_match_records(iter->hash, parser->
buffer, parser->
idx);
3511 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3514 orig_hash = &iter->ops->filter_hash;
3516 orig_hash = &iter->ops->notrace_hash;
3519 ret = ftrace_hash_move(iter->ops, filter_hash,
3520 orig_hash, iter->hash);
3521 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3523 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3527 free_ftrace_hash(iter->hash);
3535 .
open = ftrace_avail_open,
3542 .
open = ftrace_enabled_open,
3549 .
open = ftrace_filter_open,
3551 .write = ftrace_filter_write,
3552 .llseek = ftrace_regex_lseek,
3553 .release = ftrace_regex_release,
3557 .
open = ftrace_notrace_open,
3559 .write = ftrace_notrace_write,
3560 .llseek = ftrace_regex_lseek,
3561 .release = ftrace_regex_release,
3564 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3568 int ftrace_graph_count;
3569 int ftrace_graph_filter_enabled;
3570 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS]
__read_mostly;
3573 __g_next(
struct seq_file *m, loff_t *pos)
3575 if (*pos >= ftrace_graph_count)
3577 return &ftrace_graph_funcs[*
pos];
3581 g_next(
struct seq_file *m,
void *v, loff_t *pos)
3584 return __g_next(m, pos);
3587 static void *g_start(
struct seq_file *m, loff_t *pos)
3592 if (!ftrace_graph_filter_enabled && !*pos)
3595 return __g_next(m, pos);
3598 static void g_stop(
struct seq_file *m,
void *p)
3603 static int g_show(
struct seq_file *m,
void *v)
3605 unsigned long *
ptr =
v;
3610 if (ptr == (
unsigned long *)1) {
3611 seq_printf(m,
"#### all functions enabled ####\n");
3628 ftrace_graph_open(
struct inode *inode,
struct file *file)
3638 ftrace_graph_filter_enabled = 0;
3639 ftrace_graph_count = 0;
3640 memset(ftrace_graph_funcs, 0,
sizeof(ftrace_graph_funcs));
3645 ret =
seq_open(file, &ftrace_graph_seq_ops);
3651 ftrace_graph_release(
struct inode *inode,
struct file *file)
3659 ftrace_set_func(
unsigned long *array,
int *idx,
char *
buffer)
3661 struct dyn_ftrace *rec;
3662 struct ftrace_page *
pg;
3672 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3675 search_len =
strlen(search);
3684 do_for_each_ftrace_rec(pg, rec) {
3686 if (ftrace_match_record(rec,
NULL, search, search_len, type)) {
3689 for (i = 0; i < *
idx; i++) {
3690 if (array[i] == rec->ip) {
3699 array[(*idx)++] = rec->ip;
3700 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3705 array[
i] = array[--(*idx)];
3711 } while_for_each_ftrace_rec();
3718 ftrace_graph_filter_enabled = 1;
3723 ftrace_graph_write(
struct file *file,
const char __user *ubuf,
3724 size_t cnt, loff_t *ppos)
3741 if (read >= 0 && trace_parser_loaded((&parser))) {
3745 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3762 .
open = ftrace_graph_open,
3764 .write = ftrace_graph_write,
3765 .release = ftrace_graph_release,
3770 static __init int ftrace_init_dyn_debugfs(
struct dentry *d_tracer)
3774 d_tracer,
NULL, &ftrace_avail_fops);
3777 d_tracer,
NULL, &ftrace_enabled_fops);
3780 NULL, &ftrace_filter_fops);
3783 NULL, &ftrace_notrace_fops);
3785 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3788 &ftrace_graph_fops);
3794 static int ftrace_cmp_ips(
const void *a,
const void *b)
3796 const unsigned long *
ipa =
a;
3797 const unsigned long *
ipb =
b;
3806 static void ftrace_swap_ips(
void *a,
void *b,
int size)
3808 unsigned long *ipa =
a;
3809 unsigned long *ipb =
b;
3817 static int ftrace_process_locs(
struct module *mod,
3818 unsigned long *start,
3821 struct ftrace_page *start_pg;
3822 struct ftrace_page *
pg;
3823 struct dyn_ftrace *rec;
3824 unsigned long count;
3827 unsigned long flags = 0;
3830 count = end -
start;
3835 sort(start, count,
sizeof(*start),
3836 ftrace_cmp_ips, ftrace_swap_ips);
3838 start_pg = ftrace_allocate_pages(count);
3850 WARN_ON(ftrace_pages || ftrace_pages_start);
3852 ftrace_pages = ftrace_pages_start = start_pg;
3857 if (
WARN_ON(ftrace_pages->next)) {
3859 while (ftrace_pages->next)
3860 ftrace_pages = ftrace_pages->next;
3863 ftrace_pages->next = start_pg;
3869 addr = ftrace_call_adjust(*p++);
3879 if (pg->index == pg->size) {
3886 rec = &pg->records[pg->index++];
3897 ftrace_new_pgs = start_pg;
3909 ftrace_update_code(mod);
3919 #ifdef CONFIG_MODULES
3921 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3923 void ftrace_release_mod(
struct module *mod)
3925 struct dyn_ftrace *rec;
3926 struct ftrace_page **last_pg;
3927 struct ftrace_page *
pg;
3932 if (ftrace_disabled)
3939 last_pg = &ftrace_pages_start;
3940 for (pg = ftrace_pages_start;
pg; pg = *last_pg) {
3941 rec = &pg->records[0];
3942 if (within_module_core(rec->ip, mod)) {
3947 if (
WARN_ON(pg == ftrace_pages_start))
3951 if (pg == ftrace_pages)
3952 ftrace_pages = next_to_ftrace_page(last_pg);
3954 *last_pg = pg->next;
3956 free_pages((
unsigned long)pg->records, order);
3959 last_pg = &pg->next;
3965 static void ftrace_init_module(
struct module *mod,
3966 unsigned long *start,
unsigned long *end)
3968 if (ftrace_disabled || start == end)
3970 ftrace_process_locs(mod, start, end);
3974 unsigned long val,
void *data)
3979 case MODULE_STATE_COMING:
3980 ftrace_init_module(mod, mod->ftrace_callsites,
3981 mod->ftrace_callsites +
3982 mod->num_ftrace_callsites);
3984 case MODULE_STATE_GOING:
3985 ftrace_release_mod(mod);
3993 unsigned long val,
void *data)
4004 extern unsigned long __start_mcount_loc[];
4005 extern unsigned long __stop_mcount_loc[];
4007 void __init ftrace_init(
void)
4013 addr = (
unsigned long)ftrace_stub;
4023 count = __stop_mcount_loc - __start_mcount_loc;
4025 ret = ftrace_dyn_table_alloc(count);
4029 last_ftrace_enabled = ftrace_enabled = 1;
4031 ret = ftrace_process_locs(
NULL,
4037 pr_warning(
"Failed to register trace ftrace module notifier\n");
4039 set_ftrace_early_filters();
4043 ftrace_disabled = 1;
4048 static struct ftrace_ops global_ops = {
4049 .func = ftrace_stub,
4050 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4053 static int __init ftrace_nodyn_init(
void)
4060 static inline int ftrace_init_dyn_debugfs(
struct dentry *d_tracer) {
return 0; }
4061 static inline void ftrace_startup_enable(
int command) { }
4063 # define ftrace_startup(ops, command) \
4065 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4068 # define ftrace_shutdown(ops, command) do { } while (0)
4069 # define ftrace_startup_sysctl() do { } while (0)
4070 # define ftrace_shutdown_sysctl() do { } while (0)
4073 ftrace_ops_test(
struct ftrace_ops *ops,
unsigned long ip)
4081 ftrace_ops_control_func(
unsigned long ip,
unsigned long parent_ip,
4082 struct ftrace_ops *op,
struct pt_regs *regs)
4094 while (op != &ftrace_list_end) {
4095 if (!ftrace_function_local_disabled(op) &&
4096 ftrace_ops_test(op, ip))
4097 op->func(ip, parent_ip, op, regs);
4105 static struct ftrace_ops control_ops = {
4106 .func = ftrace_ops_control_func,
4107 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4111 __ftrace_ops_list_func(
unsigned long ip,
unsigned long parent_ip,
4112 struct ftrace_ops *
ignored,
struct pt_regs *regs)
4114 struct ftrace_ops *
op;
4116 if (function_trace_stop)
4129 while (op != &ftrace_list_end) {
4130 if (ftrace_ops_test(op, ip))
4131 op->func(ip, parent_ip, op, regs);
4151 #if ARCH_SUPPORTS_FTRACE_OPS
4153 struct ftrace_ops *op,
struct pt_regs *regs)
4155 __ftrace_ops_list_func(ip, parent_ip,
NULL, regs);
4158 static void ftrace_ops_no_ops(
unsigned long ip,
unsigned long parent_ip)
4160 __ftrace_ops_list_func(ip, parent_ip,
NULL,
NULL);
4164 static void clear_ftrace_swapper(
void)
4172 clear_tsk_trace_trace(p);
4177 static void set_ftrace_swapper(
void)
4185 set_tsk_trace_trace(p);
4190 static void clear_ftrace_pid(
struct pid *
pid)
4196 clear_tsk_trace_trace(p);
4203 static void set_ftrace_pid(
struct pid *
pid)
4209 set_tsk_trace_trace(p);
4214 static void clear_ftrace_pid_task(
struct pid *
pid)
4216 if (pid == ftrace_swapper_pid)
4217 clear_ftrace_swapper();
4219 clear_ftrace_pid(pid);
4222 static void set_ftrace_pid_task(
struct pid *pid)
4224 if (pid == ftrace_swapper_pid)
4225 set_ftrace_swapper();
4227 set_ftrace_pid(pid);
4230 static int ftrace_pid_add(
int p)
4239 pid = ftrace_swapper_pid;
4249 if (fpid->pid == pid)
4258 list_add(&fpid->list, &ftrace_pids);
4261 set_ftrace_pid_task(pid);
4263 ftrace_update_pid_func();
4264 ftrace_startup_enable(0);
4270 if (pid != ftrace_swapper_pid)
4278 static
void ftrace_pid_reset(
void)
4284 struct pid *pid = fpid->
pid;
4286 clear_ftrace_pid_task(pid);
4292 ftrace_update_pid_func();
4293 ftrace_startup_enable(0);
4298 static void *fpid_start(
struct seq_file *m, loff_t *pos)
4302 if (list_empty(&ftrace_pids) && (!*pos))
4308 static void *fpid_next(
struct seq_file *m,
void *v, loff_t *pos)
4316 static void fpid_stop(
struct seq_file *m,
void *p)
4321 static int fpid_show(
struct seq_file *m,
void *v)
4325 if (v == (
void *)1) {
4330 if (fpid->
pid == ftrace_swapper_pid)
4339 .start = fpid_start,
4346 ftrace_pid_open(
struct inode *inode,
struct file *file)
4355 ret =
seq_open(file, &ftrace_pid_sops);
4361 ftrace_pid_write(
struct file *filp,
const char __user *ubuf,
4362 size_t cnt, loff_t *ppos)
4368 if (cnt >=
sizeof(buf))
4380 tmp = strstrip(buf);
4388 ret = ftrace_pid_add(val);
4390 return ret ? ret :
cnt;
4394 ftrace_pid_release(
struct inode *inode,
struct file *file)
4403 .open = ftrace_pid_open,
4404 .write = ftrace_pid_write,
4407 .release = ftrace_pid_release,
4410 static __init int ftrace_init_debugfs(
void)
4418 ftrace_init_dyn_debugfs(d_tracer);
4421 NULL, &ftrace_pid_fops);
4423 ftrace_profile_debugfs(d_tracer);
4438 ftrace_disabled = 1;
4448 return ftrace_disabled;
4468 ret = __register_ftrace_function(ops);
4489 ret = __unregister_ftrace_function(ops);
4500 void __user *buffer,
size_t *lenp,
4512 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4515 last_ftrace_enabled = !!ftrace_enabled;
4517 if (ftrace_enabled) {
4522 if (ftrace_ops_list != &ftrace_list_end) {
4523 if (ftrace_ops_list->next == &ftrace_list_end)
4524 ftrace_trace_function = ftrace_ops_list->func;
4531 ftrace_trace_function = ftrace_stub;
4541 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4543 static int ftrace_graph_active;
4557 static int alloc_retstack_tasklist(
struct ftrace_ret_stack **ret_stack_list)
4561 unsigned long flags;
4562 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4565 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4566 ret_stack_list[
i] =
kmalloc(FTRACE_RETFUNC_DEPTH
4569 if (!ret_stack_list[i]) {
4584 if (t->ret_stack ==
NULL) {
4587 t->curr_ret_stack = -1;
4590 t->ret_stack = ret_stack_list[start++];
4597 for (i = start; i <
end; i++)
4598 kfree(ret_stack_list[i]);
4603 ftrace_graph_probe_sched_switch(
void *ignore,
4621 if (!next->ftrace_timestamp)
4628 timestamp -= next->ftrace_timestamp;
4630 for (index = next->curr_ret_stack; index >= 0; index--)
4635 static int start_graph_tracing(
void)
4640 ret_stack_list =
kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4644 if (!ret_stack_list)
4650 ftrace_graph_init_idle_task(
idle_task(cpu), cpu);
4654 ret = alloc_retstack_tasklist(ret_stack_list);
4655 }
while (ret == -
EAGAIN);
4658 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch,
NULL);
4660 pr_info(
"ftrace_graph: Couldn't activate tracepoint"
4661 " probe to kernel_sched_switch\n");
4664 kfree(ret_stack_list);
4679 pause_graph_tracing();
4683 unpause_graph_tracing();
4697 if (ftrace_graph_active) {
4702 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4703 register_pm_notifier(&ftrace_suspend_notifier);
4705 ftrace_graph_active++;
4706 ret = start_graph_tracing();
4708 ftrace_graph_active--;
4712 ftrace_graph_return = retfunc;
4713 ftrace_graph_entry = entryfunc;
4722 void unregister_ftrace_graph(
void)
4726 if (
unlikely(!ftrace_graph_active))
4729 ftrace_graph_active--;
4731 ftrace_graph_entry = ftrace_graph_entry_stub;
4733 unregister_pm_notifier(&ftrace_suspend_notifier);
4734 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch,
NULL);
4747 t->ftrace_timestamp = 0;
4750 t->ret_stack = ret_stack;
4757 void ftrace_graph_init_idle_task(
struct task_struct *t,
int cpu)
4759 t->curr_ret_stack = -1;
4767 if (ftrace_graph_active) {
4770 ret_stack =
per_cpu(idle_ret_stack, cpu);
4772 ret_stack =
kmalloc(FTRACE_RETFUNC_DEPTH
4777 per_cpu(idle_ret_stack, cpu) = ret_stack;
4779 graph_init_task(t, ret_stack);
4784 void ftrace_graph_init_task(
struct task_struct *t)
4787 t->ret_stack =
NULL;
4788 t->curr_ret_stack = -1;
4790 if (ftrace_graph_active) {
4793 ret_stack =
kmalloc(FTRACE_RETFUNC_DEPTH
4798 graph_init_task(t, ret_stack);
4802 void ftrace_graph_exit_task(
struct task_struct *t)
4806 t->ret_stack =
NULL;
4813 void ftrace_graph_stop(
void)