Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 2 | * |
0c956676 MD |
3 | * lttng.c |
4 | * | |
5 | * LTTng logger ABI | |
6 | * | |
7 | * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
0c956676 MD |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
0116245f | 11 | #include <wrapper/tracepoint.h> |
0c956676 MD |
12 | #include <linux/uaccess.h> |
13 | #include <linux/gfp.h> | |
14 | #include <linux/fs.h> | |
15 | #include <linux/proc_fs.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/mm.h> | |
4d328377 | 18 | #include <linux/miscdevice.h> |
156a3977 | 19 | #include <wrapper/vmalloc.h> |
2df37e95 | 20 | #include <lttng/events.h> |
75624557 | 21 | #include <lttng/events-internal.h> |
0c956676 MD |
22 | |
23 | #define TP_MODULE_NOAUTOLOAD | |
24 | #define LTTNG_PACKAGE_BUILD | |
25 | #define CREATE_TRACE_POINTS | |
4f47ccf0 | 26 | #define TRACE_INCLUDE_PATH instrumentation/events |
0c956676 | 27 | #define TRACE_INCLUDE_FILE lttng |
3bc29f0a | 28 | #define LTTNG_INSTRUMENTATION |
0c956676 | 29 | |
4f47ccf0 | 30 | #include <instrumentation/events/lttng.h> |
0c956676 MD |
31 | |
32 | /* Events written through logger are truncated at 1024 bytes */ | |
33 | #define LTTNG_LOGGER_COUNT_MAX 1024 | |
34 | #define LTTNG_LOGGER_FILE "lttng-logger" | |
35 | ||
0116245f MJ |
36 | LTTNG_DEFINE_TRACE(lttng_logger, |
37 | PARAMS(const char __user *text, size_t len), | |
38 | PARAMS(text, len) | |
39 | ); | |
20591cf7 | 40 | |
0c956676 MD |
41 | static struct proc_dir_entry *lttng_logger_dentry; |
42 | ||
43 | /** | |
44 | * lttng_logger_write - write a userspace string into the trace system | |
45 | * @file: file pointer | |
46 | * @user_buf: user string | |
47 | * @count: length to copy | |
48 | * @ppos: file position | |
49 | * | |
50 | * Copy a userspace string into a trace event named "lttng:logger". | |
51 | * Copies at most @count bytes into the event "msg" dynamic array. | |
52 | * Truncates the count at LTTNG_LOGGER_COUNT_MAX. Returns the number of | |
53 | * bytes copied from the source. | |
54 | * Return -1 on error, with EFAULT errno. | |
55 | */ | |
56 | static | |
57 | ssize_t lttng_logger_write(struct file *file, const char __user *user_buf, | |
58 | size_t count, loff_t *ppos) | |
59 | { | |
467f3a48 | 60 | int nr_pages = 1, i; |
0c956676 MD |
61 | unsigned long uaddr = (unsigned long) user_buf; |
62 | struct page *pages[2]; | |
63 | ssize_t written; | |
64 | int ret; | |
65 | ||
66 | /* Truncate count */ | |
67 | if (unlikely(count > LTTNG_LOGGER_COUNT_MAX)) | |
68 | count = LTTNG_LOGGER_COUNT_MAX; | |
69 | ||
70 | /* How many pages are we dealing with ? */ | |
71 | if (unlikely((uaddr & PAGE_MASK) != ((uaddr + count) & PAGE_MASK))) | |
72 | nr_pages = 2; | |
73 | ||
74 | /* Pin userspace pages */ | |
75 | ret = get_user_pages_fast(uaddr, nr_pages, 0, pages); | |
76 | if (unlikely(ret < nr_pages)) { | |
77 | if (ret > 0) { | |
78 | BUG_ON(ret != 1); | |
79 | put_page(pages[0]); | |
80 | } | |
81 | written = -EFAULT; | |
82 | goto end; | |
83 | } | |
84 | ||
85 | /* Trace the event */ | |
86 | trace_lttng_logger(user_buf, count); | |
87 | written = count; | |
88 | *ppos += written; | |
89 | ||
90 | for (i = 0; i < nr_pages; i++) | |
91 | put_page(pages[i]); | |
92 | end: | |
93 | return written; | |
94 | } | |
95 | ||
96 | static const struct file_operations lttng_logger_operations = { | |
97 | .write = lttng_logger_write, | |
98 | }; | |
99 | ||
059de147 MJ |
100 | /* |
101 | * Linux 5.6 introduced a separate proc_ops struct for /proc operations | |
102 | * to decouple it from the vfs. | |
103 | */ | |
5f4c791e | 104 | #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0)) |
059de147 MJ |
105 | static const struct proc_ops lttng_logger_proc_ops = { |
106 | .proc_write = lttng_logger_write, | |
107 | }; | |
108 | #else | |
109 | #define lttng_logger_proc_ops lttng_logger_operations | |
110 | #endif | |
111 | ||
4d328377 SG |
112 | static struct miscdevice logger_dev = { |
113 | .minor = MISC_DYNAMIC_MINOR, | |
114 | .name = "lttng-logger", | |
115 | .mode = 0666, | |
116 | .fops = <tng_logger_operations | |
117 | }; | |
118 | ||
0c956676 MD |
119 | int __init lttng_logger_init(void) |
120 | { | |
121 | int ret = 0; | |
122 | ||
263b6c88 | 123 | wrapper_vmalloc_sync_mappings(); |
4d328377 SG |
124 | |
125 | /* /dev/lttng-logger */ | |
126 | ret = misc_register(&logger_dev); | |
127 | if (ret) { | |
5a15f70c | 128 | printk(KERN_ERR "LTTng: Error creating logger device\n"); |
4d328377 SG |
129 | goto error; |
130 | } | |
131 | ||
132 | /* /proc/lttng-logger */ | |
0c956676 MD |
133 | lttng_logger_dentry = proc_create_data(LTTNG_LOGGER_FILE, |
134 | S_IRUGO | S_IWUGO, NULL, | |
059de147 | 135 | <tng_logger_proc_ops, NULL); |
0c956676 | 136 | if (!lttng_logger_dentry) { |
5a15f70c | 137 | printk(KERN_ERR "LTTng: Error creating logger proc file\n"); |
0c956676 | 138 | ret = -ENOMEM; |
4d328377 | 139 | goto error_proc; |
0c956676 | 140 | } |
4d328377 SG |
141 | |
142 | /* Init */ | |
0c956676 MD |
143 | ret = __lttng_events_init__lttng(); |
144 | if (ret) | |
145 | goto error_events; | |
146 | return ret; | |
147 | ||
148 | error_events: | |
149 | remove_proc_entry("lttng-logger", NULL); | |
4d328377 SG |
150 | error_proc: |
151 | misc_deregister(&logger_dev); | |
0c956676 MD |
152 | error: |
153 | return ret; | |
154 | } | |
155 | ||
32fe46fb | 156 | void lttng_logger_exit(void) |
0c956676 MD |
157 | { |
158 | __lttng_events_exit__lttng(); | |
159 | if (lttng_logger_dentry) | |
160 | remove_proc_entry("lttng-logger", NULL); | |
4d328377 | 161 | misc_deregister(&logger_dev); |
0c956676 | 162 | } |