1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
7 * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/tracepoint.h>
12 #include <linux/uaccess.h>
13 #include <linux/gfp.h>
15 #include <linux/proc_fs.h>
16 #include <linux/slab.h>
18 #include <linux/miscdevice.h>
19 #include <lttng-events.h>
21 #define TP_MODULE_NOAUTOLOAD
22 #define LTTNG_PACKAGE_BUILD
23 #define CREATE_TRACE_POINTS
24 #define TRACE_INCLUDE_PATH instrumentation/events/lttng-module
25 #define TRACE_INCLUDE_FILE lttng
26 #define LTTNG_INSTRUMENTATION
28 #include <instrumentation/events/lttng-module/lttng.h>
30 /* Events written through logger are truncated at 1024 bytes */
31 #define LTTNG_LOGGER_COUNT_MAX 1024
32 #define LTTNG_LOGGER_FILE "lttng-logger"
34 DEFINE_TRACE(lttng_logger
);
36 static struct proc_dir_entry
*lttng_logger_dentry
;
39 * lttng_logger_write - write a userspace string into the trace system
41 * @user_buf: user string
42 * @count: length to copy
43 * @ppos: file position
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.
52 ssize_t
lttng_logger_write(struct file
*file
, const char __user
*user_buf
,
53 size_t count
, loff_t
*ppos
)
56 unsigned long uaddr
= (unsigned long) user_buf
;
57 struct page
*pages
[2];
62 if (unlikely(count
> LTTNG_LOGGER_COUNT_MAX
))
63 count
= LTTNG_LOGGER_COUNT_MAX
;
65 /* How many pages are we dealing with ? */
66 if (unlikely((uaddr
& PAGE_MASK
) != ((uaddr
+ count
) & PAGE_MASK
)))
69 /* Pin userspace pages */
70 ret
= get_user_pages_fast(uaddr
, nr_pages
, 0, pages
);
71 if (unlikely(ret
< nr_pages
)) {
81 trace_lttng_logger(user_buf
, count
);
85 for (i
= 0; i
< nr_pages
; i
++)
91 static const struct file_operations lttng_logger_operations
= {
92 .write
= lttng_logger_write
,
95 static const struct proc_ops lttng_logger_proc_ops
= {
96 .proc_write
= lttng_logger_write
,
99 static struct miscdevice logger_dev
= {
100 .minor
= MISC_DYNAMIC_MINOR
,
101 .name
= "lttng-logger",
103 .fops
= <tng_logger_operations
106 int __init
lttng_logger_init(void)
110 /* /dev/lttng-logger */
111 ret
= misc_register(&logger_dev
);
113 printk(KERN_ERR
"Error creating LTTng logger device\n");
117 /* /proc/lttng-logger */
118 lttng_logger_dentry
= proc_create_data(LTTNG_LOGGER_FILE
,
119 S_IRUGO
| S_IWUGO
, NULL
,
120 <tng_logger_proc_ops
, NULL
);
121 if (!lttng_logger_dentry
) {
122 printk(KERN_ERR
"Error creating LTTng logger proc file\n");
128 ret
= __lttng_events_init__lttng();
134 remove_proc_entry("lttng-logger", NULL
);
136 misc_deregister(&logger_dev
);
141 void lttng_logger_exit(void)
143 __lttng_events_exit__lttng();
144 if (lttng_logger_dentry
)
145 remove_proc_entry("lttng-logger", NULL
);
146 misc_deregister(&logger_dev
);