trace mechanism in linux

一:tracing目录文件以及创建的代码

root@memory-153:/sys/kernel/debug/tracing# ls

available_events            options              stack_trace_filter

available_filter_functions  per_cpu              trace

available_tracers           printk_formats       trace_clock

buffer_size_kb              README               trace_marker

buffer_total_size_kb        saved_cmdlines       trace_options

current_tracer              saved_cmdlines_size  trace_pipe

dyn_ftrace_total_info       set_event            trace_stat

enabled_functions           set_ftrace_filter    tracing_cpumask

events                      set_ftrace_notrace   tracing_max_latency

free_buffer                 set_ftrace_pid       tracing_on

function_profile_enabled    set_graph_function   tracing_thresh

instances                   set_graph_notrace    uprobe_events

kprobe_events               snapshot             uprobe_profile

kprobe_profile              stack_max_size

max_graph_depth             stack_trace

root@memory-153:/sys/kernel/debug/tracing# cat /boot/System.map-3.12.1 | grep trace | grep __initcall

ffffffff81e482d0 t __initcall_trace_init_flags_sys_exitearly

ffffffff81e482d8 t __initcall_trace_init_flags_sys_enterearly

ffffffff81e482e8 t __initcall_register_trigger_all_cpu_backtraceearly

ffffffff81e48348 t __initcall_tracer_alloc_buffersearly

ffffffff81e48358 t __initcall_init_trace_printkearly

ffffffff81e48360 t __initcall_event_trace_memsetupearly

ffffffff81e48368 t __initcall_init_ftrace_syscallsearly

ffffffff81e48410 t __initcall_ftrace_mod_cmd_init1

ffffffff81e48418 t __initcall_init_function_trace1

ffffffff81e48420 t __initcall_init_wakeup_tracer1

ffffffff81e48428 t __initcall_init_graph_trace1

ffffffff81e48430 t __initcall_event_trace_enable1

ffffffff81e48978 t __initcall_ftrace_init_debugfs5

ffffffff81e48980 t __initcall_tracer_init_debugfs5

ffffffff81e48988 t __initcall_init_trace_printk_function_export5

ffffffff81e48998 t __initcall_event_trace_init5

ffffffff81e489a0 t __initcall_init_kprobe_trace5

ffffffff81e489a8 t __initcall_init_uprobe_trace5

ffffffff81e48c10 t __initcall_init_tracepoints6

ffffffff81e48c20 t __initcall_stack_trace_init6

ffffffff81e48c28 t __initcall_init_mmio_trace6

ffffffff81e48c30 t __initcall_init_blk_tracer6

ffffffff81e494a8 t __initcall_clear_boot_tracer7

ffffffff81e494b0 t __initcall_kdb_ftrace_register7

ftrace_init_debugfs 7

available_filter_functions enabled_functions set_ftrace_filter

set_ftrace_notrace set_graph_function set_ftrace_pid trace_stat function_profile_enabled

tracer_init_debugfs 20

tracing_cpumask trace_options trace trace_pipe buffer_size_kb buffer_total_size_kb free_buffer trace_marker trace_clock tracing_on snapshot per_cpu

available_tracers current_tracer tracing_max_latency tracing_thresh README saved_cmdlines dyn_ftrace_total_info instances options

init_trace_printk_function_export 1 printk_formats

event_trace_init 3

available_events set_event events

init_kprobe_trace 2

kprobe_events kprobe_profile

init_uprobe_trace 2

uprobe_events uprobe_profile

stack_trace_init 3

stack_max_size stack_trace stack_trace_filter

not found:

saved_cmdlines_size set_graph_notrace max_graph_depth

二:tracepoint, ftrace event, syscall trace

tracepoint

VMLINUX_SYMBOL(__start___tracepoints_ptrs) = .;  \

*(__tracepoints_ptrs) /* Tracepoints: pointer array */\

VMLINUX_SYMBOL(__stop___tracepoints_ptrs) = .;  \

*(__tracepoints_strings)/* Tracepoints: strings */ \

tracepoint.h

#define DEFINE_TRACE_FN(name, reg, unreg)     \

static const char __tpstrtab_##name[]     \

__attribute__((section("__tracepoints_strings"))) = #name;  \

struct tracepoint __tracepoint_##name     \

__attribute__((section("__tracepoints"))) =    \

{ __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\

static struct tracepoint * const __tracepoint_ptr_##name __used  \

__attribute__((section("__tracepoints_ptrs"))) =   \

&__tracepoint_##name;

ftrace event

trace_types  所有tracer链表

nop tracer

function_trace

wakeup_tracer

wakeup_rt_tracer

mmio_tracer

blk_tracer

vmlinux.lds.h

#define FTRACE_EVENTS() . = ALIGN(8);     \

VMLINUX_SYMBOL(__start_ftrace_events) = .; \

*(_ftrace_events)    \

VMLINUX_SYMBOL(__stop_ftrace_events) = .;

ftrace.h

#undef DEFINE_EVENT_PRINT

#define DEFINE_EVENT_PRINT(template, call, proto, args, print)  \

\

static const char print_fmt_##call[] = print;    \

\

static struct ftrace_event_call __used event_##call = {   \

.name   = #call,    \

.class   = &event_class_##template,  \

.event.funcs  = &ftrace_event_type_funcs_##call, \

.print_fmt  = print_fmt_##call,   \

};         \

static struct ftrace_event_call __used     \

__attribute__((section("_ftrace_events"))) *__event_##call = &event_##call

syscall trace

#define SYSCALL_TRACE_ENTER_EVENT(sname)    \

static struct syscall_metadata __syscall_meta_##sname;  \

static struct ftrace_event_call __used    \

event_enter_##sname = {     \

.name                   = "sys_enter"#sname,  \

.class   = &event_class_syscall_enter, \

.event.funcs            = &enter_syscall_print_funcs, \

.data   = (void *)&__syscall_meta_##sname,\

.flags   = TRACE_EVENT_FL_CAP_ANY, \

};        \

static struct ftrace_event_call __used    \

__attribute__((section("_ftrace_events")))   \

*__event_enter_##sname = &event_enter_##sname;

#define SYSCALL_TRACE_EXIT_EVENT(sname)     \

static struct syscall_metadata __syscall_meta_##sname;  \

static struct ftrace_event_call __used    \

event_exit_##sname = {     \

.name                   = "sys_exit"#sname,  \

.class   = &event_class_syscall_exit, \

.event.funcs  = &exit_syscall_print_funcs, \

.data   = (void *)&__syscall_meta_##sname,\

.flags   = TRACE_EVENT_FL_CAP_ANY, \

};        \

static struct ftrace_event_call __used    \

__attribute__((section("_ftrace_events")))   \

*__event_exit_##sname = &event_exit_##sname;

#define SYSCALL_METADATA(sname, nb, ...)   \

static const char *types_##sname[] = {   \

__MAP(nb,__SC_STR_TDECL,__VA_ARGS__)  \

};       \

static const char *args_##sname[] = {   \

__MAP(nb,__SC_STR_ADECL,__VA_ARGS__)  \

};       \

SYSCALL_TRACE_ENTER_EVENT(sname);   \

SYSCALL_TRACE_EXIT_EVENT(sname);   \

static struct syscall_metadata __used   \

__syscall_meta_##sname = {    \

.name   = "sys"#sname,   \

.syscall_nr = -1, /* Filled in at boot */ \

.nb_args  = nb,    \

.types  = nb ? types_##sname : NULL, \

.args  = nb ? args_##sname : NULL, \

.enter_event = &event_enter_##sname,  \

.exit_event = &event_exit_##sname,  \

.enter_fields = LIST_HEAD_INIT(__syscall_meta_##sname.enter_fields), \

};       \

static struct syscall_metadata __used   \

__attribute__((section("__syscalls_metadata"))) \

*__p_syscall_meta_##sname = &__syscall_meta_##sname;

#else

#define SYSCALL_METADATA(sname, nb, ...)

#endif

#define SYSCALL_DEFINE0(sname)     \

SYSCALL_METADATA(_##sname, 0);    \

asmlinkage long sys_##sname(void)

#define SYSCALL_DEFINE1(name, ...) SYSCALL_DEFINEx(1, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE2(name, ...) SYSCALL_DEFINEx(2, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE3(name, ...) SYSCALL_DEFINEx(3, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE4(name, ...) SYSCALL_DEFINEx(4, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE5(name, ...) SYSCALL_DEFINEx(5, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE6(name, ...) SYSCALL_DEFINEx(6, _##name, __VA_ARGS__)

#define SYSCALL_DEFINEx(x, sname, ...)    \

SYSCALL_METADATA(sname, x, __VA_ARGS__)   \

__SYSCALL_DEFINEx(x, sname, __VA_ARGS__)

你可能感兴趣的:(trace mechanism in linux)