2 * This code exports profiling data as debugfs files to userspace.
4 * Copyright IBM Corp. 2009
5 * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
7 * Uses gcc-internal data definitions.
8 * Based on the gcov-kernel patch by:
9 * Hubertus Franke <frankeh@us.ibm.com>
10 * Nigel Hinds <nhinds@us.ibm.com>
11 * Rajan Ravindran <rajancr@us.ibm.com>
12 * Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
17 #define pr_fmt(fmt) "gcov: " fmt
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/debugfs.h>
23 #include <linux/list.h>
24 #include <linux/string.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/seq_file.h>
31 * struct gcov_node - represents a debugfs entry
32 * @list: list head for child node list
33 * @children: child nodes
34 * @all: list head for list of all nodes
35 * @parent: parent node
36 * @info: associated profiling data structure if not a directory
37 * @ghost: when an object file containing profiling data is unloaded we keep a
38 * copy of the profiling data here to allow collecting coverage data
39 * for cleanup code. Such a node is called a "ghost".
40 * @dentry: main debugfs entry, either a directory or data file
41 * @links: associated symbolic links
42 * @name: data file basename
44 * struct gcov_node represents an entity within the gcov/ subdirectory
45 * of debugfs. There are directory and data file nodes. The latter represent
46 * the actual synthesized data file plus any associated symbolic links which
47 * are needed by the gcov tool to work correctly.
50 struct list_head list
;
51 struct list_head children
;
53 struct gcov_node
*parent
;
54 struct gcov_info
*info
;
55 struct gcov_info
*ghost
;
56 struct dentry
*dentry
;
57 struct dentry
**links
;
61 static const char objtree
[] = OBJTREE
;
62 static const char srctree
[] = SRCTREE
;
63 static struct gcov_node root_node
;
64 static struct dentry
*reset_dentry
;
65 static LIST_HEAD(all_head
);
66 static DEFINE_MUTEX(node_lock
);
68 /* If non-zero, keep copies of profiling data for unloaded modules. */
69 static int gcov_persist
= 1;
71 static int __init
gcov_persist_setup(char *str
)
75 if (strict_strtoul(str
, 0, &val
)) {
76 pr_warning("invalid gcov_persist parameter '%s'\n", str
);
80 pr_info("setting gcov_persist to %d\n", gcov_persist
);
84 __setup("gcov_persist=", gcov_persist_setup
);
87 * seq_file.start() implementation for gcov data files. Note that the
88 * gcov_iterator interface is designed to be more restrictive than seq_file
89 * (no start from arbitrary position, etc.), to simplify the iterator
92 static void *gcov_seq_start(struct seq_file
*seq
, loff_t
*pos
)
96 gcov_iter_start(seq
->private);
97 for (i
= 0; i
< *pos
; i
++) {
98 if (gcov_iter_next(seq
->private))
104 /* seq_file.next() implementation for gcov data files. */
105 static void *gcov_seq_next(struct seq_file
*seq
, void *data
, loff_t
*pos
)
107 struct gcov_iterator
*iter
= data
;
109 if (gcov_iter_next(iter
))
116 /* seq_file.show() implementation for gcov data files. */
117 static int gcov_seq_show(struct seq_file
*seq
, void *data
)
119 struct gcov_iterator
*iter
= data
;
121 if (gcov_iter_write(iter
, seq
))
126 static void gcov_seq_stop(struct seq_file
*seq
, void *data
)
131 static const struct seq_operations gcov_seq_ops
= {
132 .start
= gcov_seq_start
,
133 .next
= gcov_seq_next
,
134 .show
= gcov_seq_show
,
135 .stop
= gcov_seq_stop
,
139 * Return the profiling data set for a given node. This can either be the
140 * original profiling data structure or a duplicate (also called "ghost")
141 * in case the associated object file has been unloaded.
143 static struct gcov_info
*get_node_info(struct gcov_node
*node
)
152 * open() implementation for gcov data files. Create a copy of the profiling
153 * data set and initialize the iterator and seq_file interface.
155 static int gcov_seq_open(struct inode
*inode
, struct file
*file
)
157 struct gcov_node
*node
= inode
->i_private
;
158 struct gcov_iterator
*iter
;
159 struct seq_file
*seq
;
160 struct gcov_info
*info
;
163 mutex_lock(&node_lock
);
165 * Read from a profiling data copy to minimize reference tracking
166 * complexity and concurrent access.
168 info
= gcov_info_dup(get_node_info(node
));
171 iter
= gcov_iter_new(info
);
174 rc
= seq_open(file
, &gcov_seq_ops
);
176 goto err_free_iter_info
;
177 seq
= file
->private_data
;
180 mutex_unlock(&node_lock
);
184 gcov_iter_free(iter
);
186 gcov_info_free(info
);
191 * release() implementation for gcov data files. Release resources allocated
194 static int gcov_seq_release(struct inode
*inode
, struct file
*file
)
196 struct gcov_iterator
*iter
;
197 struct gcov_info
*info
;
198 struct seq_file
*seq
;
200 seq
= file
->private_data
;
202 info
= gcov_iter_get_info(iter
);
203 gcov_iter_free(iter
);
204 gcov_info_free(info
);
205 seq_release(inode
, file
);
211 * Find a node by the associated data file name. Needs to be called with
214 static struct gcov_node
*get_node_by_name(const char *name
)
216 struct gcov_node
*node
;
217 struct gcov_info
*info
;
219 list_for_each_entry(node
, &all_head
, all
) {
220 info
= get_node_info(node
);
221 if (info
&& (strcmp(info
->filename
, name
) == 0))
228 static void remove_node(struct gcov_node
*node
);
231 * write() implementation for gcov data files. Reset profiling data for the
232 * associated file. If the object file has been unloaded (i.e. this is
233 * a "ghost" node), remove the debug fs node as well.
235 static ssize_t
gcov_seq_write(struct file
*file
, const char __user
*addr
,
236 size_t len
, loff_t
*pos
)
238 struct seq_file
*seq
;
239 struct gcov_info
*info
;
240 struct gcov_node
*node
;
242 seq
= file
->private_data
;
243 info
= gcov_iter_get_info(seq
->private);
244 mutex_lock(&node_lock
);
245 node
= get_node_by_name(info
->filename
);
247 /* Reset counts or remove node for unloaded modules. */
251 gcov_info_reset(node
->info
);
253 /* Reset counts for open file. */
254 gcov_info_reset(info
);
255 mutex_unlock(&node_lock
);
261 * Given a string <path> representing a file path of format:
263 * construct and return a new string:
264 * <dir/>path/to/file.<ext>
266 static char *link_target(const char *dir
, const char *path
, const char *ext
)
272 copy
= kstrdup(path
, GFP_KERNEL
);
275 old_ext
= strrchr(copy
, '.');
279 target
= kasprintf(GFP_KERNEL
, "%s/%s.%s", dir
, copy
, ext
);
281 target
= kasprintf(GFP_KERNEL
, "%s.%s", copy
, ext
);
288 * Construct a string representing the symbolic link target for the given
289 * gcov data file name and link type. Depending on the link type and the
290 * location of the data file, the link target can either point to a
291 * subdirectory of srctree, objtree or in an external location.
293 static char *get_link_target(const char *filename
, const struct gcov_link
*ext
)
298 if (strncmp(filename
, objtree
, strlen(objtree
)) == 0) {
299 rel
= filename
+ strlen(objtree
) + 1;
300 if (ext
->dir
== SRC_TREE
)
301 result
= link_target(srctree
, rel
, ext
->ext
);
303 result
= link_target(objtree
, rel
, ext
->ext
);
305 /* External compilation. */
306 result
= link_target(NULL
, filename
, ext
->ext
);
312 #define SKEW_PREFIX ".tmp_"
315 * For a filename .tmp_filename.ext return filename.ext. Needed to compensate
316 * for filename skewing caused by the mod-versioning mechanism.
318 static const char *deskew(const char *basename
)
320 if (strncmp(basename
, SKEW_PREFIX
, sizeof(SKEW_PREFIX
) - 1) == 0)
321 return basename
+ sizeof(SKEW_PREFIX
) - 1;
326 * Create links to additional files (usually .c and .gcno files) which the
327 * gcov tool expects to find in the same directory as the gcov data file.
329 static void add_links(struct gcov_node
*node
, struct dentry
*parent
)
336 for (num
= 0; gcov_link
[num
].ext
; num
++)
338 node
->links
= kcalloc(num
, sizeof(struct dentry
*), GFP_KERNEL
);
341 for (i
= 0; i
< num
; i
++) {
342 target
= get_link_target(get_node_info(node
)->filename
,
346 basename
= strrchr(target
, '/');
350 node
->links
[i
] = debugfs_create_symlink(deskew(basename
),
361 debugfs_remove(node
->links
[i
]);
366 static const struct file_operations gcov_data_fops
= {
367 .open
= gcov_seq_open
,
368 .release
= gcov_seq_release
,
371 .write
= gcov_seq_write
,
374 /* Basic initialization of a new node. */
375 static void init_node(struct gcov_node
*node
, struct gcov_info
*info
,
376 const char *name
, struct gcov_node
*parent
)
378 INIT_LIST_HEAD(&node
->list
);
379 INIT_LIST_HEAD(&node
->children
);
380 INIT_LIST_HEAD(&node
->all
);
382 node
->parent
= parent
;
384 strcpy(node
->name
, name
);
388 * Create a new node and associated debugfs entry. Needs to be called with
391 static struct gcov_node
*new_node(struct gcov_node
*parent
,
392 struct gcov_info
*info
, const char *name
)
394 struct gcov_node
*node
;
396 node
= kzalloc(sizeof(struct gcov_node
) + strlen(name
) + 1, GFP_KERNEL
);
398 pr_warning("out of memory\n");
401 init_node(node
, info
, name
, parent
);
402 /* Differentiate between gcov data file nodes and directory nodes. */
404 node
->dentry
= debugfs_create_file(deskew(node
->name
), 0600,
405 parent
->dentry
, node
, &gcov_data_fops
);
407 node
->dentry
= debugfs_create_dir(node
->name
, parent
->dentry
);
409 pr_warning("could not create file\n");
414 add_links(node
, parent
->dentry
);
415 list_add(&node
->list
, &parent
->children
);
416 list_add(&node
->all
, &all_head
);
421 /* Remove symbolic links associated with node. */
422 static void remove_links(struct gcov_node
*node
)
428 for (i
= 0; gcov_link
[i
].ext
; i
++)
429 debugfs_remove(node
->links
[i
]);
435 * Remove node from all lists and debugfs and release associated resources.
436 * Needs to be called with node_lock held.
438 static void release_node(struct gcov_node
*node
)
440 list_del(&node
->list
);
441 list_del(&node
->all
);
442 debugfs_remove(node
->dentry
);
445 gcov_info_free(node
->ghost
);
449 /* Release node and empty parents. Needs to be called with node_lock held. */
450 static void remove_node(struct gcov_node
*node
)
452 struct gcov_node
*parent
;
454 while ((node
!= &root_node
) && list_empty(&node
->children
)) {
455 parent
= node
->parent
;
462 * Find child node with given basename. Needs to be called with node_lock
465 static struct gcov_node
*get_child_by_name(struct gcov_node
*parent
,
468 struct gcov_node
*node
;
470 list_for_each_entry(node
, &parent
->children
, list
) {
471 if (strcmp(node
->name
, name
) == 0)
479 * write() implementation for reset file. Reset all profiling data to zero
480 * and remove ghost nodes.
482 static ssize_t
reset_write(struct file
*file
, const char __user
*addr
,
483 size_t len
, loff_t
*pos
)
485 struct gcov_node
*node
;
487 mutex_lock(&node_lock
);
489 list_for_each_entry(node
, &all_head
, all
) {
491 gcov_info_reset(node
->info
);
492 else if (list_empty(&node
->children
)) {
494 /* Several nodes may have gone - restart loop. */
498 mutex_unlock(&node_lock
);
503 /* read() implementation for reset file. Unused. */
504 static ssize_t
reset_read(struct file
*file
, char __user
*addr
, size_t len
,
507 /* Allow read operation so that a recursive copy won't fail. */
511 static const struct file_operations gcov_reset_fops
= {
512 .write
= reset_write
,
517 * Create a node for a given profiling data set and add it to all lists and
518 * debugfs. Needs to be called with node_lock held.
520 static void add_node(struct gcov_info
*info
)
525 struct gcov_node
*parent
;
526 struct gcov_node
*node
;
528 filename
= kstrdup(info
->filename
, GFP_KERNEL
);
532 /* Create directory nodes along the path. */
533 for (curr
= filename
; (next
= strchr(curr
, '/')); curr
= next
+ 1) {
537 if (strcmp(curr
, ".") == 0)
539 if (strcmp(curr
, "..") == 0) {
542 parent
= parent
->parent
;
545 node
= get_child_by_name(parent
, curr
);
547 node
= new_node(parent
, NULL
, curr
);
553 /* Create file node. */
554 node
= new_node(parent
, info
, curr
);
567 * The profiling data set associated with this node is being unloaded. Store a
568 * copy of the profiling data and turn this node into a "ghost".
570 static int ghost_node(struct gcov_node
*node
)
572 node
->ghost
= gcov_info_dup(node
->info
);
574 pr_warning("could not save data for '%s' (out of memory)\n",
575 node
->info
->filename
);
584 * Profiling data for this node has been loaded again. Add profiling data
585 * from previous instantiation and turn this node into a regular node.
587 static void revive_node(struct gcov_node
*node
, struct gcov_info
*info
)
589 if (gcov_info_is_compatible(node
->ghost
, info
))
590 gcov_info_add(info
, node
->ghost
);
592 pr_warning("discarding saved data for '%s' (version changed)\n",
595 gcov_info_free(node
->ghost
);
601 * Callback to create/remove profiling files when code compiled with
602 * -fprofile-arcs is loaded/unloaded.
604 void gcov_event(enum gcov_action action
, struct gcov_info
*info
)
606 struct gcov_node
*node
;
608 mutex_lock(&node_lock
);
609 node
= get_node_by_name(info
->filename
);
612 /* Add new node or revive ghost. */
618 revive_node(node
, info
);
620 pr_warning("could not add '%s' (already exists)\n",
625 /* Remove node or turn into ghost. */
627 pr_warning("could not remove '%s' (not found)\n",
632 if (!ghost_node(node
))
638 mutex_unlock(&node_lock
);
641 /* Create debugfs entries. */
642 static __init
int gcov_fs_init(void)
646 init_node(&root_node
, NULL
, NULL
, NULL
);
648 * /sys/kernel/debug/gcov will be parent for the reset control file
649 * and all profiling files.
651 root_node
.dentry
= debugfs_create_dir("gcov", NULL
);
652 if (!root_node
.dentry
)
655 * Create reset file which resets all profiling counts when written
658 reset_dentry
= debugfs_create_file("reset", 0600, root_node
.dentry
,
659 NULL
, &gcov_reset_fops
);
662 /* Replay previous events to get our fs hierarchy up-to-date. */
663 gcov_enable_events();
667 pr_err("init failed\n");
668 if (root_node
.dentry
)
669 debugfs_remove(root_node
.dentry
);
673 device_initcall(gcov_fs_init
);