trace_kprobe.c (4f6b838c378a52ea3ae0b15f12ca8a20849072fa) trace_kprobe.c (d741bf41d7c7db4898bacfcb020353cddc032fd8)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Kprobes-based tracing events
4 *
5 * Created by Masami Hiramatsu <mhiramat@redhat.com>
6 *
7 */
8#define pr_fmt(fmt) "trace_kprobe: " fmt

--- 92 unchanged lines hidden (view full) ---

101static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
102{
103 return !!(kprobe_gone(&tk->rp.kp));
104}
105
106static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
107 struct module *mod)
108{
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Kprobes-based tracing events
4 *
5 * Created by Masami Hiramatsu <mhiramat@redhat.com>
6 *
7 */
8#define pr_fmt(fmt) "trace_kprobe: " fmt

--- 92 unchanged lines hidden (view full) ---

101static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
102{
103 return !!(kprobe_gone(&tk->rp.kp));
104}
105
106static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
107 struct module *mod)
108{
109 int len = strlen(module_name(mod));
109 int len = strlen(mod->name);
110 const char *name = trace_kprobe_symbol(tk);
110 const char *name = trace_kprobe_symbol(tk);
111
112 return strncmp(module_name(mod), name, len) == 0 && name[len] == ':';
111 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
113}
114
115static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
116{
117 char *p;
118 bool ret;
119
120 if (!tk->symbol)

--- 563 unchanged lines hidden (view full) ---

684 for_each_trace_kprobe(tk, pos) {
685 if (trace_kprobe_within_module(tk, mod)) {
686 /* Don't need to check busy - this should have gone. */
687 __unregister_trace_kprobe(tk);
688 ret = __register_trace_kprobe(tk);
689 if (ret)
690 pr_warn("Failed to re-register probe %s on %s: %d\n",
691 trace_probe_name(&tk->tp),
112}
113
114static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
115{
116 char *p;
117 bool ret;
118
119 if (!tk->symbol)

--- 563 unchanged lines hidden (view full) ---

683 for_each_trace_kprobe(tk, pos) {
684 if (trace_kprobe_within_module(tk, mod)) {
685 /* Don't need to check busy - this should have gone. */
686 __unregister_trace_kprobe(tk);
687 ret = __register_trace_kprobe(tk);
688 if (ret)
689 pr_warn("Failed to re-register probe %s on %s: %d\n",
690 trace_probe_name(&tk->tp),
692 module_name(mod), ret);
691 mod->name, ret);
693 }
694 }
695 mutex_unlock(&event_mutex);
696
697 return NOTIFY_DONE;
698}
699
700static struct notifier_block trace_kprobe_module_nb = {

--- 12 unchanged lines hidden (view full) ---

713static int trace_kprobe_create(int argc, const char *argv[])
714{
715 /*
716 * Argument syntax:
717 * - Add kprobe:
718 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
719 * - Add kretprobe:
720 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
692 }
693 }
694 mutex_unlock(&event_mutex);
695
696 return NOTIFY_DONE;
697}
698
699static struct notifier_block trace_kprobe_module_nb = {

--- 12 unchanged lines hidden (view full) ---

712static int trace_kprobe_create(int argc, const char *argv[])
713{
714 /*
715 * Argument syntax:
716 * - Add kprobe:
717 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
718 * - Add kretprobe:
719 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
721 * Or
722 * p:[GRP/]EVENT] [MOD:]KSYM[+0]%return [FETCHARGS]
723 *
724 * Fetch args:
725 * $retval : fetch return value
726 * $stack : fetch stack address
727 * $stackN : fetch Nth of stack (N:0-)
728 * $comm : fetch current task comm
729 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
730 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
731 * %REG : fetch register REG

--- 13 unchanged lines hidden (view full) ---

745 long offset = 0;
746 void *addr = NULL;
747 char buf[MAX_EVENT_NAME_LEN];
748 unsigned int flags = TPARG_FL_KERNEL;
749
750 switch (argv[0][0]) {
751 case 'r':
752 is_return = true;
720 * Fetch args:
721 * $retval : fetch return value
722 * $stack : fetch stack address
723 * $stackN : fetch Nth of stack (N:0-)
724 * $comm : fetch current task comm
725 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
726 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
727 * %REG : fetch register REG

--- 13 unchanged lines hidden (view full) ---

741 long offset = 0;
742 void *addr = NULL;
743 char buf[MAX_EVENT_NAME_LEN];
744 unsigned int flags = TPARG_FL_KERNEL;
745
746 switch (argv[0][0]) {
747 case 'r':
748 is_return = true;
749 flags |= TPARG_FL_RETURN;
753 break;
754 case 'p':
755 break;
756 default:
757 return -ECANCELED;
758 }
759 if (argc < 2)
760 return -ECANCELED;

--- 41 unchanged lines hidden (view full) ---

802 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
803 ret = -ECANCELED;
804 goto error;
805 }
806 /* a symbol specified */
807 symbol = kstrdup(argv[1], GFP_KERNEL);
808 if (!symbol)
809 return -ENOMEM;
750 break;
751 case 'p':
752 break;
753 default:
754 return -ECANCELED;
755 }
756 if (argc < 2)
757 return -ECANCELED;

--- 41 unchanged lines hidden (view full) ---

799 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
800 ret = -ECANCELED;
801 goto error;
802 }
803 /* a symbol specified */
804 symbol = kstrdup(argv[1], GFP_KERNEL);
805 if (!symbol)
806 return -ENOMEM;
810
811 tmp = strchr(symbol, '%');
812 if (tmp) {
813 if (!strcmp(tmp, "%return")) {
814 *tmp = '\0';
815 is_return = true;
816 } else {
817 trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX);
818 goto parse_error;
819 }
820 }
821
822 /* TODO: support .init module functions */
823 ret = traceprobe_split_symbol_offset(symbol, &offset);
824 if (ret || offset < 0 || offset > UINT_MAX) {
825 trace_probe_log_err(0, BAD_PROBE_ADDR);
826 goto parse_error;
827 }
807 /* TODO: support .init module functions */
808 ret = traceprobe_split_symbol_offset(symbol, &offset);
809 if (ret || offset < 0 || offset > UINT_MAX) {
810 trace_probe_log_err(0, BAD_PROBE_ADDR);
811 goto parse_error;
812 }
828 if (is_return)
829 flags |= TPARG_FL_RETURN;
830 if (kprobe_on_func_entry(NULL, symbol, offset))
831 flags |= TPARG_FL_FENTRY;
832 if (offset && is_return && !(flags & TPARG_FL_FENTRY)) {
833 trace_probe_log_err(0, BAD_RETPROBE);
834 goto parse_error;
835 }
836 }
837

--- 888 unchanged lines hidden (view full) ---

1726#endif
1727 return ret;
1728}
1729NOKPROBE_SYMBOL(kprobe_dispatcher);
1730
1731static int
1732kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1733{
813 if (kprobe_on_func_entry(NULL, symbol, offset))
814 flags |= TPARG_FL_FENTRY;
815 if (offset && is_return && !(flags & TPARG_FL_FENTRY)) {
816 trace_probe_log_err(0, BAD_RETPROBE);
817 goto parse_error;
818 }
819 }
820

--- 888 unchanged lines hidden (view full) ---

1709#endif
1710 return ret;
1711}
1712NOKPROBE_SYMBOL(kprobe_dispatcher);
1713
1714static int
1715kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1716{
1734 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1717 struct kretprobe *rp = get_kretprobe(ri);
1718 struct trace_kprobe *tk = container_of(rp, struct trace_kprobe, rp);
1735
1736 raw_cpu_inc(*tk->nhit);
1737
1738 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
1739 kretprobe_trace_func(tk, ri, regs);
1740#ifdef CONFIG_PERF_EVENTS
1741 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
1742 kretprobe_perf_func(tk, ri, regs);

--- 149 unchanged lines hidden (view full) ---

1892
1893 cmd = p;
1894 }
1895
1896 enable_boot_kprobe_events();
1897}
1898
1899/*
1719
1720 raw_cpu_inc(*tk->nhit);
1721
1722 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
1723 kretprobe_trace_func(tk, ri, regs);
1724#ifdef CONFIG_PERF_EVENTS
1725 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
1726 kretprobe_perf_func(tk, ri, regs);

--- 149 unchanged lines hidden (view full) ---

1876
1877 cmd = p;
1878 }
1879
1880 enable_boot_kprobe_events();
1881}
1882
1883/*
1900 * Register dynevent at core_initcall. This allows kernel to setup kprobe
1901 * events in postcore_initcall without tracefs.
1884 * Register dynevent at subsys_initcall. This allows kernel to setup kprobe
1885 * events in fs_initcall without tracefs.
1902 */
1903static __init int init_kprobe_trace_early(void)
1904{
1905 int ret;
1906
1907 ret = dyn_event_register(&trace_kprobe_ops);
1908 if (ret)
1909 return ret;
1910
1911 if (register_module_notifier(&trace_kprobe_module_nb))
1912 return -EINVAL;
1913
1914 return 0;
1915}
1886 */
1887static __init int init_kprobe_trace_early(void)
1888{
1889 int ret;
1890
1891 ret = dyn_event_register(&trace_kprobe_ops);
1892 if (ret)
1893 return ret;
1894
1895 if (register_module_notifier(&trace_kprobe_module_nb))
1896 return -EINVAL;
1897
1898 return 0;
1899}
1916core_initcall(init_kprobe_trace_early);
1900subsys_initcall(init_kprobe_trace_early);
1917
1918/* Make a tracefs interface for controlling probe points */
1919static __init int init_kprobe_trace(void)
1920{
1901
1902/* Make a tracefs interface for controlling probe points */
1903static __init int init_kprobe_trace(void)
1904{
1921 int ret;
1905 struct dentry *d_tracer;
1922 struct dentry *entry;
1923
1906 struct dentry *entry;
1907
1924 ret = tracing_init_dentry();
1925 if (ret)
1908 d_tracer = tracing_init_dentry();
1909 if (IS_ERR(d_tracer))
1926 return 0;
1927
1910 return 0;
1911
1928 entry = tracefs_create_file("kprobe_events", 0644, NULL,
1912 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1929 NULL, &kprobe_events_ops);
1930
1931 /* Event list interface */
1932 if (!entry)
1933 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1934
1935 /* Profile interface */
1913 NULL, &kprobe_events_ops);
1914
1915 /* Event list interface */
1916 if (!entry)
1917 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1918
1919 /* Profile interface */
1936 entry = tracefs_create_file("kprobe_profile", 0444, NULL,
1920 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1937 NULL, &kprobe_profile_ops);
1938
1939 if (!entry)
1940 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1941
1942 setup_boot_kprobe_events();
1943
1944 return 0;

--- 169 unchanged lines hidden ---
1921 NULL, &kprobe_profile_ops);
1922
1923 if (!entry)
1924 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1925
1926 setup_boot_kprobe_events();
1927
1928 return 0;

--- 169 unchanged lines hidden ---