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