Move headers under include/
[lttng-modules.git] / probes / lttng.c
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * lttng.c
4 *
5 * LTTng logger ABI
6 *
7 * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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>
18 #include <linux/miscdevice.h>
19 #include <lttng/lttng-events.h>
20
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
27
28 #include <instrumentation/events/lttng-module/lttng.h>
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
34 DEFINE_TRACE(lttng_logger);
35
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 {
55 int nr_pages = 1, i;
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
95 static const struct proc_ops lttng_logger_proc_ops = {
96 .proc_write = lttng_logger_write,
97 };
98
99 static struct miscdevice logger_dev = {
100 .minor = MISC_DYNAMIC_MINOR,
101 .name = "lttng-logger",
102 .mode = 0666,
103 .fops = &lttng_logger_operations
104 };
105
106 int __init lttng_logger_init(void)
107 {
108 int ret = 0;
109
110 /* /dev/lttng-logger */
111 ret = misc_register(&logger_dev);
112 if (ret) {
113 printk(KERN_ERR "Error creating LTTng logger device\n");
114 goto error;
115 }
116
117 /* /proc/lttng-logger */
118 lttng_logger_dentry = proc_create_data(LTTNG_LOGGER_FILE,
119 S_IRUGO | S_IWUGO, NULL,
120 &lttng_logger_proc_ops, NULL);
121 if (!lttng_logger_dentry) {
122 printk(KERN_ERR "Error creating LTTng logger proc file\n");
123 ret = -ENOMEM;
124 goto error_proc;
125 }
126
127 /* Init */
128 ret = __lttng_events_init__lttng();
129 if (ret)
130 goto error_events;
131 return ret;
132
133 error_events:
134 remove_proc_entry("lttng-logger", NULL);
135 error_proc:
136 misc_deregister(&logger_dev);
137 error:
138 return ret;
139 }
140
141 void lttng_logger_exit(void)
142 {
143 __lttng_events_exit__lttng();
144 if (lttng_logger_dentry)
145 remove_proc_entry("lttng-logger", NULL);
146 misc_deregister(&logger_dev);
147 }
This page took 0.032311 seconds and 4 git commands to generate.