Re: [RFC V2 PATCH]debugfs:Fix terminology inconsistency of dir name to mount debugfs filesystem.

From: Steven Rostedt
Date: Sat May 30 2009 - 09:44:23 EST



On Sat, 30 May 2009, GeunSik Lim wrote:
> @@ -1202,11 +1208,57 @@ something like this simple program:
>
> #include <stdio.h>
> #include <stdlib.h>
> +#include <string.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <unistd.h>
>
> +#define _STR(x) #x
> +#define STR(x) _STR(x)
> +#define MAX_PATH 256
> +
> +const char *find_debugfs(char *file_name)
> +{
> + static char debugfs[MAX_PATH+1];
> + static int debugfs_found;
> + char type[100];
> + FILE *fp;
> + int size;
> +
> + if (debugfs_found)
> + return debugfs;
> +
> + if ((fp = fopen("/proc/mounts","r")) == NULL) {
> + perror("/proc/mounts");
> + return NULL;
> + }
> +
> + while (fscanf(fp, "%*s %"
> + STR(MAX_PATH)
> + "s %99s %*s %*d %*d\n",
> + debugfs, type) == 2) {
> + if (strcmp(type, "debugfs") == 0)
> + break;
> + }
> + fclose(fp);
> +
> + if (strcmp(type, "debugfs") != 0) {
> + fprintf(stderr, "debugfs not mounted");
> + return NULL;
> + }
> +
> + debugfs_found = 1;
> +
> + size = MAX_PATH - strlen(debugfs);
> + strncat(debugfs, "/tracing/", size);
> +
> + size = MAX_PATH - strlen(debugfs);
> + strncat(debugfs, file_name, size);

You need to make two functions. One to find the debugfs and one to add the
file. The "debugfs_found" will make the function return just the first
file_name every time. Something like this: (remove the file_name from
find_debugfs).

const char *tracing_file(const char *file_name)
{
static char trace_file[MAX_PATH+1];

snprintf(trace_file, MAX_PATH, "%s/%s", find_debugfs(), file_name);

return trace_file;
}

The above is not thread safe, but is OK for non threaded programs.

-- Steve

> +
> + return debugfs;
> +}
> +
> int main (int argc, char **argv)
> {
> if (argc < 1)
> @@ -1217,12 +1269,12 @@ int main (int argc, char **argv)
> char line[64];
> int s;
>
> - ffd = open("/debug/tracing/current_tracer", O_WRONLY);
> + ffd = open(find_debugfs("current_tracer"), O_WRONLY);
> if (ffd < 0)
> exit(-1);
> write(ffd, "nop", 3);
>
> - fd = open("/debug/tracing/set_ftrace_pid", O_WRONLY);
> + fd = open(find_debugfs("set_ftrace_pid"), O_WRONLY);
> s = sprintf(line, "%d\n", getpid());
> write(fd, line, s);
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/