aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/lk/debugfs.c
diff options
context:
space:
mode:
authorKonrad Rzeszutek Wilk <konrad.wilk@oracle.com>2013-05-15 10:26:50 -0400
committerKonrad Rzeszutek Wilk <konrad.wilk@oracle.com>2013-05-15 10:26:50 -0400
commit12e04ffcd93b25dfd726d46338c2ee7d23de556e (patch)
treef91479a62805619168994fd3ee55e3ffa23fc24e /tools/lib/lk/debugfs.c
parent9eff37a8713939f218ab8bf0dc93f1d67af7b8b4 (diff)
parentf722406faae2d073cc1d01063d1123c35425939e (diff)
Merge tag 'v3.10-rc1' into stable/for-linus-3.10
Linux 3.10-rc1 * tag 'v3.10-rc1': (12273 commits) Linux 3.10-rc1 [SCSI] qla2xxx: Update firmware link in Kconfig file. [SCSI] iscsi class, qla4xxx: fix sess/conn refcounting when find fns are used [SCSI] sas: unify the pointlessly separated enums sas_dev_type and sas_device_type [SCSI] pm80xx: thermal, sas controller config and error handling update [SCSI] pm80xx: NCQ error handling changes [SCSI] pm80xx: WWN Modification for PM8081/88/89 controllers [SCSI] pm80xx: Changed module name and debug messages update [SCSI] pm80xx: Firmware flash memory free fix, with addition of new memory region for it [SCSI] pm80xx: SPC new firmware changes for device id 0x8081 alone [SCSI] pm80xx: Added SPCv/ve specific hardware functionalities and relevant changes in common files [SCSI] pm80xx: MSI-X implementation for using 64 interrupts [SCSI] pm80xx: Updated common functions common for SPC and SPCv/ve [SCSI] pm80xx: Multiple inbound/outbound queue configuration [SCSI] pm80xx: Added SPCv/ve specific ids, variables and modify for SPC [SCSI] lpfc: fix up Kconfig dependencies [SCSI] Handle MLQUEUE busy response in scsi_send_eh_cmnd dm cache: set config value dm cache: move config fns dm thin: generate event when metadata threshold passed ...
Diffstat (limited to 'tools/lib/lk/debugfs.c')
-rw-r--r--tools/lib/lk/debugfs.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/tools/lib/lk/debugfs.c b/tools/lib/lk/debugfs.c
new file mode 100644
index 000000000000..099e7cd022e4
--- /dev/null
+++ b/tools/lib/lk/debugfs.c
@@ -0,0 +1,101 @@
1#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <sys/vfs.h>
7#include <sys/mount.h>
8#include <linux/magic.h>
9#include <linux/kernel.h>
10
11#include "debugfs.h"
12
13char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
14
15static const char * const debugfs_known_mountpoints[] = {
16 "/sys/kernel/debug/",
17 "/debug/",
18 0,
19};
20
21static bool debugfs_found;
22
23/* find the path to the mounted debugfs */
24const char *debugfs_find_mountpoint(void)
25{
26 const char * const *ptr;
27 char type[100];
28 FILE *fp;
29
30 if (debugfs_found)
31 return (const char *)debugfs_mountpoint;
32
33 ptr = debugfs_known_mountpoints;
34 while (*ptr) {
35 if (debugfs_valid_mountpoint(*ptr) == 0) {
36 debugfs_found = true;
37 strcpy(debugfs_mountpoint, *ptr);
38 return debugfs_mountpoint;
39 }
40 ptr++;
41 }
42
43 /* give up and parse /proc/mounts */
44 fp = fopen("/proc/mounts", "r");
45 if (fp == NULL)
46 return NULL;
47
48 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
49 debugfs_mountpoint, type) == 2) {
50 if (strcmp(type, "debugfs") == 0)
51 break;
52 }
53 fclose(fp);
54
55 if (strcmp(type, "debugfs") != 0)
56 return NULL;
57
58 debugfs_found = true;
59
60 return debugfs_mountpoint;
61}
62
63/* verify that a mountpoint is actually a debugfs instance */
64
65int debugfs_valid_mountpoint(const char *debugfs)
66{
67 struct statfs st_fs;
68
69 if (statfs(debugfs, &st_fs) < 0)
70 return -ENOENT;
71 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
72 return -ENOENT;
73
74 return 0;
75}
76
77/* mount the debugfs somewhere if it's not mounted */
78char *debugfs_mount(const char *mountpoint)
79{
80 /* see if it's already mounted */
81 if (debugfs_find_mountpoint())
82 goto out;
83
84 /* if not mounted and no argument */
85 if (mountpoint == NULL) {
86 /* see if environment variable set */
87 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
88 /* if no environment variable, use default */
89 if (mountpoint == NULL)
90 mountpoint = "/sys/kernel/debug";
91 }
92
93 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
94 return NULL;
95
96 /* save the mountpoint */
97 debugfs_found = true;
98 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
99out:
100 return debugfs_mountpoint;
101}