| 
									
										
										
										
											2013-02-20 16:32:30 +01:00
										 |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							|  |  |  | #include <sys/vfs.h>
 | 
					
						
							| 
									
										
										
										
											2011-11-16 12:55:59 -02:00
										 |  |  | #include <sys/mount.h>
 | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:30 +01:00
										 |  |  | #include <linux/kernel.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "debugfs.h"
 | 
					
						
							| 
									
										
										
										
											2011-11-16 12:55:59 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-16 14:03:07 -02:00
										 |  |  | char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:30 +01:00
										 |  |  | static const char * const debugfs_known_mountpoints[] = { | 
					
						
							| 
									
										
										
										
											2014-04-26 15:55:12 +08:00
										 |  |  | 	"/sys/kernel/debug", | 
					
						
							|  |  |  | 	"/debug", | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 	0, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:27 +01:00
										 |  |  | static bool debugfs_found; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* find the path to the mounted debugfs */ | 
					
						
							|  |  |  | const char *debugfs_find_mountpoint(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:30 +01:00
										 |  |  | 	const char * const *ptr; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 	char type[100]; | 
					
						
							|  |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (debugfs_found) | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:30 +01:00
										 |  |  | 		return (const char *)debugfs_mountpoint; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ptr = debugfs_known_mountpoints; | 
					
						
							|  |  |  | 	while (*ptr) { | 
					
						
							|  |  |  | 		if (debugfs_valid_mountpoint(*ptr) == 0) { | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:27 +01:00
										 |  |  | 			debugfs_found = true; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 			strcpy(debugfs_mountpoint, *ptr); | 
					
						
							|  |  |  | 			return debugfs_mountpoint; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ptr++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* give up and parse /proc/mounts */ | 
					
						
							|  |  |  | 	fp = fopen("/proc/mounts", "r"); | 
					
						
							|  |  |  | 	if (fp == NULL) | 
					
						
							| 
									
										
										
										
											2011-11-16 14:03:07 -02:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-16 12:55:59 -02:00
										 |  |  | 	while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 		      debugfs_mountpoint, type) == 2) { | 
					
						
							|  |  |  | 		if (strcmp(type, "debugfs") == 0) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fclose(fp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (strcmp(type, "debugfs") != 0) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:27 +01:00
										 |  |  | 	debugfs_found = true; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return debugfs_mountpoint; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* verify that a mountpoint is actually a debugfs instance */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int debugfs_valid_mountpoint(const char *debugfs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct statfs st_fs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (statfs(debugfs, &st_fs) < 0) | 
					
						
							|  |  |  | 		return -ENOENT; | 
					
						
							|  |  |  | 	else if (st_fs.f_type != (long) DEBUGFS_MAGIC) | 
					
						
							|  |  |  | 		return -ENOENT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 16:47:12 +08:00
										 |  |  | /* mount the debugfs somewhere if it's not mounted */ | 
					
						
							|  |  |  | char *debugfs_mount(const char *mountpoint) | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | { | 
					
						
							|  |  |  | 	/* see if it's already mounted */ | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:27 +01:00
										 |  |  | 	if (debugfs_find_mountpoint()) | 
					
						
							| 
									
										
										
										
											2011-11-16 14:03:07 -02:00
										 |  |  | 		goto out; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* if not mounted and no argument */ | 
					
						
							|  |  |  | 	if (mountpoint == NULL) { | 
					
						
							|  |  |  | 		/* see if environment variable set */ | 
					
						
							|  |  |  | 		mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT); | 
					
						
							|  |  |  | 		/* if no environment variable, use default */ | 
					
						
							|  |  |  | 		if (mountpoint == NULL) | 
					
						
							|  |  |  | 			mountpoint = "/sys/kernel/debug"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 16:47:12 +08:00
										 |  |  | 	if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | 	/* save the mountpoint */ | 
					
						
							| 
									
										
										
										
											2013-02-20 16:32:27 +01:00
										 |  |  | 	debugfs_found = true; | 
					
						
							| 
									
										
										
										
											2011-11-16 14:03:07 -02:00
										 |  |  | 	strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); | 
					
						
							|  |  |  | out: | 
					
						
							| 
									
										
										
										
											2009-12-28 16:47:12 +08:00
										 |  |  | 	return debugfs_mountpoint; | 
					
						
							| 
									
										
										
										
											2009-11-08 09:01:37 -06:00
										 |  |  | } |