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>
29 #include <linux/vmalloc.h>
33 * struct gcov_node - represents a debugfs entry
34 * @list: list head for child node list
35 * @children: child nodes
36 * @all: list head for list of all nodes
37 * @parent: parent node
38 * @loaded_info: array of pointers to profiling data sets for loaded object
40 * @num_loaded: number of profiling data sets for loaded object files.
41 * @unloaded_info: accumulated copy of profiling data sets for unloaded
42 * object files. Used only when gcov_persist=1.
43 * @dentry: main debugfs entry, either a directory or data file
44 * @links: associated symbolic links
45 * @name: data file basename
47 * struct gcov_node represents an entity within the gcov/ subdirectory
48 * of debugfs. There are directory and data file nodes. The latter represent
49 * the actual synthesized data file plus any associated symbolic links which
50 * are needed by the gcov tool to work correctly.
53 struct list_head list;
54 struct list_head children;
56 struct gcov_node *parent;
57 struct gcov_info **loaded_info;
58 struct gcov_info *unloaded_info;
59 struct dentry *dentry;
60 struct dentry **links;
65 static const char objtree[] = OBJTREE;
66 static const char srctree[] = SRCTREE;
67 static struct gcov_node root_node;
68 static LIST_HEAD(all_head);
69 static DEFINE_MUTEX(node_lock);
71 /* If non-zero, keep copies of profiling data for unloaded modules. */
72 static int gcov_persist = 1;
74 static int __init gcov_persist_setup(char *str)
78 if (kstrtoul(str, 0, &val)) {
79 pr_warn("invalid gcov_persist parameter '%s'\n", str);
83 pr_info("setting gcov_persist to %d\n", gcov_persist);
87 __setup("gcov_persist=", gcov_persist_setup);
89 #define ITER_STRIDE PAGE_SIZE
92 * struct gcov_iterator - specifies current file position in logical records
93 * @info: associated profiling data
94 * @buffer: buffer containing file data
95 * @size: size of buffer
96 * @pos: current position in file
98 struct gcov_iterator {
99 struct gcov_info *info;
106 * gcov_iter_new - allocate and initialize profiling data iterator
107 * @info: profiling data set to be iterated
109 * Return file iterator on success, %NULL otherwise.
111 static struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
113 struct gcov_iterator *iter;
115 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
120 /* Dry-run to get the actual buffer size. */
121 iter->size = convert_to_gcda(NULL, info);
122 iter->buffer = vmalloc(iter->size);
126 convert_to_gcda(iter->buffer, info);
137 * gcov_iter_free - free iterator data
138 * @iter: file iterator
140 static void gcov_iter_free(struct gcov_iterator *iter)
147 * gcov_iter_get_info - return profiling data set for given file iterator
148 * @iter: file iterator
150 static struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
156 * gcov_iter_start - reset file iterator to starting position
157 * @iter: file iterator
159 static void gcov_iter_start(struct gcov_iterator *iter)
165 * gcov_iter_next - advance file iterator to next logical record
166 * @iter: file iterator
168 * Return zero if new position is valid, non-zero if iterator has reached end.
170 static int gcov_iter_next(struct gcov_iterator *iter)
172 if (iter->pos < iter->size)
173 iter->pos += ITER_STRIDE;
175 if (iter->pos >= iter->size)
182 * gcov_iter_write - write data for current pos to seq_file
183 * @iter: file iterator
184 * @seq: seq_file handle
186 * Return zero on success, non-zero otherwise.
188 static int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
192 if (iter->pos >= iter->size)
196 if (iter->pos + len > iter->size)
197 len = iter->size - iter->pos;
199 seq_write(seq, iter->buffer + iter->pos, len);
205 * seq_file.start() implementation for gcov data files. Note that the
206 * gcov_iterator interface is designed to be more restrictive than seq_file
207 * (no start from arbitrary position, etc.), to simplify the iterator
210 static void *gcov_seq_start(struct seq_file *seq, loff_t *pos)
214 gcov_iter_start(seq->private);
215 for (i = 0; i < *pos; i++) {
216 if (gcov_iter_next(seq->private))
222 /* seq_file.next() implementation for gcov data files. */
223 static void *gcov_seq_next(struct seq_file *seq, void *data, loff_t *pos)
225 struct gcov_iterator *iter = data;
228 if (gcov_iter_next(iter))
234 /* seq_file.show() implementation for gcov data files. */
235 static int gcov_seq_show(struct seq_file *seq, void *data)
237 struct gcov_iterator *iter = data;
239 if (gcov_iter_write(iter, seq))
244 static void gcov_seq_stop(struct seq_file *seq, void *data)
249 static const struct seq_operations gcov_seq_ops = {
250 .start = gcov_seq_start,
251 .next = gcov_seq_next,
252 .show = gcov_seq_show,
253 .stop = gcov_seq_stop,
257 * Return a profiling data set associated with the given node. This is
258 * either a data set for a loaded object file or a data set copy in case
259 * all associated object files have been unloaded.
261 static struct gcov_info *get_node_info(struct gcov_node *node)
263 if (node->num_loaded > 0)
264 return node->loaded_info[0];
266 return node->unloaded_info;
270 * Return a newly allocated profiling data set which contains the sum of
271 * all profiling data associated with the given node.
273 static struct gcov_info *get_accumulated_info(struct gcov_node *node)
275 struct gcov_info *info;
278 if (node->unloaded_info)
279 info = gcov_info_dup(node->unloaded_info);
281 info = gcov_info_dup(node->loaded_info[i++]);
284 for (; i < node->num_loaded; i++)
285 gcov_info_add(info, node->loaded_info[i]);
291 * open() implementation for gcov data files. Create a copy of the profiling
292 * data set and initialize the iterator and seq_file interface.
294 static int gcov_seq_open(struct inode *inode, struct file *file)
296 struct gcov_node *node = inode->i_private;
297 struct gcov_iterator *iter;
298 struct seq_file *seq;
299 struct gcov_info *info;
302 mutex_lock(&node_lock);
304 * Read from a profiling data copy to minimize reference tracking
305 * complexity and concurrent access and to keep accumulating multiple
306 * profiling data sets associated with one node simple.
308 info = get_accumulated_info(node);
311 iter = gcov_iter_new(info);
314 rc = seq_open(file, &gcov_seq_ops);
316 goto err_free_iter_info;
317 seq = file->private_data;
320 mutex_unlock(&node_lock);
324 gcov_iter_free(iter);
326 gcov_info_free(info);
331 * release() implementation for gcov data files. Release resources allocated
334 static int gcov_seq_release(struct inode *inode, struct file *file)
336 struct gcov_iterator *iter;
337 struct gcov_info *info;
338 struct seq_file *seq;
340 seq = file->private_data;
342 info = gcov_iter_get_info(iter);
343 gcov_iter_free(iter);
344 gcov_info_free(info);
345 seq_release(inode, file);
351 * Find a node by the associated data file name. Needs to be called with
354 static struct gcov_node *get_node_by_name(const char *name)
356 struct gcov_node *node;
357 struct gcov_info *info;
359 list_for_each_entry(node, &all_head, all) {
360 info = get_node_info(node);
361 if (info && (strcmp(gcov_info_filename(info), name) == 0))
369 * Reset all profiling data associated with the specified node.
371 static void reset_node(struct gcov_node *node)
375 if (node->unloaded_info)
376 gcov_info_reset(node->unloaded_info);
377 for (i = 0; i < node->num_loaded; i++)
378 gcov_info_reset(node->loaded_info[i]);
381 static void remove_node(struct gcov_node *node);
384 * write() implementation for gcov data files. Reset profiling data for the
385 * corresponding file. If all associated object files have been unloaded,
386 * remove the debug fs node as well.
388 static ssize_t gcov_seq_write(struct file *file, const char __user *addr,
389 size_t len, loff_t *pos)
391 struct seq_file *seq;
392 struct gcov_info *info;
393 struct gcov_node *node;
395 seq = file->private_data;
396 info = gcov_iter_get_info(seq->private);
397 mutex_lock(&node_lock);
398 node = get_node_by_name(gcov_info_filename(info));
400 /* Reset counts or remove node for unloaded modules. */
401 if (node->num_loaded == 0)
406 /* Reset counts for open file. */
407 gcov_info_reset(info);
408 mutex_unlock(&node_lock);
414 * Given a string <path> representing a file path of format:
416 * construct and return a new string:
417 * <dir/>path/to/file.<ext>
419 static char *link_target(const char *dir, const char *path, const char *ext)
425 copy = kstrdup(path, GFP_KERNEL);
428 old_ext = strrchr(copy, '.');
432 target = kasprintf(GFP_KERNEL, "%s/%s.%s", dir, copy, ext);
434 target = kasprintf(GFP_KERNEL, "%s.%s", copy, ext);
441 * Construct a string representing the symbolic link target for the given
442 * gcov data file name and link type. Depending on the link type and the
443 * location of the data file, the link target can either point to a
444 * subdirectory of srctree, objtree or in an external location.
446 static char *get_link_target(const char *filename, const struct gcov_link *ext)
451 if (strncmp(filename, objtree, strlen(objtree)) == 0) {
452 rel = filename + strlen(objtree) + 1;
453 if (ext->dir == SRC_TREE)
454 result = link_target(srctree, rel, ext->ext);
456 result = link_target(objtree, rel, ext->ext);
458 /* External compilation. */
459 result = link_target(NULL, filename, ext->ext);
465 #define SKEW_PREFIX ".tmp_"
468 * For a filename .tmp_filename.ext return filename.ext. Needed to compensate
469 * for filename skewing caused by the mod-versioning mechanism.
471 static const char *deskew(const char *basename)
473 if (strncmp(basename, SKEW_PREFIX, sizeof(SKEW_PREFIX) - 1) == 0)
474 return basename + sizeof(SKEW_PREFIX) - 1;
479 * Create links to additional files (usually .c and .gcno files) which the
480 * gcov tool expects to find in the same directory as the gcov data file.
482 static void add_links(struct gcov_node *node, struct dentry *parent)
484 const char *basename;
489 for (num = 0; gcov_link[num].ext; num++)
491 node->links = kcalloc(num, sizeof(struct dentry *), GFP_KERNEL);
494 for (i = 0; i < num; i++) {
495 target = get_link_target(
496 gcov_info_filename(get_node_info(node)),
500 basename = kbasename(target);
501 if (basename == target)
503 node->links[i] = debugfs_create_symlink(deskew(basename),
512 debugfs_remove(node->links[i]);
517 static const struct file_operations gcov_data_fops = {
518 .open = gcov_seq_open,
519 .release = gcov_seq_release,
522 .write = gcov_seq_write,
525 /* Basic initialization of a new node. */
526 static void init_node(struct gcov_node *node, struct gcov_info *info,
527 const char *name, struct gcov_node *parent)
529 INIT_LIST_HEAD(&node->list);
530 INIT_LIST_HEAD(&node->children);
531 INIT_LIST_HEAD(&node->all);
532 if (node->loaded_info) {
533 node->loaded_info[0] = info;
534 node->num_loaded = 1;
536 node->parent = parent;
538 strcpy(node->name, name);
542 * Create a new node and associated debugfs entry. Needs to be called with
545 static struct gcov_node *new_node(struct gcov_node *parent,
546 struct gcov_info *info, const char *name)
548 struct gcov_node *node;
550 node = kzalloc(sizeof(struct gcov_node) + strlen(name) + 1, GFP_KERNEL);
554 node->loaded_info = kcalloc(1, sizeof(struct gcov_info *),
556 if (!node->loaded_info)
559 init_node(node, info, name, parent);
560 /* Differentiate between gcov data file nodes and directory nodes. */
562 node->dentry = debugfs_create_file(deskew(node->name), 0600,
563 parent->dentry, node, &gcov_data_fops);
565 node->dentry = debugfs_create_dir(node->name, parent->dentry);
567 add_links(node, parent->dentry);
568 list_add(&node->list, &parent->children);
569 list_add(&node->all, &all_head);
575 pr_warn("out of memory\n");
579 /* Remove symbolic links associated with node. */
580 static void remove_links(struct gcov_node *node)
586 for (i = 0; gcov_link[i].ext; i++)
587 debugfs_remove(node->links[i]);
593 * Remove node from all lists and debugfs and release associated resources.
594 * Needs to be called with node_lock held.
596 static void release_node(struct gcov_node *node)
598 list_del(&node->list);
599 list_del(&node->all);
600 debugfs_remove(node->dentry);
602 kfree(node->loaded_info);
603 if (node->unloaded_info)
604 gcov_info_free(node->unloaded_info);
608 /* Release node and empty parents. Needs to be called with node_lock held. */
609 static void remove_node(struct gcov_node *node)
611 struct gcov_node *parent;
613 while ((node != &root_node) && list_empty(&node->children)) {
614 parent = node->parent;
621 * Find child node with given basename. Needs to be called with node_lock
624 static struct gcov_node *get_child_by_name(struct gcov_node *parent,
627 struct gcov_node *node;
629 list_for_each_entry(node, &parent->children, list) {
630 if (strcmp(node->name, name) == 0)
638 * write() implementation for reset file. Reset all profiling data to zero
639 * and remove nodes for which all associated object files are unloaded.
641 static ssize_t reset_write(struct file *file, const char __user *addr,
642 size_t len, loff_t *pos)
644 struct gcov_node *node;
646 mutex_lock(&node_lock);
648 list_for_each_entry(node, &all_head, all) {
649 if (node->num_loaded > 0)
651 else if (list_empty(&node->children)) {
653 /* Several nodes may have gone - restart loop. */
657 mutex_unlock(&node_lock);
662 /* read() implementation for reset file. Unused. */
663 static ssize_t reset_read(struct file *file, char __user *addr, size_t len,
666 /* Allow read operation so that a recursive copy won't fail. */
670 static const struct file_operations gcov_reset_fops = {
671 .write = reset_write,
673 .llseek = noop_llseek,
677 * Create a node for a given profiling data set and add it to all lists and
678 * debugfs. Needs to be called with node_lock held.
680 static void add_node(struct gcov_info *info)
685 struct gcov_node *parent;
686 struct gcov_node *node;
688 filename = kstrdup(gcov_info_filename(info), GFP_KERNEL);
692 /* Create directory nodes along the path. */
693 for (curr = filename; (next = strchr(curr, '/')); curr = next + 1) {
697 if (strcmp(curr, ".") == 0)
699 if (strcmp(curr, "..") == 0) {
702 parent = parent->parent;
705 node = get_child_by_name(parent, curr);
707 node = new_node(parent, NULL, curr);
713 /* Create file node. */
714 node = new_node(parent, info, curr);
727 * Associate a profiling data set with an existing node. Needs to be called
728 * with node_lock held.
730 static void add_info(struct gcov_node *node, struct gcov_info *info)
732 struct gcov_info **loaded_info;
733 int num = node->num_loaded;
736 * Prepare new array. This is done first to simplify cleanup in
737 * case the new data set is incompatible, the node only contains
738 * unloaded data sets and there's not enough memory for the array.
740 loaded_info = kcalloc(num + 1, sizeof(struct gcov_info *), GFP_KERNEL);
742 pr_warn("could not add '%s' (out of memory)\n",
743 gcov_info_filename(info));
746 memcpy(loaded_info, node->loaded_info,
747 num * sizeof(struct gcov_info *));
748 loaded_info[num] = info;
749 /* Check if the new data set is compatible. */
752 * A module was unloaded, modified and reloaded. The new
753 * data set replaces the copy of the last one.
755 if (!gcov_info_is_compatible(node->unloaded_info, info)) {
756 pr_warn("discarding saved data for %s "
757 "(incompatible version)\n",
758 gcov_info_filename(info));
759 gcov_info_free(node->unloaded_info);
760 node->unloaded_info = NULL;
764 * Two different versions of the same object file are loaded.
765 * The initial one takes precedence.
767 if (!gcov_info_is_compatible(node->loaded_info[0], info)) {
768 pr_warn("could not add '%s' (incompatible "
769 "version)\n", gcov_info_filename(info));
774 /* Overwrite previous array. */
775 kfree(node->loaded_info);
776 node->loaded_info = loaded_info;
777 node->num_loaded = num + 1;
781 * Return the index of a profiling data set associated with a node.
783 static int get_info_index(struct gcov_node *node, struct gcov_info *info)
787 for (i = 0; i < node->num_loaded; i++) {
788 if (node->loaded_info[i] == info)
795 * Save the data of a profiling data set which is being unloaded.
797 static void save_info(struct gcov_node *node, struct gcov_info *info)
799 if (node->unloaded_info)
800 gcov_info_add(node->unloaded_info, info);
802 node->unloaded_info = gcov_info_dup(info);
803 if (!node->unloaded_info) {
804 pr_warn("could not save data for '%s' "
806 gcov_info_filename(info));
812 * Disassociate a profiling data set from a node. Needs to be called with
815 static void remove_info(struct gcov_node *node, struct gcov_info *info)
819 i = get_info_index(node, info);
821 pr_warn("could not remove '%s' (not found)\n",
822 gcov_info_filename(info));
826 save_info(node, info);
828 node->loaded_info[i] = node->loaded_info[node->num_loaded - 1];
830 if (node->num_loaded > 0)
832 /* Last loaded data set was removed. */
833 kfree(node->loaded_info);
834 node->loaded_info = NULL;
835 node->num_loaded = 0;
836 if (!node->unloaded_info)
841 * Callback to create/remove profiling files when code compiled with
842 * -fprofile-arcs is loaded/unloaded.
844 void gcov_event(enum gcov_action action, struct gcov_info *info)
846 struct gcov_node *node;
848 mutex_lock(&node_lock);
849 node = get_node_by_name(gcov_info_filename(info));
853 add_info(node, info);
859 remove_info(node, info);
861 pr_warn("could not remove '%s' (not found)\n",
862 gcov_info_filename(info));
866 mutex_unlock(&node_lock);
869 /* Create debugfs entries. */
870 static __init int gcov_fs_init(void)
872 init_node(&root_node, NULL, NULL, NULL);
874 * /sys/kernel/debug/gcov will be parent for the reset control file
875 * and all profiling files.
877 root_node.dentry = debugfs_create_dir("gcov", NULL);
879 * Create reset file which resets all profiling counts when written
882 debugfs_create_file("reset", 0600, root_node.dentry, NULL,
884 /* Replay previous events to get our fs hierarchy up-to-date. */
885 gcov_enable_events();
888 device_initcall(gcov_fs_init);