syscall.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <sys.h>
  12. #include <fcall.h>
  13. char buf[1048576];
  14. #define NARG 5
  15. uintptr arg[NARG];
  16. /* system calls not defined in libc.h */
  17. int brk_(void*);
  18. struct{
  19. char *name;
  20. int (*func)();
  21. }tab[]={
  22. #include "tab.h"
  23. 0, 0
  24. };
  25. uintptr parse(char *);
  26. void catch(void*, char*);
  27. char*
  28. xctime(uint32_t t)
  29. {
  30. char *buf, *s;
  31. s = ctime(t);
  32. s[strlen(s)-1] = '\0'; /* remove newline */
  33. buf = malloc(512);
  34. if(buf == nil)
  35. sysfatal("can't malloc: %r");
  36. snprint(buf, 512, "%s (%lu)", s, t);
  37. return buf;
  38. }
  39. char*
  40. lstime(int32_t l)
  41. {
  42. static char buf[32];
  43. char *t;
  44. int32_t clk;
  45. clk = time(0);
  46. t = ctime(l);
  47. /* 6 months in the past or a day in the future */
  48. if(l<clk-180L*24*60*60 || clk+24L*60*60<l){
  49. memmove(buf, t+4, 7); /* month and day */
  50. memmove(buf+7, t+23, 5); /* year */
  51. }else
  52. memmove(buf, t+4, 12); /* skip day of week */
  53. buf[12] = 0;
  54. return buf;
  55. }
  56. void
  57. main(int argc, char *argv[])
  58. {
  59. int i, j, c;
  60. int oflag, xflag, sflag;
  61. int64_t r;
  62. Dir d;
  63. char strs[1024];
  64. char ebuf[1024];
  65. fmtinstall('M', dirmodefmt);
  66. oflag = 0;
  67. xflag = 0;
  68. sflag = 0;
  69. ARGBEGIN{
  70. case 'o':
  71. oflag++;
  72. break;
  73. case 's':
  74. sflag++;
  75. break;
  76. case 'x':
  77. xflag++;
  78. break;
  79. default:
  80. goto Usage;
  81. }ARGEND
  82. if(argc<1 || argc>1+NARG){
  83. Usage:
  84. fprint(2, "usage: syscall [-ox] entry [args; buf==1MB buffer]\n");
  85. fprint(2, "\tsyscall write 1 hello 5\n");
  86. fprint(2, "\tsyscall -o errstr buf 1024\n");
  87. fprint(2, "\tsyscall -[xs] stat file buf 1024\n");
  88. exits("usage");
  89. }
  90. for(i=1; i<argc; i++)
  91. arg[i-1] = parse(argv[i]);
  92. notify(catch);
  93. for(i=0; tab[i].name; i++)
  94. if(strcmp(tab[i].name, argv[0])==0){
  95. /* special case for seek, pread, pwrite; vlongs are problematic */
  96. if(strcmp(argv[0], "seek") == 0)
  97. r=seek(arg[0], strtoll(argv[2], 0, 0), arg[2]);
  98. else if(strcmp(argv[0], "pread") == 0)
  99. r=pread(arg[0], (void*)arg[1], arg[2], strtoll(argv[4], 0, 0));
  100. else if(strcmp(argv[0], "pwrite") == 0)
  101. r=pwrite(arg[0], (void*)arg[1], arg[2], strtoll(argv[4], 0, 0));
  102. else
  103. r=(*tab[i].func)(arg[0], arg[1], arg[2], arg[3], arg[4]);
  104. if(r == -1){
  105. errstr(ebuf, sizeof ebuf);
  106. fprint(2, "syscall: return %lld, error:%s\n", r, ebuf);
  107. }else{
  108. ebuf[0] = 0;
  109. fprint(2, "syscall: return %lld, no error\n", r);
  110. }
  111. if(oflag)
  112. print("%s\n", buf);
  113. if(xflag){
  114. for(j=0; j<r; j++){
  115. if(j%16 == 0)
  116. print("%.4x\t", j);
  117. c = buf[j]&0xFF;
  118. if('!'<=c && c<='~')
  119. print(" %c ", c);
  120. else
  121. print("%.2x ", c);
  122. if(j%16 == 15)
  123. print("\n");
  124. }
  125. print("\n");
  126. }
  127. if(sflag && r > 0){
  128. r = convM2D((uint8_t*)buf, r, &d, strs);
  129. if(r <= BIT16SZ)
  130. print("short stat message\n");
  131. else{
  132. print("[%s] ", d.muid);
  133. print("(%.16llux %lu %.2x) ", d.qid.path, d.qid.vers, d.qid.type);
  134. print("%M (%lo) ", d.mode, d.mode);
  135. print("%c %d ", d.type, d.dev);
  136. print("%s %s ", d.uid, d.gid);
  137. print("%lld ", d.length);
  138. print("%s ", lstime(d.mtime));
  139. print("%s\n", d.name);
  140. print("\tmtime: %s\n\tatime: %s\n", xctime(d.mtime), xctime(d.atime));
  141. }
  142. }
  143. exits(ebuf);
  144. }
  145. fprint(2, "syscall: %s not known\n", argv[0]);
  146. exits("unknown");
  147. }
  148. uintptr
  149. parse(char *s)
  150. {
  151. char *t;
  152. uintptr l;
  153. if(strcmp(s, "buf") == 0)
  154. return (uintptr)buf;
  155. l = strtoull(s, &t, 0);
  156. if(t>s && *t==0)
  157. return l;
  158. return (uintptr)s;
  159. }
  160. void
  161. catch(void *v, char *msg)
  162. {
  163. fprint(2, "syscall: received note='%s'\n", msg);
  164. noted(NDFLT);
  165. }