1 // SPDX-License-Identifier: GPL-2.0
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; version 2.
6 // This program is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // GNU General Public License for more details.
11 // You should have received a copy of the GNU General Public License
12 // along with this program; if not, write to the Free Software
13 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * This code exports profiling data as debugfs files to userspace.
18 * Copyright IBM Corp. 2009
19 * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
21 * Uses gcc-internal data definitions.
22 * Based on the gcov-kernel patch by:
23 * Hubertus Franke <frankeh@us.ibm.com>
24 * Nigel Hinds <nhinds@us.ibm.com>
25 * Rajan Ravindran <rajancr@us.ibm.com>
26 * Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/linker.h>
42 #include <sys/module.h>
43 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/syslog.h>
48 #include <sys/sched.h>
49 #include <sys/syslog.h>
50 #include <sys/sysctl.h>
51 #include <linux/debugfs.h>
53 #include <gnu/gcov/gcov.h>
54 #include <sys/queue.h>
56 extern int gcov_events_enabled;
57 static int gcov_persist;
58 static struct mtx gcov_mtx;
59 MTX_SYSINIT(gcov_init, &gcov_mtx, "gcov_mtx", MTX_DEF);
60 MALLOC_DEFINE(M_GCOV, "gcov", "gcov");
62 void __gcov_init(struct gcov_info *info);
63 void __gcov_flush(void);
64 void __gcov_merge_add(gcov_type *counters, unsigned int n_counters);
65 void __gcov_merge_single(gcov_type *counters, unsigned int n_counters);
66 void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters);
67 void __gcov_merge_ior(gcov_type *counters, unsigned int n_counters);
68 void __gcov_merge_time_profile(gcov_type *counters, unsigned int n_counters);
69 void __gcov_merge_icall_topn(gcov_type *counters, unsigned int n_counters);
70 void __gcov_exit(void);
72 static void gcov_event(enum gcov_action action, struct gcov_info *info);
76 * Private copy taken from libc
79 (basename)(char *path)
84 * If path is a null pointer or points to an empty string,
85 * basename() shall return a pointer to the string ".".
87 if (path == NULL || *path == '\0')
88 return (__DECONST(char *, "."));
90 /* Find end of last pathname component and null terminate it. */
91 ptr = path + strlen(path);
92 while (ptr > path + 1 && *(ptr - 1) == '/')
96 /* Find beginning of last pathname component. */
97 while (ptr > path && *(ptr - 1) != '/')
103 * __gcov_init is called by gcc-generated constructor code for each object
104 * file compiled with -fprofile-arcs.
107 __gcov_init(struct gcov_info *info)
109 static unsigned int gcov_version;
112 if (gcov_version == 0) {
113 gcov_version = gcov_info_version(info);
115 * Printing gcc's version magic may prove useful for debugging
116 * incompatibility reports.
118 log(LOG_INFO, "version magic: 0x%x\n", gcov_version);
121 * Add new profiling data structure to list and inform event
124 gcov_info_link(info);
125 if (gcov_events_enabled)
126 gcov_event(GCOV_ADD, info);
127 mtx_unlock(&gcov_mtx);
131 * These functions may be referenced by gcc-generated profiling code but serve
132 * no function for kernel profiling.
141 __gcov_merge_add(gcov_type *counters, unsigned int n_counters)
147 __gcov_merge_single(gcov_type *counters, unsigned int n_counters)
153 __gcov_merge_delta(gcov_type *counters, unsigned int n_counters)
159 __gcov_merge_ior(gcov_type *counters, unsigned int n_counters)
165 __gcov_merge_time_profile(gcov_type *counters, unsigned int n_counters)
171 __gcov_merge_icall_topn(gcov_type *counters, unsigned int n_counters)
184 * struct gcov_node - represents a debugfs entry
185 * @entry: list entry for parent's child node list
186 * @children: child nodes
187 * @all_entry: list entry for list of all nodes
188 * @parent: parent node
189 * @loaded_info: array of pointers to profiling data sets for loaded object
191 * @num_loaded: number of profiling data sets for loaded object files.
192 * @unloaded_info: accumulated copy of profiling data sets for unloaded
193 * object files. Used only when gcov_persist=1.
194 * @dentry: main debugfs entry, either a directory or data file
195 * @links: associated symbolic links
196 * @name: data file basename
198 * struct gcov_node represents an entity within the gcov/ subdirectory
199 * of debugfs. There are directory and data file nodes. The latter represent
200 * the actual synthesized data file plus any associated symbolic links which
201 * are needed by the gcov tool to work correctly.
204 LIST_ENTRY(gcov_node) children_entry;
205 LIST_ENTRY(gcov_node) all_entry;
207 struct gcov_node *lh_first;
209 struct gcov_node *parent;
210 struct gcov_info **loaded_info;
211 struct gcov_info *unloaded_info;
212 struct dentry *dentry;
213 struct dentry **links;
219 static const char objtree[] = OBJTREE;
220 static const char srctree[] = SRCTREE;
222 static const char objtree[] = "";
223 static const char srctree[] = "";
225 static struct gcov_node root_node;
227 struct gcov_node *lh_first;
229 static struct mtx node_lock;
230 MTX_SYSINIT(node_init, &node_lock, "node_lock", MTX_DEF);
231 static void remove_node(struct gcov_node *node);
234 * seq_file.start() implementation for gcov data files. Note that the
235 * gcov_iterator interface is designed to be more restrictive than seq_file
236 * (no start from arbitrary position, etc.), to simplify the iterator
240 gcov_seq_start(struct seq_file *seq, off_t *pos)
244 gcov_iter_start(seq->private);
245 for (i = 0; i < *pos; i++) {
246 if (gcov_iter_next(seq->private))
252 /* seq_file.next() implementation for gcov data files. */
254 gcov_seq_next(struct seq_file *seq, void *data, off_t *pos)
256 struct gcov_iterator *iter = data;
258 if (gcov_iter_next(iter))
265 /* seq_file.show() implementation for gcov data files. */
267 gcov_seq_show(struct seq_file *seq, void *data)
269 struct gcov_iterator *iter = data;
271 if (gcov_iter_write(iter, seq->buf))
277 gcov_seq_stop(struct seq_file *seq, void *data)
282 static const struct seq_operations gcov_seq_ops = {
283 .start = gcov_seq_start,
284 .next = gcov_seq_next,
285 .show = gcov_seq_show,
286 .stop = gcov_seq_stop,
290 * Return a profiling data set associated with the given node. This is
291 * either a data set for a loaded object file or a data set copy in case
292 * all associated object files have been unloaded.
294 static struct gcov_info *
295 get_node_info(struct gcov_node *node)
297 if (node->num_loaded > 0)
298 return (node->loaded_info[0]);
300 return (node->unloaded_info);
304 * Return a newly allocated profiling data set which contains the sum of
305 * all profiling data associated with the given node.
307 static struct gcov_info *
308 get_accumulated_info(struct gcov_node *node)
310 struct gcov_info *info;
313 if (node->unloaded_info)
314 info = gcov_info_dup(node->unloaded_info);
316 info = gcov_info_dup(node->loaded_info[i++]);
319 for (; i < node->num_loaded; i++)
320 gcov_info_add(info, node->loaded_info[i]);
326 * open() implementation for gcov data files. Create a copy of the profiling
327 * data set and initialize the iterator and seq_file interface.
330 gcov_seq_open(struct inode *inode, struct file *file)
332 struct gcov_node *node = inode->i_private;
333 struct gcov_iterator *iter;
334 struct seq_file *seq;
335 struct gcov_info *info;
338 mtx_lock(&node_lock);
340 * Read from a profiling data copy to minimize reference tracking
341 * complexity and concurrent access and to keep accumulating multiple
342 * profiling data sets associated with one node simple.
344 info = get_accumulated_info(node);
347 iter = gcov_iter_new(info);
350 rc = seq_open(file, &gcov_seq_ops);
352 goto err_free_iter_info;
353 seq = file->private_data;
356 mtx_unlock(&node_lock);
360 gcov_iter_free(iter);
362 gcov_info_free(info);
367 * release() implementation for gcov data files. Release resources allocated
371 gcov_seq_release(struct inode *inode, struct file *file)
373 struct gcov_iterator *iter;
374 struct gcov_info *info;
375 struct seq_file *seq;
377 seq = file->private_data;
379 info = gcov_iter_get_info(iter);
380 gcov_iter_free(iter);
381 gcov_info_free(info);
382 seq_release(inode, file);
388 * Find a node by the associated data file name. Needs to be called with
391 static struct gcov_node *
392 get_node_by_name(const char *name)
394 struct gcov_node *node;
395 struct gcov_info *info;
397 LIST_FOREACH(node, &all_head, all_entry) {
398 info = get_node_info(node);
399 if (info && (strcmp(gcov_info_filename(info), name) == 0))
407 * Reset all profiling data associated with the specified node.
410 reset_node(struct gcov_node *node)
414 if (node->unloaded_info)
415 gcov_info_reset(node->unloaded_info);
416 for (i = 0; i < node->num_loaded; i++)
417 gcov_info_reset(node->loaded_info[i]);
421 gcov_stats_reset(void)
423 struct gcov_node *node;
425 mtx_lock(&node_lock);
427 LIST_FOREACH(node, &all_head, all_entry) {
428 if (node->num_loaded > 0)
430 else if (LIST_EMPTY(&node->children)) {
435 mtx_unlock(&node_lock);
439 * write() implementation for gcov data files. Reset profiling data for the
440 * corresponding file. If all associated object files have been unloaded,
441 * remove the debug fs node as well.
444 gcov_seq_write(struct file *file, const char *addr, size_t len, off_t *pos)
446 struct seq_file *seq;
447 struct gcov_info *info;
448 struct gcov_node *node;
450 seq = file->private_data;
451 info = gcov_iter_get_info(seq->private);
452 mtx_lock(&node_lock);
453 node = get_node_by_name(gcov_info_filename(info));
455 /* Reset counts or remove node for unloaded modules. */
456 if (node->num_loaded == 0)
461 /* Reset counts for open file. */
462 gcov_info_reset(info);
463 mtx_unlock(&node_lock);
469 * Given a string <path> representing a file path of format:
471 * construct and return a new string:
472 * <dir/>path/to/file.<ext>
475 link_target(const char *dir, const char *path, const char *ext)
481 copy = strdup_flags(path, M_GCOV, M_NOWAIT);
484 old_ext = strrchr(copy, '.');
489 asprintf(&target, M_GCOV, "%s/%s.%s", dir, copy, ext);
491 asprintf(&target, M_GCOV, "%s.%s", copy, ext);
498 * Construct a string representing the symbolic link target for the given
499 * gcov data file name and link type. Depending on the link type and the
500 * location of the data file, the link target can either point to a
501 * subdirectory of srctree, objtree or in an external location.
504 get_link_target(const char *filename, const struct gcov_link *ext)
509 if (strncmp(filename, objtree, strlen(objtree)) == 0) {
510 rel = filename + strlen(objtree) + 1;
511 if (ext->dir == SRC_TREE)
512 result = link_target(srctree, rel, ext->ext);
514 result = link_target(objtree, rel, ext->ext);
516 /* External compilation. */
517 result = link_target(NULL, filename, ext->ext);
523 #define SKEW_PREFIX ".tmp_"
526 * For a filename .tmp_filename.ext return filename.ext. Needed to compensate
527 * for filename skewing caused by the mod-versioning mechanism.
530 deskew(const char *basename)
532 if (strncmp(basename, SKEW_PREFIX, sizeof(SKEW_PREFIX) - 1) == 0)
533 return (basename + sizeof(SKEW_PREFIX) - 1);
538 * Create links to additional files (usually .c and .gcno files) which the
539 * gcov tool expects to find in the same directory as the gcov data file.
542 add_links(struct gcov_node *node, struct dentry *parent)
544 const char *path_basename;
549 for (num = 0; gcov_link[num].ext; num++)
551 node->links = malloc((num*sizeof(struct dentry *)), M_GCOV, M_NOWAIT|M_ZERO);
552 if (node->links == NULL)
554 for (i = 0; i < num; i++) {
555 target = get_link_target(
556 gcov_info_filename(get_node_info(node)),
560 path_basename = basename(target);
561 if (path_basename == target)
563 node->links[i] = debugfs_create_symlink(deskew(path_basename),
567 free(target, M_GCOV);
572 free(target, M_GCOV);
574 debugfs_remove(node->links[i]);
575 free(node->links, M_GCOV);
579 static const struct file_operations gcov_data_fops = {
580 .open = gcov_seq_open,
581 .release = gcov_seq_release,
584 .write = gcov_seq_write,
587 /* Basic initialization of a new node. */
589 init_node(struct gcov_node *node, struct gcov_info *info,
590 const char *name, struct gcov_node *parent)
592 LIST_INIT(&node->children);
593 if (node->loaded_info) {
594 node->loaded_info[0] = info;
595 node->num_loaded = 1;
597 node->parent = parent;
599 strcpy(node->name, name);
603 * Create a new node and associated debugfs entry. Needs to be called with
606 static struct gcov_node *
607 new_node(struct gcov_node *parent, struct gcov_info *info, const char *name)
609 struct gcov_node *node;
611 node = malloc(sizeof(struct gcov_node) + strlen(name) + 1, M_GCOV, M_NOWAIT|M_ZERO);
615 node->loaded_info = malloc(sizeof(struct gcov_info *), M_GCOV, M_NOWAIT|M_ZERO);
616 if (!node->loaded_info)
619 init_node(node, info, name, parent);
620 /* Differentiate between gcov data file nodes and directory nodes. */
622 node->dentry = debugfs_create_file(deskew(node->name), 0600,
623 parent->dentry, node, &gcov_data_fops);
625 node->dentry = debugfs_create_dir(node->name, parent->dentry);
627 log(LOG_WARNING, "could not create file\n");
632 add_links(node, parent->dentry);
633 LIST_INSERT_HEAD(&parent->children, node, children_entry);
634 LIST_INSERT_HEAD(&all_head, node, all_entry);
640 log(LOG_WARNING, "out of memory\n");
644 /* Remove symbolic links associated with node. */
646 remove_links(struct gcov_node *node)
649 if (node->links == NULL)
651 for (int i = 0; gcov_link[i].ext; i++)
652 debugfs_remove(node->links[i]);
653 free(node->links, M_GCOV);
658 * Remove node from all lists and debugfs and release associated resources.
659 * Needs to be called with node_lock held.
662 release_node(struct gcov_node *node)
664 LIST_REMOVE(node, children_entry);
665 LIST_REMOVE(node, all_entry);
666 debugfs_remove(node->dentry);
668 free(node->loaded_info, M_GCOV);
669 if (node->unloaded_info)
670 gcov_info_free(node->unloaded_info);
674 /* Release node and empty parents. Needs to be called with node_lock held. */
676 remove_node(struct gcov_node *node)
678 struct gcov_node *parent;
680 while ((node != &root_node) && LIST_EMPTY(&node->children)) {
681 parent = node->parent;
688 * Find child node with given basename. Needs to be called with node_lock
691 static struct gcov_node *
692 get_child_by_name(struct gcov_node *parent, const char *name)
694 struct gcov_node *node;
696 LIST_FOREACH(node, &parent->children, children_entry) {
697 if (strcmp(node->name, name) == 0)
705 * Create a node for a given profiling data set and add it to all lists and
706 * debugfs. Needs to be called with node_lock held.
709 add_node(struct gcov_info *info)
714 struct gcov_node *parent;
715 struct gcov_node *node;
717 filename = strdup_flags(gcov_info_filename(info), M_GCOV, M_NOWAIT);
718 if (filename == NULL)
721 /* Create directory nodes along the path. */
722 for (curr = filename; (next = strchr(curr, '/')); curr = next + 1) {
726 if (strcmp(curr, ".") == 0)
728 if (strcmp(curr, "..") == 0) {
731 parent = parent->parent;
734 node = get_child_by_name(parent, curr);
736 node = new_node(parent, NULL, curr);
742 /* Create file node. */
743 node = new_node(parent, info, curr);
747 free(filename, M_GCOV);
756 * Associate a profiling data set with an existing node. Needs to be called
757 * with node_lock held.
760 add_info(struct gcov_node *node, struct gcov_info *info)
762 struct gcov_info **loaded_info;
763 int num = node->num_loaded;
766 * Prepare new array. This is done first to simplify cleanup in
767 * case the new data set is incompatible, the node only contains
768 * unloaded data sets and there's not enough memory for the array.
770 loaded_info = malloc((num + 1)* sizeof(struct gcov_info *), M_GCOV, M_NOWAIT|M_ZERO);
772 log(LOG_WARNING, "could not add '%s' (out of memory)\n",
773 gcov_info_filename(info));
776 memcpy(loaded_info, node->loaded_info,
777 num * sizeof(struct gcov_info *));
778 loaded_info[num] = info;
779 /* Check if the new data set is compatible. */
782 * A module was unloaded, modified and reloaded. The new
783 * data set replaces the copy of the last one.
785 if (!gcov_info_is_compatible(node->unloaded_info, info)) {
786 log(LOG_WARNING, "discarding saved data for %s "
787 "(incompatible version)\n",
788 gcov_info_filename(info));
789 gcov_info_free(node->unloaded_info);
790 node->unloaded_info = NULL;
794 * Two different versions of the same object file are loaded.
795 * The initial one takes precedence.
797 if (!gcov_info_is_compatible(node->loaded_info[0], info)) {
798 log(LOG_WARNING, "could not add '%s' (incompatible "
799 "version)\n", gcov_info_filename(info));
800 free(loaded_info, M_GCOV);
804 /* Overwrite previous array. */
805 free(node->loaded_info, M_GCOV);
806 node->loaded_info = loaded_info;
807 node->num_loaded = num + 1;
811 * Return the index of a profiling data set associated with a node.
814 get_info_index(struct gcov_node *node, struct gcov_info *info)
818 for (i = 0; i < node->num_loaded; i++) {
819 if (node->loaded_info[i] == info)
826 * Save the data of a profiling data set which is being unloaded.
829 save_info(struct gcov_node *node, struct gcov_info *info)
831 if (node->unloaded_info)
832 gcov_info_add(node->unloaded_info, info);
834 node->unloaded_info = gcov_info_dup(info);
835 if (!node->unloaded_info) {
836 log(LOG_WARNING, "could not save data for '%s' "
838 gcov_info_filename(info));
844 * Disassociate a profiling data set from a node. Needs to be called with
848 remove_info(struct gcov_node *node, struct gcov_info *info)
852 i = get_info_index(node, info);
854 log(LOG_WARNING, "could not remove '%s' (not found)\n",
855 gcov_info_filename(info));
859 save_info(node, info);
861 node->loaded_info[i] = node->loaded_info[node->num_loaded - 1];
863 if (node->num_loaded > 0)
865 /* Last loaded data set was removed. */
866 free(node->loaded_info, M_GCOV);
867 node->loaded_info = NULL;
868 node->num_loaded = 0;
869 if (!node->unloaded_info)
874 * Callback to create/remove profiling files when code compiled with
875 * -fprofile-arcs is loaded/unloaded.
878 gcov_event(enum gcov_action action, struct gcov_info *info)
880 struct gcov_node *node;
882 mtx_lock(&node_lock);
883 node = get_node_by_name(gcov_info_filename(info));
887 add_info(node, info);
893 remove_info(node, info);
895 log(LOG_WARNING, "could not remove '%s' (not found)\n",
896 gcov_info_filename(info));
900 mtx_unlock(&node_lock);
904 * gcov_enable_events - enable event reporting through gcov_event()
906 * Turn on reporting of profiling data load/unload-events through the
907 * gcov_event() callback. Also replay all previous events once. This function
908 * is needed because some events are potentially generated too early for the
909 * callback implementation to handle them initially.
912 gcov_enable_events(void)
914 struct gcov_info *info = NULL;
920 /* Perform event callback for previously registered entries. */
921 while ((info = gcov_info_next(info))) {
922 gcov_event(GCOV_ADD, info);
923 sched_relinquish(curthread);
927 mtx_unlock(&gcov_mtx);
928 printf("%s found %d events\n", __func__, count);
931 /* Update list and generate events when modules are unloaded. */
933 gcov_module_unload(void *arg __unused, module_t mod)
935 struct gcov_info *info = NULL;
936 struct gcov_info *prev = NULL;
938 mtx_lock(&gcov_mtx );
940 /* Remove entries located in module from linked list. */
941 while ((info = gcov_info_next(info))) {
942 if (within_module((vm_offset_t)info, mod)) {
943 gcov_info_unlink(prev, info);
944 if (gcov_events_enabled)
945 gcov_event(GCOV_REMOVE, info);
950 mtx_unlock(&gcov_mtx);
956 init_node(&root_node, NULL, NULL, NULL);
957 root_node.dentry = debugfs_create_dir("gcov", NULL);