12521f2c2SPeter Oberparleiter /* 22521f2c2SPeter Oberparleiter * This code maintains a list of active profiling data structures. 32521f2c2SPeter Oberparleiter * 42521f2c2SPeter Oberparleiter * Copyright IBM Corp. 2009 52521f2c2SPeter Oberparleiter * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com> 62521f2c2SPeter Oberparleiter * 72521f2c2SPeter Oberparleiter * Uses gcc-internal data definitions. 82521f2c2SPeter Oberparleiter * Based on the gcov-kernel patch by: 92521f2c2SPeter Oberparleiter * Hubertus Franke <frankeh@us.ibm.com> 102521f2c2SPeter Oberparleiter * Nigel Hinds <nhinds@us.ibm.com> 112521f2c2SPeter Oberparleiter * Rajan Ravindran <rajancr@us.ibm.com> 122521f2c2SPeter Oberparleiter * Peter Oberparleiter <oberpar@linux.vnet.ibm.com> 132521f2c2SPeter Oberparleiter * Paul Larson 142521f2c2SPeter Oberparleiter */ 152521f2c2SPeter Oberparleiter 162521f2c2SPeter Oberparleiter #define pr_fmt(fmt) "gcov: " fmt 172521f2c2SPeter Oberparleiter 182521f2c2SPeter Oberparleiter #include <linux/init.h> 192521f2c2SPeter Oberparleiter #include <linux/module.h> 202521f2c2SPeter Oberparleiter #include <linux/mutex.h> 219d796e66SAndrey Ryabinin #include <linux/sched.h> 222521f2c2SPeter Oberparleiter #include "gcov.h" 232521f2c2SPeter Oberparleiter 242521f2c2SPeter Oberparleiter static int gcov_events_enabled; 252521f2c2SPeter Oberparleiter static DEFINE_MUTEX(gcov_lock); 262521f2c2SPeter Oberparleiter 272521f2c2SPeter Oberparleiter /* 282521f2c2SPeter Oberparleiter * __gcov_init is called by gcc-generated constructor code for each object 292521f2c2SPeter Oberparleiter * file compiled with -fprofile-arcs. 302521f2c2SPeter Oberparleiter */ 312521f2c2SPeter Oberparleiter void __gcov_init(struct gcov_info *info) 322521f2c2SPeter Oberparleiter { 332521f2c2SPeter Oberparleiter static unsigned int gcov_version; 342521f2c2SPeter Oberparleiter 352521f2c2SPeter Oberparleiter mutex_lock(&gcov_lock); 362521f2c2SPeter Oberparleiter if (gcov_version == 0) { 378cbce376SFrantisek Hrbata gcov_version = gcov_info_version(info); 382521f2c2SPeter Oberparleiter /* 392521f2c2SPeter Oberparleiter * Printing gcc's version magic may prove useful for debugging 402521f2c2SPeter Oberparleiter * incompatibility reports. 412521f2c2SPeter Oberparleiter */ 422521f2c2SPeter Oberparleiter pr_info("version magic: 0x%x\n", gcov_version); 432521f2c2SPeter Oberparleiter } 442521f2c2SPeter Oberparleiter /* 452521f2c2SPeter Oberparleiter * Add new profiling data structure to list and inform event 462521f2c2SPeter Oberparleiter * listener. 472521f2c2SPeter Oberparleiter */ 488cbce376SFrantisek Hrbata gcov_info_link(info); 492521f2c2SPeter Oberparleiter if (gcov_events_enabled) 502521f2c2SPeter Oberparleiter gcov_event(GCOV_ADD, info); 512521f2c2SPeter Oberparleiter mutex_unlock(&gcov_lock); 522521f2c2SPeter Oberparleiter } 532521f2c2SPeter Oberparleiter EXPORT_SYMBOL(__gcov_init); 542521f2c2SPeter Oberparleiter 552521f2c2SPeter Oberparleiter /* 562521f2c2SPeter Oberparleiter * These functions may be referenced by gcc-generated profiling code but serve 572521f2c2SPeter Oberparleiter * no function for kernel profiling. 582521f2c2SPeter Oberparleiter */ 592521f2c2SPeter Oberparleiter void __gcov_flush(void) 602521f2c2SPeter Oberparleiter { 612521f2c2SPeter Oberparleiter /* Unused. */ 622521f2c2SPeter Oberparleiter } 632521f2c2SPeter Oberparleiter EXPORT_SYMBOL(__gcov_flush); 642521f2c2SPeter Oberparleiter 652521f2c2SPeter Oberparleiter void __gcov_merge_add(gcov_type *counters, unsigned int n_counters) 662521f2c2SPeter Oberparleiter { 672521f2c2SPeter Oberparleiter /* Unused. */ 682521f2c2SPeter Oberparleiter } 692521f2c2SPeter Oberparleiter EXPORT_SYMBOL(__gcov_merge_add); 702521f2c2SPeter Oberparleiter 712521f2c2SPeter Oberparleiter void __gcov_merge_single(gcov_type *counters, unsigned int n_counters) 722521f2c2SPeter Oberparleiter { 732521f2c2SPeter Oberparleiter /* Unused. */ 742521f2c2SPeter Oberparleiter } 752521f2c2SPeter Oberparleiter EXPORT_SYMBOL(__gcov_merge_single); 762521f2c2SPeter Oberparleiter 772521f2c2SPeter Oberparleiter void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters) 782521f2c2SPeter Oberparleiter { 792521f2c2SPeter Oberparleiter /* Unused. */ 802521f2c2SPeter Oberparleiter } 812521f2c2SPeter Oberparleiter EXPORT_SYMBOL(__gcov_merge_delta); 822521f2c2SPeter Oberparleiter 835f41ea03SFrantisek Hrbata void __gcov_merge_ior(gcov_type *counters, unsigned int n_counters) 845f41ea03SFrantisek Hrbata { 855f41ea03SFrantisek Hrbata /* Unused. */ 865f41ea03SFrantisek Hrbata } 875f41ea03SFrantisek Hrbata EXPORT_SYMBOL(__gcov_merge_ior); 885f41ea03SFrantisek Hrbata 89a992bf83SYuan Pengfei void __gcov_merge_time_profile(gcov_type *counters, unsigned int n_counters) 90a992bf83SYuan Pengfei { 91a992bf83SYuan Pengfei /* Unused. */ 92a992bf83SYuan Pengfei } 93a992bf83SYuan Pengfei EXPORT_SYMBOL(__gcov_merge_time_profile); 94a992bf83SYuan Pengfei 953e44c471SLorenzo Stoakes void __gcov_merge_icall_topn(gcov_type *counters, unsigned int n_counters) 963e44c471SLorenzo Stoakes { 973e44c471SLorenzo Stoakes /* Unused. */ 983e44c471SLorenzo Stoakes } 993e44c471SLorenzo Stoakes EXPORT_SYMBOL(__gcov_merge_icall_topn); 1003e44c471SLorenzo Stoakes 10105384213SMartin Liska void __gcov_exit(void) 10205384213SMartin Liska { 10305384213SMartin Liska /* Unused. */ 10405384213SMartin Liska } 10505384213SMartin Liska EXPORT_SYMBOL(__gcov_exit); 10605384213SMartin Liska 1072521f2c2SPeter Oberparleiter /** 1082521f2c2SPeter Oberparleiter * gcov_enable_events - enable event reporting through gcov_event() 1092521f2c2SPeter Oberparleiter * 1102521f2c2SPeter Oberparleiter * Turn on reporting of profiling data load/unload-events through the 1112521f2c2SPeter Oberparleiter * gcov_event() callback. Also replay all previous events once. This function 1122521f2c2SPeter Oberparleiter * is needed because some events are potentially generated too early for the 1132521f2c2SPeter Oberparleiter * callback implementation to handle them initially. 1142521f2c2SPeter Oberparleiter */ 1152521f2c2SPeter Oberparleiter void gcov_enable_events(void) 1162521f2c2SPeter Oberparleiter { 1178cbce376SFrantisek Hrbata struct gcov_info *info = NULL; 1182521f2c2SPeter Oberparleiter 1192521f2c2SPeter Oberparleiter mutex_lock(&gcov_lock); 1202521f2c2SPeter Oberparleiter gcov_events_enabled = 1; 1218cbce376SFrantisek Hrbata 1222521f2c2SPeter Oberparleiter /* Perform event callback for previously registered entries. */ 1239d796e66SAndrey Ryabinin while ((info = gcov_info_next(info))) { 1242521f2c2SPeter Oberparleiter gcov_event(GCOV_ADD, info); 1259d796e66SAndrey Ryabinin cond_resched(); 1269d796e66SAndrey Ryabinin } 1278cbce376SFrantisek Hrbata 1282521f2c2SPeter Oberparleiter mutex_unlock(&gcov_lock); 1292521f2c2SPeter Oberparleiter } 1302521f2c2SPeter Oberparleiter 1312521f2c2SPeter Oberparleiter #ifdef CONFIG_MODULES 1322521f2c2SPeter Oberparleiter /* Update list and generate events when modules are unloaded. */ 1332521f2c2SPeter Oberparleiter static int gcov_module_notifier(struct notifier_block *nb, unsigned long event, 1342521f2c2SPeter Oberparleiter void *data) 1352521f2c2SPeter Oberparleiter { 1362521f2c2SPeter Oberparleiter struct module *mod = data; 1378cbce376SFrantisek Hrbata struct gcov_info *info = NULL; 1388cbce376SFrantisek Hrbata struct gcov_info *prev = NULL; 1392521f2c2SPeter Oberparleiter 1402521f2c2SPeter Oberparleiter if (event != MODULE_STATE_GOING) 1412521f2c2SPeter Oberparleiter return NOTIFY_OK; 1422521f2c2SPeter Oberparleiter mutex_lock(&gcov_lock); 1438cbce376SFrantisek Hrbata 1442521f2c2SPeter Oberparleiter /* Remove entries located in module from linked list. */ 1458cbce376SFrantisek Hrbata while ((info = gcov_info_next(info))) { 146c65abf35SRusty Russell if (within_module((unsigned long)info, mod)) { 1478cbce376SFrantisek Hrbata gcov_info_unlink(prev, info); 1482521f2c2SPeter Oberparleiter if (gcov_events_enabled) 1492521f2c2SPeter Oberparleiter gcov_event(GCOV_REMOVE, info); 1502521f2c2SPeter Oberparleiter } else 1512521f2c2SPeter Oberparleiter prev = info; 1522521f2c2SPeter Oberparleiter } 1538cbce376SFrantisek Hrbata 1542521f2c2SPeter Oberparleiter mutex_unlock(&gcov_lock); 1552521f2c2SPeter Oberparleiter 1562521f2c2SPeter Oberparleiter return NOTIFY_OK; 1572521f2c2SPeter Oberparleiter } 1582521f2c2SPeter Oberparleiter 1592521f2c2SPeter Oberparleiter static struct notifier_block gcov_nb = { 1602521f2c2SPeter Oberparleiter .notifier_call = gcov_module_notifier, 1612521f2c2SPeter Oberparleiter }; 1622521f2c2SPeter Oberparleiter 1632521f2c2SPeter Oberparleiter static int __init gcov_init(void) 1642521f2c2SPeter Oberparleiter { 1652521f2c2SPeter Oberparleiter return register_module_notifier(&gcov_nb); 1662521f2c2SPeter Oberparleiter } 1672521f2c2SPeter Oberparleiter device_initcall(gcov_init); 1682521f2c2SPeter Oberparleiter #endif /* CONFIG_MODULES */ 169