1 // SPDX-License-Identifier: GPL-2.0
3 * This code exports profiling data as debugfs files to userspace.
5 * Copyright IBM Corp. 2009
6 * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
8 * Uses gcc-internal data definitions.
9 * Based on the gcov-kernel patch by:
10 * Hubertus Franke <frankeh@us.ibm.com>
11 * Nigel Hinds <nhinds@us.ibm.com>
12 * Rajan Ravindran <rajancr@us.ibm.com>
13 * Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
18 #define pr_fmt(fmt) "gcov: " fmt
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/debugfs.h>
24 #include <linux/list.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
27 #include <linux/mutex.h>
28 #include <linux/seq_file.h>
32 * struct gcov_node - represents a debugfs entry
33 * @list: list head for child node list
34 * @children: child nodes
35 * @all: list head for list of all nodes
36 * @parent: parent node
37 * @loaded_info: array of pointers to profiling data sets for loaded object
39 * @num_loaded: number of profiling data sets for loaded object files.
40 * @unloaded_info: accumulated copy of profiling data sets for unloaded
41 * object files. Used only when gcov_persist=1.
42 * @dentry: main debugfs entry, either a directory or data file
43 * @links: associated symbolic links
44 * @name: data file basename
46 * struct gcov_node represents an entity within the gcov/ subdirectory
47 * of debugfs. There are directory and data file nodes. The latter represent
48 * the actual synthesized data file plus any associated symbolic links which
49 * are needed by the gcov tool to work correctly.
52 struct list_head list
;
53 struct list_head children
;
55 struct gcov_node
*parent
;
56 struct gcov_info
**loaded_info
;
57 struct gcov_info
*unloaded_info
;
58 struct dentry
*dentry
;
59 struct dentry
**links
;
64 static const char objtree
[] = OBJTREE
;
65 static const char srctree
[] = SRCTREE
;
66 static struct gcov_node root_node
;
67 static LIST_HEAD(all_head
);
68 static DEFINE_MUTEX(node_lock
);
70 /* If non-zero, keep copies of profiling data for unloaded modules. */
71 static int gcov_persist
= 1;
73 static int __init
gcov_persist_setup(char *str
)
77 if (kstrtoul(str
, 0, &val
)) {
78 pr_warn("invalid gcov_persist parameter '%s'\n", str
);
82 pr_info("setting gcov_persist to %d\n", gcov_persist
);
86 __setup("gcov_persist=", gcov_persist_setup
);
89 * seq_file.start() implementation for gcov data files. Note that the
90 * gcov_iterator interface is designed to be more restrictive than seq_file
91 * (no start from arbitrary position, etc.), to simplify the iterator
94 static void *gcov_seq_start(struct seq_file
*seq
, loff_t
*pos
)
98 gcov_iter_start(seq
->private);
99 for (i
= 0; i
< *pos
; i
++) {
100 if (gcov_iter_next(seq
->private))
106 /* seq_file.next() implementation for gcov data files. */
107 static void *gcov_seq_next(struct seq_file
*seq
, void *data
, loff_t
*pos
)
109 struct gcov_iterator
*iter
= data
;
111 if (gcov_iter_next(iter
))
118 /* seq_file.show() implementation for gcov data files. */
119 static int gcov_seq_show(struct seq_file
*seq
, void *data
)
121 struct gcov_iterator
*iter
= data
;
123 if (gcov_iter_write(iter
, seq
))
128 static void gcov_seq_stop(struct seq_file
*seq
, void *data
)
133 static const struct seq_operations gcov_seq_ops
= {
134 .start
= gcov_seq_start
,
135 .next
= gcov_seq_next
,
136 .show
= gcov_seq_show
,
137 .stop
= gcov_seq_stop
,
141 * Return a profiling data set associated with the given node. This is
142 * either a data set for a loaded object file or a data set copy in case
143 * all associated object files have been unloaded.
145 static struct gcov_info
*get_node_info(struct gcov_node
*node
)
147 if (node
->num_loaded
> 0)
148 return node
->loaded_info
[0];
150 return node
->unloaded_info
;
154 * Return a newly allocated profiling data set which contains the sum of
155 * all profiling data associated with the given node.
157 static struct gcov_info
*get_accumulated_info(struct gcov_node
*node
)
159 struct gcov_info
*info
;
162 if (node
->unloaded_info
)
163 info
= gcov_info_dup(node
->unloaded_info
);
165 info
= gcov_info_dup(node
->loaded_info
[i
++]);
168 for (; i
< node
->num_loaded
; i
++)
169 gcov_info_add(info
, node
->loaded_info
[i
]);
175 * open() implementation for gcov data files. Create a copy of the profiling
176 * data set and initialize the iterator and seq_file interface.
178 static int gcov_seq_open(struct inode
*inode
, struct file
*file
)
180 struct gcov_node
*node
= inode
->i_private
;
181 struct gcov_iterator
*iter
;
182 struct seq_file
*seq
;
183 struct gcov_info
*info
;
186 mutex_lock(&node_lock
);
188 * Read from a profiling data copy to minimize reference tracking
189 * complexity and concurrent access and to keep accumulating multiple
190 * profiling data sets associated with one node simple.
192 info
= get_accumulated_info(node
);
195 iter
= gcov_iter_new(info
);
198 rc
= seq_open(file
, &gcov_seq_ops
);
200 goto err_free_iter_info
;
201 seq
= file
->private_data
;
204 mutex_unlock(&node_lock
);
208 gcov_iter_free(iter
);
210 gcov_info_free(info
);
215 * release() implementation for gcov data files. Release resources allocated
218 static int gcov_seq_release(struct inode
*inode
, struct file
*file
)
220 struct gcov_iterator
*iter
;
221 struct gcov_info
*info
;
222 struct seq_file
*seq
;
224 seq
= file
->private_data
;
226 info
= gcov_iter_get_info(iter
);
227 gcov_iter_free(iter
);
228 gcov_info_free(info
);
229 seq_release(inode
, file
);
235 * Find a node by the associated data file name. Needs to be called with
238 static struct gcov_node
*get_node_by_name(const char *name
)
240 struct gcov_node
*node
;
241 struct gcov_info
*info
;
243 list_for_each_entry(node
, &all_head
, all
) {
244 info
= get_node_info(node
);
245 if (info
&& (strcmp(gcov_info_filename(info
), name
) == 0))
253 * Reset all profiling data associated with the specified node.
255 static void reset_node(struct gcov_node
*node
)
259 if (node
->unloaded_info
)
260 gcov_info_reset(node
->unloaded_info
);
261 for (i
= 0; i
< node
->num_loaded
; i
++)
262 gcov_info_reset(node
->loaded_info
[i
]);
265 static void remove_node(struct gcov_node
*node
);
268 * write() implementation for gcov data files. Reset profiling data for the
269 * corresponding file. If all associated object files have been unloaded,
270 * remove the debug fs node as well.
272 static ssize_t
gcov_seq_write(struct file
*file
, const char __user
*addr
,
273 size_t len
, loff_t
*pos
)
275 struct seq_file
*seq
;
276 struct gcov_info
*info
;
277 struct gcov_node
*node
;
279 seq
= file
->private_data
;
280 info
= gcov_iter_get_info(seq
->private);
281 mutex_lock(&node_lock
);
282 node
= get_node_by_name(gcov_info_filename(info
));
284 /* Reset counts or remove node for unloaded modules. */
285 if (node
->num_loaded
== 0)
290 /* Reset counts for open file. */
291 gcov_info_reset(info
);
292 mutex_unlock(&node_lock
);
298 * Given a string <path> representing a file path of format:
300 * construct and return a new string:
301 * <dir/>path/to/file.<ext>
303 static char *link_target(const char *dir
, const char *path
, const char *ext
)
309 copy
= kstrdup(path
, GFP_KERNEL
);
312 old_ext
= strrchr(copy
, '.');
316 target
= kasprintf(GFP_KERNEL
, "%s/%s.%s", dir
, copy
, ext
);
318 target
= kasprintf(GFP_KERNEL
, "%s.%s", copy
, ext
);
325 * Construct a string representing the symbolic link target for the given
326 * gcov data file name and link type. Depending on the link type and the
327 * location of the data file, the link target can either point to a
328 * subdirectory of srctree, objtree or in an external location.
330 static char *get_link_target(const char *filename
, const struct gcov_link
*ext
)
335 if (strncmp(filename
, objtree
, strlen(objtree
)) == 0) {
336 rel
= filename
+ strlen(objtree
) + 1;
337 if (ext
->dir
== SRC_TREE
)
338 result
= link_target(srctree
, rel
, ext
->ext
);
340 result
= link_target(objtree
, rel
, ext
->ext
);
342 /* External compilation. */
343 result
= link_target(NULL
, filename
, ext
->ext
);
349 #define SKEW_PREFIX ".tmp_"
352 * For a filename .tmp_filename.ext return filename.ext. Needed to compensate
353 * for filename skewing caused by the mod-versioning mechanism.
355 static const char *deskew(const char *basename
)
357 if (strncmp(basename
, SKEW_PREFIX
, sizeof(SKEW_PREFIX
) - 1) == 0)
358 return basename
+ sizeof(SKEW_PREFIX
) - 1;
363 * Create links to additional files (usually .c and .gcno files) which the
364 * gcov tool expects to find in the same directory as the gcov data file.
366 static void add_links(struct gcov_node
*node
, struct dentry
*parent
)
368 const char *basename
;
373 for (num
= 0; gcov_link
[num
].ext
; num
++)
375 node
->links
= kcalloc(num
, sizeof(struct dentry
*), GFP_KERNEL
);
378 for (i
= 0; i
< num
; i
++) {
379 target
= get_link_target(
380 gcov_info_filename(get_node_info(node
)),
384 basename
= kbasename(target
);
385 if (basename
== target
)
387 node
->links
[i
] = debugfs_create_symlink(deskew(basename
),
396 debugfs_remove(node
->links
[i
]);
401 static const struct file_operations gcov_data_fops
= {
402 .open
= gcov_seq_open
,
403 .release
= gcov_seq_release
,
406 .write
= gcov_seq_write
,
409 /* Basic initialization of a new node. */
410 static void init_node(struct gcov_node
*node
, struct gcov_info
*info
,
411 const char *name
, struct gcov_node
*parent
)
413 INIT_LIST_HEAD(&node
->list
);
414 INIT_LIST_HEAD(&node
->children
);
415 INIT_LIST_HEAD(&node
->all
);
416 if (node
->loaded_info
) {
417 node
->loaded_info
[0] = info
;
418 node
->num_loaded
= 1;
420 node
->parent
= parent
;
422 strcpy(node
->name
, name
);
426 * Create a new node and associated debugfs entry. Needs to be called with
429 static struct gcov_node
*new_node(struct gcov_node
*parent
,
430 struct gcov_info
*info
, const char *name
)
432 struct gcov_node
*node
;
434 node
= kzalloc(sizeof(struct gcov_node
) + strlen(name
) + 1, GFP_KERNEL
);
438 node
->loaded_info
= kcalloc(1, sizeof(struct gcov_info
*),
440 if (!node
->loaded_info
)
443 init_node(node
, info
, name
, parent
);
444 /* Differentiate between gcov data file nodes and directory nodes. */
446 node
->dentry
= debugfs_create_file(deskew(node
->name
), 0600,
447 parent
->dentry
, node
, &gcov_data_fops
);
449 node
->dentry
= debugfs_create_dir(node
->name
, parent
->dentry
);
451 add_links(node
, parent
->dentry
);
452 list_add(&node
->list
, &parent
->children
);
453 list_add(&node
->all
, &all_head
);
459 pr_warn("out of memory\n");
463 /* Remove symbolic links associated with node. */
464 static void remove_links(struct gcov_node
*node
)
470 for (i
= 0; gcov_link
[i
].ext
; i
++)
471 debugfs_remove(node
->links
[i
]);
477 * Remove node from all lists and debugfs and release associated resources.
478 * Needs to be called with node_lock held.
480 static void release_node(struct gcov_node
*node
)
482 list_del(&node
->list
);
483 list_del(&node
->all
);
484 debugfs_remove(node
->dentry
);
486 kfree(node
->loaded_info
);
487 if (node
->unloaded_info
)
488 gcov_info_free(node
->unloaded_info
);
492 /* Release node and empty parents. Needs to be called with node_lock held. */
493 static void remove_node(struct gcov_node
*node
)
495 struct gcov_node
*parent
;
497 while ((node
!= &root_node
) && list_empty(&node
->children
)) {
498 parent
= node
->parent
;
505 * Find child node with given basename. Needs to be called with node_lock
508 static struct gcov_node
*get_child_by_name(struct gcov_node
*parent
,
511 struct gcov_node
*node
;
513 list_for_each_entry(node
, &parent
->children
, list
) {
514 if (strcmp(node
->name
, name
) == 0)
522 * write() implementation for reset file. Reset all profiling data to zero
523 * and remove nodes for which all associated object files are unloaded.
525 static ssize_t
reset_write(struct file
*file
, const char __user
*addr
,
526 size_t len
, loff_t
*pos
)
528 struct gcov_node
*node
;
530 mutex_lock(&node_lock
);
532 list_for_each_entry(node
, &all_head
, all
) {
533 if (node
->num_loaded
> 0)
535 else if (list_empty(&node
->children
)) {
537 /* Several nodes may have gone - restart loop. */
541 mutex_unlock(&node_lock
);
546 /* read() implementation for reset file. Unused. */
547 static ssize_t
reset_read(struct file
*file
, char __user
*addr
, size_t len
,
550 /* Allow read operation so that a recursive copy won't fail. */
554 static const struct file_operations gcov_reset_fops
= {
555 .write
= reset_write
,
557 .llseek
= noop_llseek
,
561 * Create a node for a given profiling data set and add it to all lists and
562 * debugfs. Needs to be called with node_lock held.
564 static void add_node(struct gcov_info
*info
)
569 struct gcov_node
*parent
;
570 struct gcov_node
*node
;
572 filename
= kstrdup(gcov_info_filename(info
), GFP_KERNEL
);
576 /* Create directory nodes along the path. */
577 for (curr
= filename
; (next
= strchr(curr
, '/')); curr
= next
+ 1) {
581 if (strcmp(curr
, ".") == 0)
583 if (strcmp(curr
, "..") == 0) {
586 parent
= parent
->parent
;
589 node
= get_child_by_name(parent
, curr
);
591 node
= new_node(parent
, NULL
, curr
);
597 /* Create file node. */
598 node
= new_node(parent
, info
, curr
);
611 * Associate a profiling data set with an existing node. Needs to be called
612 * with node_lock held.
614 static void add_info(struct gcov_node
*node
, struct gcov_info
*info
)
616 struct gcov_info
**loaded_info
;
617 int num
= node
->num_loaded
;
620 * Prepare new array. This is done first to simplify cleanup in
621 * case the new data set is incompatible, the node only contains
622 * unloaded data sets and there's not enough memory for the array.
624 loaded_info
= kcalloc(num
+ 1, sizeof(struct gcov_info
*), GFP_KERNEL
);
626 pr_warn("could not add '%s' (out of memory)\n",
627 gcov_info_filename(info
));
630 memcpy(loaded_info
, node
->loaded_info
,
631 num
* sizeof(struct gcov_info
*));
632 loaded_info
[num
] = info
;
633 /* Check if the new data set is compatible. */
636 * A module was unloaded, modified and reloaded. The new
637 * data set replaces the copy of the last one.
639 if (!gcov_info_is_compatible(node
->unloaded_info
, info
)) {
640 pr_warn("discarding saved data for %s "
641 "(incompatible version)\n",
642 gcov_info_filename(info
));
643 gcov_info_free(node
->unloaded_info
);
644 node
->unloaded_info
= NULL
;
648 * Two different versions of the same object file are loaded.
649 * The initial one takes precedence.
651 if (!gcov_info_is_compatible(node
->loaded_info
[0], info
)) {
652 pr_warn("could not add '%s' (incompatible "
653 "version)\n", gcov_info_filename(info
));
658 /* Overwrite previous array. */
659 kfree(node
->loaded_info
);
660 node
->loaded_info
= loaded_info
;
661 node
->num_loaded
= num
+ 1;
665 * Return the index of a profiling data set associated with a node.
667 static int get_info_index(struct gcov_node
*node
, struct gcov_info
*info
)
671 for (i
= 0; i
< node
->num_loaded
; i
++) {
672 if (node
->loaded_info
[i
] == info
)
679 * Save the data of a profiling data set which is being unloaded.
681 static void save_info(struct gcov_node
*node
, struct gcov_info
*info
)
683 if (node
->unloaded_info
)
684 gcov_info_add(node
->unloaded_info
, info
);
686 node
->unloaded_info
= gcov_info_dup(info
);
687 if (!node
->unloaded_info
) {
688 pr_warn("could not save data for '%s' "
690 gcov_info_filename(info
));
696 * Disassociate a profiling data set from a node. Needs to be called with
699 static void remove_info(struct gcov_node
*node
, struct gcov_info
*info
)
703 i
= get_info_index(node
, info
);
705 pr_warn("could not remove '%s' (not found)\n",
706 gcov_info_filename(info
));
710 save_info(node
, info
);
712 node
->loaded_info
[i
] = node
->loaded_info
[node
->num_loaded
- 1];
714 if (node
->num_loaded
> 0)
716 /* Last loaded data set was removed. */
717 kfree(node
->loaded_info
);
718 node
->loaded_info
= NULL
;
719 node
->num_loaded
= 0;
720 if (!node
->unloaded_info
)
725 * Callback to create/remove profiling files when code compiled with
726 * -fprofile-arcs is loaded/unloaded.
728 void gcov_event(enum gcov_action action
, struct gcov_info
*info
)
730 struct gcov_node
*node
;
732 mutex_lock(&node_lock
);
733 node
= get_node_by_name(gcov_info_filename(info
));
737 add_info(node
, info
);
743 remove_info(node
, info
);
745 pr_warn("could not remove '%s' (not found)\n",
746 gcov_info_filename(info
));
750 mutex_unlock(&node_lock
);
753 /* Create debugfs entries. */
754 static __init
int gcov_fs_init(void)
756 init_node(&root_node
, NULL
, NULL
, NULL
);
758 * /sys/kernel/debug/gcov will be parent for the reset control file
759 * and all profiling files.
761 root_node
.dentry
= debugfs_create_dir("gcov", NULL
);
763 * Create reset file which resets all profiling counts when written
766 debugfs_create_file("reset", 0600, root_node
.dentry
, NULL
,
768 /* Replay previous events to get our fs hierarchy up-to-date. */
769 gcov_enable_events();
772 device_initcall(gcov_fs_init
);