xref: /third_party/libfuse/test/stracedecode.c (revision 6881f68f)
16881f68fSopenharmony_ci#include <stdio.h>
26881f68fSopenharmony_ci#include <string.h>
36881f68fSopenharmony_ci#include "fuse_kernel.h"
46881f68fSopenharmony_ci
56881f68fSopenharmony_cistatic struct {
66881f68fSopenharmony_ci	const char *name;
76881f68fSopenharmony_ci} fuse_ll_ops[] = {
86881f68fSopenharmony_ci	[FUSE_LOOKUP]	   = { "LOOKUP"	     },
96881f68fSopenharmony_ci	[FUSE_FORGET]	   = { "FORGET"	     },
106881f68fSopenharmony_ci	[FUSE_GETATTR]	   = { "GETATTR"     },
116881f68fSopenharmony_ci	[FUSE_SETATTR]	   = { "SETATTR"     },
126881f68fSopenharmony_ci	[FUSE_READLINK]	   = { "READLINK"    },
136881f68fSopenharmony_ci	[FUSE_SYMLINK]	   = { "SYMLINK"     },
146881f68fSopenharmony_ci	[FUSE_MKNOD]	   = { "MKNOD"	     },
156881f68fSopenharmony_ci	[FUSE_MKDIR]	   = { "MKDIR"	     },
166881f68fSopenharmony_ci	[FUSE_UNLINK]	   = { "UNLINK"	     },
176881f68fSopenharmony_ci	[FUSE_RMDIR]	   = { "RMDIR"	     },
186881f68fSopenharmony_ci	[FUSE_RENAME]	   = { "RENAME"	     },
196881f68fSopenharmony_ci	[FUSE_LINK]	   = { "LINK"	     },
206881f68fSopenharmony_ci	[FUSE_OPEN]	   = { "OPEN"	     },
216881f68fSopenharmony_ci	[FUSE_READ]	   = { "READ"	     },
226881f68fSopenharmony_ci	[FUSE_WRITE]	   = { "WRITE"	     },
236881f68fSopenharmony_ci	[FUSE_STATFS]	   = { "STATFS"	     },
246881f68fSopenharmony_ci	[FUSE_RELEASE]	   = { "RELEASE"     },
256881f68fSopenharmony_ci	[FUSE_FSYNC]	   = { "FSYNC"	     },
266881f68fSopenharmony_ci	[FUSE_SETXATTR]	   = { "SETXATTR"    },
276881f68fSopenharmony_ci	[FUSE_GETXATTR]	   = { "GETXATTR"    },
286881f68fSopenharmony_ci	[FUSE_LISTXATTR]   = { "LISTXATTR"   },
296881f68fSopenharmony_ci	[FUSE_REMOVEXATTR] = { "REMOVEXATTR" },
306881f68fSopenharmony_ci	[FUSE_FLUSH]	   = { "FLUSH"	     },
316881f68fSopenharmony_ci	[FUSE_INIT]	   = { "INIT"	     },
326881f68fSopenharmony_ci	[FUSE_OPENDIR]	   = { "OPENDIR"     },
336881f68fSopenharmony_ci	[FUSE_READDIR]	   = { "READDIR"     },
346881f68fSopenharmony_ci	[FUSE_RELEASEDIR]  = { "RELEASEDIR"  },
356881f68fSopenharmony_ci	[FUSE_FSYNCDIR]	   = { "FSYNCDIR"    },
366881f68fSopenharmony_ci	[FUSE_GETLK]	   = { "GETLK"	     },
376881f68fSopenharmony_ci	[FUSE_SETLK]	   = { "SETLK"	     },
386881f68fSopenharmony_ci	[FUSE_SETLKW]	   = { "SETLKW"	     },
396881f68fSopenharmony_ci	[FUSE_ACCESS]	   = { "ACCESS"	     },
406881f68fSopenharmony_ci	[FUSE_CREATE]	   = { "CREATE"	     },
416881f68fSopenharmony_ci	[FUSE_INTERRUPT]   = { "INTERRUPT"   },
426881f68fSopenharmony_ci	[FUSE_BMAP]	   = { "BMAP"	     },
436881f68fSopenharmony_ci	[FUSE_DESTROY]	   = { "DESTROY"     },
446881f68fSopenharmony_ci	[FUSE_READDIRPLUS] = { "READDIRPLUS" },
456881f68fSopenharmony_ci};
466881f68fSopenharmony_ci
476881f68fSopenharmony_ci#define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
486881f68fSopenharmony_ci
496881f68fSopenharmony_cistatic const char *opname(enum fuse_opcode opcode)
506881f68fSopenharmony_ci{
516881f68fSopenharmony_ci	if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
526881f68fSopenharmony_ci		return "???";
536881f68fSopenharmony_ci	else
546881f68fSopenharmony_ci		return fuse_ll_ops[opcode].name;
556881f68fSopenharmony_ci}
566881f68fSopenharmony_ci
576881f68fSopenharmony_ci
586881f68fSopenharmony_cistatic void process_buf(int dir, char *buf, int len)
596881f68fSopenharmony_ci{
606881f68fSopenharmony_ci	static unsigned long long prevuniq = -1;
616881f68fSopenharmony_ci	static int prevopcode;
626881f68fSopenharmony_ci
636881f68fSopenharmony_ci	if (!dir) {
646881f68fSopenharmony_ci		struct fuse_in_header *in = (struct fuse_in_header *) buf;
656881f68fSopenharmony_ci		buf += sizeof(struct fuse_in_header);
666881f68fSopenharmony_ci
676881f68fSopenharmony_ci		printf("unique: %llu, opcode: %s (%i), nodeid: %lu, len: %i, insize: %i\n",
686881f68fSopenharmony_ci		       (unsigned long long) in->unique,
696881f68fSopenharmony_ci		       opname((enum fuse_opcode) in->opcode), in->opcode,
706881f68fSopenharmony_ci		       (unsigned long) in->nodeid, in->len, len);
716881f68fSopenharmony_ci
726881f68fSopenharmony_ci		switch (in->opcode) {
736881f68fSopenharmony_ci		case FUSE_READ: {
746881f68fSopenharmony_ci			struct fuse_read_in *arg = (struct fuse_read_in *) buf;
756881f68fSopenharmony_ci			printf("-READ fh:%llu off:%llu siz:%u rfl:%u own:%llu fl:%u\n",
766881f68fSopenharmony_ci			       arg->fh, arg->offset, arg->size, arg->read_flags,
776881f68fSopenharmony_ci			       arg->lock_owner, arg->flags);
786881f68fSopenharmony_ci			break;
796881f68fSopenharmony_ci		}
806881f68fSopenharmony_ci		case FUSE_WRITE: {
816881f68fSopenharmony_ci			struct fuse_write_in *arg = (struct fuse_write_in *) buf;
826881f68fSopenharmony_ci			printf("-WRITE fh:%llu off:%llu siz:%u wfl:%u own:%llu fl:%u\n",
836881f68fSopenharmony_ci			       arg->fh, arg->offset, arg->size, arg->write_flags,
846881f68fSopenharmony_ci			       arg->lock_owner, arg->flags);
856881f68fSopenharmony_ci			break;
866881f68fSopenharmony_ci		}
876881f68fSopenharmony_ci		}
886881f68fSopenharmony_ci		prevuniq = in->unique;
896881f68fSopenharmony_ci		prevopcode = in->opcode;
906881f68fSopenharmony_ci	} else {
916881f68fSopenharmony_ci		struct fuse_out_header *out = (struct fuse_out_header *) buf;
926881f68fSopenharmony_ci		buf += sizeof(struct fuse_out_header);
936881f68fSopenharmony_ci
946881f68fSopenharmony_ci		printf("   unique: %llu, error: %i (%s), len: %i, outsize: %i\n",
956881f68fSopenharmony_ci		       (unsigned long long) out->unique, out->error,
966881f68fSopenharmony_ci		       strerror(-out->error), out->len, len);
976881f68fSopenharmony_ci
986881f68fSopenharmony_ci		if (out->unique == prevuniq) {
996881f68fSopenharmony_ci			switch (prevopcode) {
1006881f68fSopenharmony_ci			case FUSE_GETATTR: {
1016881f68fSopenharmony_ci				struct fuse_attr_out *arg = (struct fuse_attr_out *) buf;
1026881f68fSopenharmony_ci				printf("+ATTR v:%llu.%09u i:%llu s:%llu b:%llu\n",
1036881f68fSopenharmony_ci				       arg->attr_valid, arg->attr_valid_nsec,
1046881f68fSopenharmony_ci				       arg->attr.ino, arg->attr.size, arg->attr.blocks);
1056881f68fSopenharmony_ci				break;
1066881f68fSopenharmony_ci			}
1076881f68fSopenharmony_ci			case FUSE_LOOKUP: {
1086881f68fSopenharmony_ci				struct fuse_entry_out *arg = (struct fuse_entry_out *) buf;
1096881f68fSopenharmony_ci				printf("+ENTRY nodeid:%llu v:%llu.%09u i:%llu s:%llu b:%llu\n",
1106881f68fSopenharmony_ci				       arg->nodeid, arg->attr_valid, arg->attr_valid_nsec,
1116881f68fSopenharmony_ci				       arg->attr.ino, arg->attr.size, arg->attr.blocks);
1126881f68fSopenharmony_ci				break;
1136881f68fSopenharmony_ci			}
1146881f68fSopenharmony_ci			}
1156881f68fSopenharmony_ci		}
1166881f68fSopenharmony_ci	}
1176881f68fSopenharmony_ci
1186881f68fSopenharmony_ci}
1196881f68fSopenharmony_ci
1206881f68fSopenharmony_ciint main(void)
1216881f68fSopenharmony_ci{
1226881f68fSopenharmony_ci	FILE *in = stdin;
1236881f68fSopenharmony_ci	while (1) {
1246881f68fSopenharmony_ci		int dir;
1256881f68fSopenharmony_ci		int res;
1266881f68fSopenharmony_ci		char buf[1048576];
1276881f68fSopenharmony_ci		unsigned len = 0;
1286881f68fSopenharmony_ci
1296881f68fSopenharmony_ci		memset(buf, 0, sizeof(buf));
1306881f68fSopenharmony_ci		while (1) {
1316881f68fSopenharmony_ci			char str[32];
1326881f68fSopenharmony_ci
1336881f68fSopenharmony_ci			res = fscanf(in, "%30s", str);
1346881f68fSopenharmony_ci			if (res != 1 && feof(in))
1356881f68fSopenharmony_ci				return 0;
1366881f68fSopenharmony_ci
1376881f68fSopenharmony_ci			if (res == 0)
1386881f68fSopenharmony_ci				continue;
1396881f68fSopenharmony_ci
1406881f68fSopenharmony_ci			if (strncmp(str, "read(", 5) == 0) {
1416881f68fSopenharmony_ci				dir = 0;
1426881f68fSopenharmony_ci				break;
1436881f68fSopenharmony_ci			} else if (strncmp(str, "writev(", 7) == 0) {
1446881f68fSopenharmony_ci				dir = 1;
1456881f68fSopenharmony_ci				break;
1466881f68fSopenharmony_ci			}
1476881f68fSopenharmony_ci		}
1486881f68fSopenharmony_ci
1496881f68fSopenharmony_ci		while (1) {
1506881f68fSopenharmony_ci			int c = getc(in);
1516881f68fSopenharmony_ci			if (c == '"') {
1526881f68fSopenharmony_ci				while (1) {
1536881f68fSopenharmony_ci					int val;
1546881f68fSopenharmony_ci
1556881f68fSopenharmony_ci					c = getc(in);
1566881f68fSopenharmony_ci					if (c == EOF) {
1576881f68fSopenharmony_ci						fprintf(stderr, "eof in string\n");
1586881f68fSopenharmony_ci						break;
1596881f68fSopenharmony_ci					}
1606881f68fSopenharmony_ci					if (c == '\n') {
1616881f68fSopenharmony_ci						fprintf(stderr, "eol in string\n");
1626881f68fSopenharmony_ci						break;
1636881f68fSopenharmony_ci					}
1646881f68fSopenharmony_ci					if (c == '"')
1656881f68fSopenharmony_ci						break;
1666881f68fSopenharmony_ci					if (c != '\\') {
1676881f68fSopenharmony_ci						val = c;
1686881f68fSopenharmony_ci					} else {
1696881f68fSopenharmony_ci						c = getc(in);
1706881f68fSopenharmony_ci						switch (c) {
1716881f68fSopenharmony_ci						case 'n': val = '\n'; break;
1726881f68fSopenharmony_ci						case 'r': val = '\r'; break;
1736881f68fSopenharmony_ci						case 't': val = '\t'; break;
1746881f68fSopenharmony_ci						case '"': val = '"'; break;
1756881f68fSopenharmony_ci						case '\\': val = '\\'; break;
1766881f68fSopenharmony_ci						case 'x':
1776881f68fSopenharmony_ci							res = scanf("%x", &val);
1786881f68fSopenharmony_ci							if (res != 1) {
1796881f68fSopenharmony_ci								fprintf(stderr, "parse error\n");
1806881f68fSopenharmony_ci								continue;
1816881f68fSopenharmony_ci							}
1826881f68fSopenharmony_ci							break;
1836881f68fSopenharmony_ci						default:
1846881f68fSopenharmony_ci							fprintf(stderr, "unknown sequence: '\\%c'\n", c);
1856881f68fSopenharmony_ci							continue;
1866881f68fSopenharmony_ci						}
1876881f68fSopenharmony_ci					}
1886881f68fSopenharmony_ci					buf[len++] = val;
1896881f68fSopenharmony_ci				}
1906881f68fSopenharmony_ci			}
1916881f68fSopenharmony_ci			if (c == '\n')
1926881f68fSopenharmony_ci				break;
1936881f68fSopenharmony_ci		}
1946881f68fSopenharmony_ci		process_buf(dir, buf, len);
1956881f68fSopenharmony_ci		memset(buf, 0, len);
1966881f68fSopenharmony_ci		len = 0;
1976881f68fSopenharmony_ci	}
1986881f68fSopenharmony_ci}
199