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