devproc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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 "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "../port/edf.h"
  16. #include "tos.h"
  17. #include <trace.h>
  18. #include "ureg.h"
  19. enum
  20. {
  21. Qdir,
  22. Qtrace,
  23. Qtracepids,
  24. Qargs,
  25. Qctl,
  26. Qfd,
  27. Qfpregs,
  28. Qgdbregs,
  29. Qkregs,
  30. Qmem,
  31. Qnote,
  32. Qnoteid,
  33. Qnotepg,
  34. Qns,
  35. Qproc,
  36. Qregs,
  37. Qsegment,
  38. Qstatus,
  39. Qstrace,
  40. Qtext,
  41. Qwait,
  42. Qprofile,
  43. Qsyscall,
  44. Qcore,
  45. Qtls,
  46. Qpager,
  47. };
  48. enum
  49. {
  50. CMclose,
  51. CMclosefiles,
  52. CMfixedpri,
  53. CMhang,
  54. CMkill,
  55. CMnohang,
  56. CMnoswap,
  57. CMpri,
  58. CMprivate,
  59. CMprofile,
  60. CMstart,
  61. CMstartstop,
  62. CMstartsyscall,
  63. CMstop,
  64. CMwaitstop,
  65. CMwired,
  66. CMtrace,
  67. /* real time */
  68. CMperiod,
  69. CMdeadline,
  70. CMcost,
  71. CMsporadic,
  72. CMdeadlinenotes,
  73. CMadmit,
  74. CMextra,
  75. CMexpel,
  76. CMevent,
  77. CMcore,
  78. CMstraceme,
  79. CMstraceall,
  80. CMstraceoff,
  81. };
  82. enum{
  83. Nevents = 0x4000,
  84. Emask = Nevents - 1,
  85. Ntracedpids = 1024,
  86. };
  87. #define STATSIZE (2*KNAMELEN+NUMSIZE + 9*NUMSIZE + 6*NUMSIZE + 2*NUMSIZE + 1)
  88. /*
  89. * Status, fd, and ns are left fully readable (0444) because of their use in debugging,
  90. * particularly on shared servers.
  91. * Arguably, ns and fd shouldn't be readable; if you'd prefer, change them to 0000
  92. */
  93. Dirtab procdir[] =
  94. {
  95. {"args", {Qargs}, 0, 0660},
  96. {"ctl", {Qctl}, 0, 0000},
  97. {"fd", {Qfd}, 0, 0444},
  98. {"fpregs", {Qfpregs}, 0, 0000},
  99. {"kregs", {Qkregs}, sizeof(Ureg), 0600},
  100. {"mem", {Qmem}, 0, 0000},
  101. {"note", {Qnote}, 0, 0000},
  102. {"noteid", {Qnoteid}, 0, 0664},
  103. {"notepg", {Qnotepg}, 0, 0000},
  104. {"ns", {Qns}, 0, 0444},
  105. {"proc", {Qproc}, 0, 0400},
  106. {"gdbregs", {Qgdbregs}, GDB_NUMREGBYTES, 0000},
  107. {"regs", {Qregs}, sizeof(Ureg), 0000},
  108. {"segment", {Qsegment}, 0, 0444},
  109. {"status", {Qstatus}, STATSIZE, 0444},
  110. {"strace", {Qstrace}, 0, 0666},
  111. {"text", {Qtext}, 0, 0000},
  112. {"wait", {Qwait}, 0, 0400},
  113. {"profile", {Qprofile}, 0, 0400},
  114. {"syscall", {Qsyscall}, 0, 0400},
  115. {"core", {Qcore}, 0, 0444},
  116. {"tls", {Qtls}, 0, 0600},
  117. {"pager", {Qpager}, 0, 0600|DMEXCL},
  118. };
  119. static
  120. Cmdtab proccmd[] = {
  121. {CMclose, "close", 2},
  122. {CMclosefiles, "closefiles", 1},
  123. {CMfixedpri, "fixedpri", 2},
  124. {CMhang, "hang", 1},
  125. {CMnohang, "nohang", 1},
  126. {CMnoswap, "noswap", 1},
  127. {CMkill, "kill", 1},
  128. {CMpri, "pri", 2},
  129. {CMprivate, "private", 1},
  130. {CMprofile, "profile", 1},
  131. {CMstart, "start", 1},
  132. {CMstartstop, "startstop", 1},
  133. {CMstartsyscall, "startsyscall", 1},
  134. {CMstop, "stop", 1},
  135. {CMwaitstop, "waitstop", 1},
  136. {CMwired, "wired", 2},
  137. {CMtrace, "trace", 0},
  138. {CMperiod, "period", 2},
  139. {CMdeadline, "deadline", 2},
  140. {CMcost, "cost", 2},
  141. {CMsporadic, "sporadic", 1},
  142. {CMdeadlinenotes, "deadlinenotes", 1},
  143. {CMadmit, "admit", 1},
  144. {CMextra, "extra", 1},
  145. {CMexpel, "expel", 1},
  146. {CMevent, "event", 1},
  147. {CMcore, "core", 2},
  148. {CMstraceme, "straceme", 0},
  149. {CMstraceall, "straceall", 0},
  150. {CMstraceoff, "straceoff", 0},
  151. };
  152. /*
  153. * Qids are, in path:
  154. * 4 bits of file type (qids above)
  155. * 23 bits of process slot number + 1
  156. * in vers,
  157. * 32 bits of pid, for consistency checking
  158. * If notepg, c->pgrpid.path is pgrp slot, .vers is noteid.
  159. */
  160. #define QSHIFT 5 /* location in qid of proc slot # */
  161. #define SLOTBITS 23 /* number of bits in the slot */
  162. #define QIDMASK ((1<<QSHIFT)-1)
  163. #define SLOTMASK (((1<<SLOTBITS)-1) << QSHIFT)
  164. #define QID(q) ((((uint32_t)(q).path)&QIDMASK)>>0)
  165. #define SLOT(q) (((((uint32_t)(q).path)&SLOTMASK)>>QSHIFT)-1)
  166. #define PID(q) ((q).vers)
  167. #define NOTEID(q) ((q).vers)
  168. static void procctlreq(Proc*, char*, int);
  169. static int procctlmemio(Proc*, uintptr_t, int, void*, int);
  170. static Chan* proctext(Chan*, Proc*);
  171. static Segment* txt2data(Proc*, Segment*);
  172. static int procstopped(void*);
  173. static void mntscan(Mntwalk*, Proc*);
  174. static Traceevent *tevents;
  175. static char *tpids, *tpidsc, *tpidse;
  176. static Lock tlock;
  177. static int topens;
  178. static int tproduced, tconsumed;
  179. static void notrace(Proc*, int, int64_t);
  180. void (*proctrace)(Proc*, int, int64_t) = notrace;
  181. static void
  182. profclock(Ureg *ur, Timer *ti)
  183. {
  184. Proc *up = externup();
  185. Tos *tos;
  186. if(up == nil || up->state != Running)
  187. return;
  188. /* user profiling clock */
  189. if(userureg(ur)){
  190. tos = (Tos*)(USTKTOP-sizeof(Tos));
  191. tos->clock += TK2MS(1);
  192. segclock(userpc(ur));
  193. }
  194. }
  195. static int
  196. procgen(Chan *c, char *name, Dirtab *tab, int j, int s, Dir *dp)
  197. {
  198. Proc *up = externup();
  199. Qid qid;
  200. Proc *p;
  201. char *ename;
  202. int pid, sno;
  203. uint32_t path, perm, len;
  204. if(s == DEVDOTDOT){
  205. mkqid(&qid, Qdir, 0, QTDIR);
  206. devdir(c, qid, "#p", 0, eve, 0555, dp);
  207. return 1;
  208. }
  209. if(c->qid.path == Qdir){
  210. if(s == 0){
  211. strcpy(up->genbuf, "trace");
  212. mkqid(&qid, Qtrace, -1, QTFILE);
  213. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  214. return 1;
  215. }
  216. if(s == 1){
  217. strcpy(up->genbuf, "tracepids");
  218. mkqid(&qid, Qtracepids, -1, QTFILE);
  219. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  220. return 1;
  221. }
  222. s -= 2;
  223. if(name != nil){
  224. /* ignore s and use name to find pid */
  225. pid = strtol(name, &ename, 10);
  226. if(pid<=0 || ename[0]!='\0')
  227. return -1;
  228. s = psindex(pid);
  229. if(s < 0)
  230. return -1;
  231. }
  232. else if(s >= conf.nproc)
  233. return -1;
  234. if((p = psincref(s)) == nil || (pid = p->pid) == 0)
  235. return 0;
  236. snprint(up->genbuf, sizeof up->genbuf, "%u", pid);
  237. /*
  238. * String comparison is done in devwalk so
  239. * name must match its formatted pid.
  240. */
  241. if(name != nil && strcmp(name, up->genbuf) != 0)
  242. return -1;
  243. mkqid(&qid, (s+1)<<QSHIFT, pid, QTDIR);
  244. devdir(c, qid, up->genbuf, 0, p->user, DMDIR|0555, dp);
  245. psdecref(p);
  246. return 1;
  247. }
  248. if(c->qid.path == Qtrace){
  249. strcpy(up->genbuf, "trace");
  250. mkqid(&qid, Qtrace, -1, QTFILE);
  251. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  252. return 1;
  253. }
  254. if(c->qid.path == Qtracepids){
  255. strcpy(up->genbuf, "tracepids");
  256. mkqid(&qid, Qtrace, -1, QTFILE);
  257. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  258. return 1;
  259. }
  260. if(s >= nelem(procdir))
  261. return -1;
  262. if(tab)
  263. panic("procgen");
  264. tab = &procdir[s];
  265. path = c->qid.path&~(((1<<QSHIFT)-1)); /* slot component */
  266. if((p = psincref(SLOT(c->qid))) == nil)
  267. return -1;
  268. perm = tab->perm;
  269. if(perm == 0)
  270. perm = p->procmode;
  271. else /* just copy read bits */
  272. perm |= p->procmode & 0444;
  273. len = tab->length;
  274. switch(QID(c->qid)) {
  275. case Qwait:
  276. len = p->nwait; /* incorrect size, but >0 means there's something to read */
  277. break;
  278. case Qprofile: /* TODO(aki): test this */
  279. len = 0;
  280. for(sno = 0; sno < NSEG; sno++){
  281. if(p->seg[sno] != nil && (p->seg[sno]->type & SG_EXEC) != 0){
  282. Segment *s;
  283. s = p->seg[sno];
  284. if(s->profile)
  285. len += ((s->top-s->base)>>LRESPROF) * sizeof s->profile[0];
  286. }
  287. }
  288. break;
  289. }
  290. mkqid(&qid, path|tab->qid.path, c->qid.vers, QTFILE);
  291. devdir(c, qid, tab->name, len, p->user, perm, dp);
  292. psdecref(p);
  293. return 1;
  294. }
  295. static void
  296. notrace(Proc* p, int n, int64_t m)
  297. {
  298. }
  299. static Lock tlck;
  300. static void
  301. _proctrace(Proc* p, int etype, int64_t ts)
  302. {
  303. Traceevent *te;
  304. int tp;
  305. ilock(&tlck);
  306. if (p->trace == 0 || topens == 0 ||
  307. tproduced - tconsumed >= Nevents){
  308. iunlock(&tlck);
  309. return;
  310. }
  311. tp = tproduced++;
  312. iunlock(&tlck);
  313. te = &tevents[tp&Emask];
  314. te->pid = p->pid;
  315. te->etype = etype;
  316. if (ts == 0)
  317. te->time = todget(nil);
  318. else
  319. te->time = ts;
  320. te->core = machp()->machno;
  321. }
  322. void
  323. proctracepid(Proc *p)
  324. {
  325. if(p->trace == 1 && proctrace != notrace){
  326. p->trace = 2;
  327. ilock(&tlck);
  328. tpidsc = seprint(tpidsc, tpidse, "%d %s\n", p->pid, p->text);
  329. iunlock(&tlck);
  330. }
  331. }
  332. static void
  333. procinit(void)
  334. {
  335. if(conf.nproc >= (SLOTMASK>>QSHIFT) - 1)
  336. print("warning: too many procs for devproc\n");
  337. addclock0link((void (*)(void))profclock, 113); /* Relative prime to HZ */
  338. }
  339. static Chan*
  340. procattach(char *spec)
  341. {
  342. return devattach('p', spec);
  343. }
  344. static Walkqid*
  345. procwalk(Chan *c, Chan *nc, char **name, int nname)
  346. {
  347. return devwalk(c, nc, name, nname, 0, 0, procgen);
  348. }
  349. static int32_t
  350. procstat(Chan *c, uint8_t *db, int32_t n)
  351. {
  352. return devstat(c, db, n, 0, 0, procgen);
  353. }
  354. /*
  355. * none can't read or write state on other
  356. * processes. This is to contain access of
  357. * servers running as none should they be
  358. * subverted by, for example, a stack attack.
  359. */
  360. static void
  361. nonone(Proc *p)
  362. {
  363. Proc *up = externup();
  364. if(p == up)
  365. return;
  366. if(strcmp(up->user, "none") != 0)
  367. return;
  368. if(iseve())
  369. return;
  370. error(Eperm);
  371. }
  372. static Chan*
  373. procopen(Chan *c, int omode)
  374. {
  375. Proc *up = externup();
  376. Proc *p;
  377. Pgrp *pg;
  378. Chan *tc;
  379. int pid;
  380. if(c->qid.type & QTDIR)
  381. return devopen(c, omode, 0, 0, procgen);
  382. if(QID(c->qid) == Qtrace){
  383. if (omode != OREAD)
  384. error(Eperm);
  385. lock(&tlock);
  386. if (waserror()){
  387. unlock(&tlock);
  388. nexterror();
  389. }
  390. if (topens > 0)
  391. error("already open");
  392. topens++;
  393. if (tevents == nil){
  394. tevents = (Traceevent*)malloc(sizeof(Traceevent) * Nevents);
  395. if(tevents == nil)
  396. error(Enomem);
  397. tpids = malloc(Ntracedpids * 20);
  398. if(tpids == nil){
  399. free(tpids);
  400. tpids = nil;
  401. error(Enomem);
  402. }
  403. tpidsc = tpids;
  404. tpidse = tpids + Ntracedpids * 20;
  405. *tpidsc = 0;
  406. tproduced = tconsumed = 0;
  407. }
  408. proctrace = _proctrace;
  409. poperror();
  410. unlock(&tlock);
  411. c->mode = openmode(omode);
  412. c->flag |= COPEN;
  413. c->offset = 0;
  414. return c;
  415. }
  416. if(QID(c->qid) == Qtracepids){
  417. if (omode != OREAD)
  418. error(Eperm);
  419. c->mode = openmode(omode);
  420. c->flag |= COPEN;
  421. c->offset = 0;
  422. return c;
  423. }
  424. if((p = psincref(SLOT(c->qid))) == nil)
  425. error(Eprocdied);
  426. qlock(&p->debug);
  427. if(waserror()){
  428. qunlock(&p->debug);
  429. psdecref(p);
  430. nexterror();
  431. }
  432. pid = PID(c->qid);
  433. if(p->pid != pid)
  434. error(Eprocdied);
  435. omode = openmode(omode);
  436. switch(QID(c->qid)){
  437. case Qtext:
  438. if(omode != OREAD)
  439. error(Eperm);
  440. tc = proctext(c, p);
  441. tc->offset = 0;
  442. poperror();
  443. qunlock(&p->debug);
  444. psdecref(p);
  445. cclose(c);
  446. return tc;
  447. case Qproc:
  448. case Qkregs:
  449. case Qsegment:
  450. case Qprofile:
  451. case Qfd:
  452. if(omode != OREAD)
  453. error(Eperm);
  454. break;
  455. case Qnote:
  456. if(p->privatemem)
  457. error(Eperm);
  458. break;
  459. case Qmem:
  460. case Qctl:
  461. if(p->privatemem)
  462. error(Eperm);
  463. nonone(p);
  464. break;
  465. case Qtls:
  466. if(p->pid != up->pid)
  467. error(Eperm);
  468. nonone(p);
  469. break;
  470. case Qargs:
  471. case Qnoteid:
  472. case Qstatus:
  473. case Qwait:
  474. case Qgdbregs:
  475. case Qregs:
  476. case Qfpregs:
  477. case Qsyscall:
  478. case Qcore:
  479. nonone(p);
  480. break;
  481. case Qpager:
  482. p->resp = qopen(1024, Qmsg, nil, 0);
  483. p->req = qopen(1024, Qmsg, nil, 0);
  484. print("p %d sets resp %p req %p\n", p->pid, p->resp, p->req);
  485. c->aux = p;
  486. break;
  487. case Qns:
  488. if(omode != OREAD)
  489. error(Eperm);
  490. c->aux = malloc(sizeof(Mntwalk));
  491. break;
  492. case Qnotepg:
  493. nonone(p);
  494. pg = p->pgrp;
  495. if(pg == nil)
  496. error(Eprocdied);
  497. if(omode!=OWRITE || pg->pgrpid == 1)
  498. error(Eperm);
  499. c->pgrpid.path = pg->pgrpid+1;
  500. c->pgrpid.vers = p->noteid;
  501. break;
  502. case Qstrace:
  503. if (!p->strace)
  504. error("Process does not have tracing enabled");
  505. incref(&p->strace->users);
  506. c->aux = p->strace;
  507. break;
  508. default:
  509. poperror();
  510. qunlock(&p->debug);
  511. psdecref(p);
  512. pprint("procopen %#llx\n", c->qid.path);
  513. error(Egreg);
  514. }
  515. /* Affix pid to qid */
  516. if(p->state != Dead)
  517. c->qid.vers = p->pid;
  518. /* make sure the process slot didn't get reallocated while we were playing */
  519. coherence();
  520. if(p->pid != pid)
  521. error(Eprocdied);
  522. tc = devopen(c, omode, 0, 0, procgen);
  523. poperror();
  524. qunlock(&p->debug);
  525. psdecref(p);
  526. return tc;
  527. }
  528. static int32_t
  529. procwstat(Chan *c, uint8_t *db, int32_t n)
  530. {
  531. Proc *up = externup();
  532. Proc *p;
  533. Dir *d;
  534. if(c->qid.type & QTDIR)
  535. error(Eperm);
  536. if(QID(c->qid) == Qtrace)
  537. return devwstat(c, db, n);
  538. if((p = psincref(SLOT(c->qid))) == nil)
  539. error(Eprocdied);
  540. nonone(p);
  541. d = nil;
  542. qlock(&p->debug);
  543. if(waserror()){
  544. qunlock(&p->debug);
  545. psdecref(p);
  546. free(d);
  547. nexterror();
  548. }
  549. if(p->pid != PID(c->qid))
  550. error(Eprocdied);
  551. if(strcmp(up->user, p->user) != 0 && strcmp(up->user, eve) != 0)
  552. error(Eperm);
  553. d = smalloc(sizeof(Dir)+n);
  554. n = convM2D(db, n, &d[0], (char*)&d[1]);
  555. if(n == 0)
  556. error(Eshortstat);
  557. if(!emptystr(d->uid) && strcmp(d->uid, p->user) != 0){
  558. if(strcmp(up->user, eve) != 0)
  559. error(Eperm);
  560. else
  561. kstrdup(&p->user, d->uid);
  562. }
  563. if(d->mode != (uint32_t)~0UL)
  564. p->procmode = d->mode&0777;
  565. poperror();
  566. qunlock(&p->debug);
  567. psdecref(p);
  568. free(d);
  569. return n;
  570. }
  571. static int32_t
  572. procoffset(int32_t offset, char *va, int *np)
  573. {
  574. if(offset > 0) {
  575. offset -= *np;
  576. if(offset < 0) {
  577. memmove(va, va+*np+offset, -offset);
  578. *np = -offset;
  579. }
  580. else
  581. *np = 0;
  582. }
  583. return offset;
  584. }
  585. static int
  586. procqidwidth(Chan *c)
  587. {
  588. char buf[32];
  589. return sprint(buf, "%lu", c->qid.vers);
  590. }
  591. int
  592. procfdprint(Chan *c, int fd, int w, char *s, int ns)
  593. {
  594. int n;
  595. if(w == 0)
  596. w = procqidwidth(c);
  597. n = snprint(s, ns, "%3d %.2s %C %4ud (%.16llx %*lu %.2x) %5ld %8lld %s\n",
  598. fd,
  599. &"r w rw"[(c->mode&3)<<1],
  600. c->dev->dc, c->devno,
  601. c->qid.path, w, c->qid.vers, c->qid.type,
  602. c->iounit, c->offset, c->path->s);
  603. return n;
  604. }
  605. static int
  606. procfds(Proc *p, char *va, int count, int32_t offset)
  607. {
  608. Proc *up = externup();
  609. Fgrp *f;
  610. Chan *c;
  611. char buf[256];
  612. int n, i, w, ww;
  613. char *a;
  614. /* print to buf to avoid holding fgrp lock while writing to user space */
  615. if(count > sizeof buf)
  616. count = sizeof buf;
  617. a = buf;
  618. qlock(&p->debug);
  619. f = p->fgrp;
  620. if(f == nil){
  621. qunlock(&p->debug);
  622. return 0;
  623. }
  624. lock(&f->r.l);
  625. if(waserror()){
  626. unlock(&f->r.l);
  627. qunlock(&p->debug);
  628. nexterror();
  629. }
  630. n = readstr(0, a, count, p->dot->path->s);
  631. n += snprint(a+n, count-n, "\n");
  632. offset = procoffset(offset, a, &n);
  633. /* compute width of qid.path */
  634. w = 0;
  635. for(i = 0; i <= f->maxfd; i++) {
  636. c = f->fd[i];
  637. if(c == nil)
  638. continue;
  639. ww = procqidwidth(c);
  640. if(ww > w)
  641. w = ww;
  642. }
  643. for(i = 0; i <= f->maxfd; i++) {
  644. c = f->fd[i];
  645. if(c == nil)
  646. continue;
  647. n += procfdprint(c, i, w, a+n, count-n);
  648. offset = procoffset(offset, a, &n);
  649. }
  650. poperror();
  651. unlock(&f->r.l);
  652. qunlock(&p->debug);
  653. /* copy result to user space, now that locks are released */
  654. memmove(va, buf, n);
  655. return n;
  656. }
  657. static void
  658. procclose(Chan * c)
  659. {
  660. if(QID(c->qid) == Qtrace){
  661. lock(&tlock);
  662. if(topens > 0)
  663. topens--;
  664. if(topens == 0)
  665. proctrace = notrace;
  666. unlock(&tlock);
  667. }
  668. if (QID(c->qid) == Qsyscall) {
  669. if (c->aux)
  670. qclose(c->aux);
  671. c->aux = nil;
  672. }
  673. if(QID(c->qid) == Qpager){
  674. print("leaking queueus for pager\n");
  675. }
  676. if(QID(c->qid) == Qns && c->aux != 0)
  677. free(c->aux);
  678. if (QID(c->qid) == Qstrace && c->aux != 0) {
  679. Strace *s = c->aux;
  680. // TODO: leak!
  681. print("are we leaking strace?\n");
  682. decref(&s->users);
  683. c->aux = nil;
  684. }
  685. }
  686. static void
  687. int2flag(int flag, char *s)
  688. {
  689. if(flag == 0){
  690. *s = '\0';
  691. return;
  692. }
  693. *s++ = '-';
  694. if(flag & MAFTER)
  695. *s++ = 'a';
  696. if(flag & MBEFORE)
  697. *s++ = 'b';
  698. if(flag & MCREATE)
  699. *s++ = 'c';
  700. if(flag & MCACHE)
  701. *s++ = 'C';
  702. *s = '\0';
  703. }
  704. static int
  705. procargs(Proc *p, char *buf, int nbuf)
  706. {
  707. int j, k, m;
  708. char *a;
  709. int n;
  710. a = p->args;
  711. if(p->setargs){
  712. snprint(buf, nbuf, "%s [%s]", p->text, p->args);
  713. return strlen(buf);
  714. }
  715. n = p->nargs;
  716. for(j = 0; j < nbuf - 1; j += m){
  717. if(n <= 0)
  718. break;
  719. if(j != 0)
  720. buf[j++] = ' ';
  721. m = snprint(buf+j, nbuf-j, "%q", a);
  722. k = strlen(a) + 1;
  723. a += k;
  724. n -= k;
  725. }
  726. return j;
  727. }
  728. static int
  729. eventsavailable(void *v)
  730. {
  731. return tproduced > tconsumed;
  732. }
  733. static int32_t
  734. procread(Chan *c, void *va, int32_t n, int64_t off)
  735. {
  736. Strace *strace;
  737. Proc *up = externup();
  738. Proc *p;
  739. Mach *ac, *wired;
  740. int32_t l, r;
  741. Waitq *wq;
  742. Ureg kur;
  743. uint8_t *rptr;
  744. Confmem *cm;
  745. Mntwalk *mw;
  746. Segment *sg, *s;
  747. int i, j, navail, pid, rsize, sno;
  748. char flag[10], *sps, *srv, *statbuf;
  749. uintptr_t offset, profoff, u;
  750. int tesz;
  751. uintptr_t gdbregs[DBG_MAX_REG_NUM];
  752. if(c->qid.type & QTDIR)
  753. return devdirread(c, va, n, 0, 0, procgen);
  754. offset = off;
  755. if(QID(c->qid) == Qtrace){
  756. if(!eventsavailable(nil))
  757. return 0;
  758. rptr = va;
  759. tesz = BIT32SZ + BIT32SZ + BIT64SZ + BIT32SZ;
  760. navail = tproduced - tconsumed;
  761. if(navail > n / tesz)
  762. navail = n / tesz;
  763. while(navail > 0) {
  764. PBIT32(rptr, tevents[tconsumed & Emask].pid);
  765. rptr += BIT32SZ;
  766. PBIT32(rptr, tevents[tconsumed & Emask].etype);
  767. rptr += BIT32SZ;
  768. PBIT64(rptr, tevents[tconsumed & Emask].time);
  769. rptr += BIT64SZ;
  770. PBIT32(rptr, tevents[tconsumed & Emask].core);
  771. rptr += BIT32SZ;
  772. tconsumed++;
  773. navail--;
  774. }
  775. return rptr - (uint8_t*)va;
  776. }
  777. if(QID(c->qid) == Qtracepids){
  778. if(tpids == nil)
  779. return 0;
  780. else
  781. return readstr(off, va, n, tpids);
  782. }
  783. if((p = psincref(SLOT(c->qid))) == nil || p->pid != PID(c->qid))
  784. error(Eprocdied);
  785. switch(QID(c->qid)){
  786. default:
  787. psdecref(p);
  788. break;
  789. case Qargs:
  790. qlock(&p->debug);
  791. j = procargs(p, up->genbuf, sizeof up->genbuf);
  792. qunlock(&p->debug);
  793. psdecref(p);
  794. if(offset >= j)
  795. return 0;
  796. if(offset+n > j)
  797. n = j-offset;
  798. memmove(va, &up->genbuf[offset], n);
  799. return n;
  800. case Qsyscall:
  801. if(p->syscalltrace == nil)
  802. return 0;
  803. return readstr(offset, va, n, p->syscalltrace);
  804. case Qcore:
  805. i = 0;
  806. ac = p->ac;
  807. wired = p->wired;
  808. if(ac != nil)
  809. i = ac->machno;
  810. else if(wired != nil)
  811. i = wired->machno;
  812. statbuf = smalloc(STATSIZE);
  813. snprint(statbuf, STATSIZE, "%d\n", i);
  814. n = readstr(offset, va, n, statbuf);
  815. free(statbuf);
  816. return n;
  817. case Qmem:
  818. if(offset < KZERO || (offset >= USTKTOP-USTKSIZE && offset < USTKTOP)){
  819. r = procctlmemio(p, offset, n, va, 1);
  820. psdecref(p);
  821. return r;
  822. }
  823. if(!iseve()){
  824. psdecref(p);
  825. error(Eperm);
  826. }
  827. /* validate kernel addresses */
  828. if(offset < PTR2UINT(end)) {
  829. if(offset+n > PTR2UINT(end))
  830. n = PTR2UINT(end) - offset;
  831. memmove(va, UINT2PTR(offset), n);
  832. psdecref(p);
  833. return n;
  834. }
  835. for(i=0; i<nelem(conf.mem); i++){
  836. cm = &conf.mem[i];
  837. /* klimit-1 because klimit might be zero! */
  838. if(cm->kbase <= offset && offset <= cm->klimit-1){
  839. if(offset+n >= cm->klimit-1)
  840. n = cm->klimit - offset;
  841. memmove(va, UINT2PTR(offset), n);
  842. psdecref(p);
  843. return n;
  844. }
  845. }
  846. psdecref(p);
  847. error(Ebadarg);
  848. case Qprofile:
  849. profoff = 0;
  850. for(sno = 0; sno < NSEG; sno++){
  851. if(p->seg[sno] == nil)
  852. continue;
  853. if((p->seg[sno]->type & SG_EXEC) == 0)
  854. continue;
  855. if(p->seg[sno]->profile == nil)
  856. continue;
  857. s = p->seg[sno];
  858. i = ((s->top-s->base)>>LRESPROF) * sizeof s->profile[0];
  859. if(offset >= profoff+i){
  860. profoff += i;
  861. continue;
  862. }
  863. if(offset+n > profoff+i)
  864. n = profoff+i - offset;
  865. memmove(va, ((char*)s->profile)+(offset-profoff), n);
  866. psdecref(p);
  867. return n;
  868. }
  869. psdecref(p);
  870. if(sno == NSEG)
  871. error("profile is off");
  872. return 0;
  873. case Qnote:
  874. qlock(&p->debug);
  875. if(waserror()){
  876. qunlock(&p->debug);
  877. psdecref(p);
  878. nexterror();
  879. }
  880. if(p->pid != PID(c->qid))
  881. error(Eprocdied);
  882. if(n < 1) /* must accept at least the '\0' */
  883. error(Etoosmall);
  884. if(p->nnote == 0)
  885. n = 0;
  886. else {
  887. i = strlen(p->note[0].msg) + 1;
  888. if(i > n)
  889. i = n;
  890. rptr = va;
  891. memmove(rptr, p->note[0].msg, i);
  892. rptr[i-1] = '\0';
  893. p->nnote--;
  894. memmove(p->note, p->note+1, p->nnote*sizeof(Note));
  895. n = i;
  896. }
  897. if(p->nnote == 0)
  898. p->notepending = 0;
  899. poperror();
  900. qunlock(&p->debug);
  901. psdecref(p);
  902. return n;
  903. case Qproc:
  904. if(offset >= sizeof(Proc)){
  905. psdecref(p);
  906. return 0;
  907. }
  908. if(offset+n > sizeof(Proc))
  909. n = sizeof(Proc) - offset;
  910. memmove(va, ((char*)p)+offset, n);
  911. psdecref(p);
  912. return n;
  913. case Qregs:
  914. rptr = (uint8_t*)p->dbgreg;
  915. rsize = sizeof(Ureg);
  916. regread:
  917. if(rptr == 0){
  918. psdecref(p);
  919. error(Enoreg);
  920. }
  921. if(offset >= rsize){
  922. psdecref(p);
  923. return 0;
  924. }
  925. if(offset+n > rsize)
  926. n = rsize - offset;
  927. memmove(va, rptr+offset, n);
  928. psdecref(p);
  929. return n;
  930. /* Sorry about the code duplication. TODO: clean this up? */
  931. case Qgdbregs:
  932. rptr = (uint8_t*)&gdbregs[0];
  933. // not sizeof; it's an odd number of 32-bit words ... yuck.
  934. rsize = GDB_NUMREGBYTES;
  935. if(rptr == 0){
  936. psdecref(p);
  937. error(Enoreg);
  938. }
  939. if(offset >= rsize){
  940. psdecref(p);
  941. return 0;
  942. }
  943. if(offset+n > rsize)
  944. n = rsize - offset;
  945. ureg2gdb(p->dbgreg, gdbregs);
  946. print("Qgdbregs: va %p, rptr +offset %p, n %d\n", va, rptr+offset, n);
  947. memmove(va, rptr+offset, n);
  948. psdecref(p);
  949. return n;
  950. case Qkregs:
  951. memset(&kur, 0, sizeof(Ureg));
  952. setkernur(&kur, p);
  953. rptr = (uint8_t*)&kur;
  954. rsize = sizeof(Ureg);
  955. goto regread;
  956. case Qfpregs:
  957. r = fpudevprocio(p, va, n, offset, 0);
  958. psdecref(p);
  959. return r;
  960. case Qstrace:
  961. strace = c->aux;
  962. n = qread(strace->q, va, n);
  963. return n;
  964. case Qstatus:
  965. if(offset >= STATSIZE){
  966. psdecref(p);
  967. return 0;
  968. }
  969. if(offset+n > STATSIZE)
  970. n = STATSIZE - offset;
  971. sps = p->psstate;
  972. if(sps == 0)
  973. sps = statename[p->state];
  974. statbuf = smalloc(STATSIZE);
  975. memset(statbuf, ' ', STATSIZE);
  976. sprint(statbuf, "%-*.*s%-*.*s%-12.11s",
  977. KNAMELEN, KNAMELEN-1, p->text,
  978. KNAMELEN, KNAMELEN-1, p->user,
  979. sps);
  980. j = 2*KNAMELEN + 12;
  981. for(i = 0; i < 6; i++) {
  982. l = p->time[i];
  983. if(i == TReal)
  984. l = sys->ticks - l;
  985. l = TK2MS(l);
  986. readnum(0, statbuf+j+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
  987. }
  988. /* ignore stacks, which are typically not faulted in */
  989. u = 0;
  990. for(i=0; i<NSEG; i++){
  991. s = p->seg[i];
  992. if(s != nil && (s->type&SG_TYPE) != SG_STACK)
  993. u += s->top - s->base;
  994. }
  995. readnum(0, statbuf+j+NUMSIZE*6, NUMSIZE, u>>10u, NUMSIZE); /* wrong size */
  996. readnum(0, statbuf+j+NUMSIZE*7, NUMSIZE, p->basepri, NUMSIZE);
  997. readnum(0, statbuf+j+NUMSIZE*8, NUMSIZE, p->priority, NUMSIZE);
  998. /*
  999. * NIX: added # of traps, syscalls, and iccs
  1000. */
  1001. readnum(0, statbuf+j+NUMSIZE*9, NUMSIZE, p->ntrap, NUMSIZE);
  1002. readnum(0, statbuf+j+NUMSIZE*10, NUMSIZE, p->nintr, NUMSIZE);
  1003. readnum(0, statbuf+j+NUMSIZE*11, NUMSIZE, p->nsyscall, NUMSIZE);
  1004. readnum(0, statbuf+j+NUMSIZE*12, NUMSIZE, p->nicc, NUMSIZE);
  1005. readnum(0, statbuf+j+NUMSIZE*13, NUMSIZE, p->nactrap, NUMSIZE);
  1006. readnum(0, statbuf+j+NUMSIZE*14, NUMSIZE, p->nacsyscall, NUMSIZE);
  1007. /*
  1008. * external pager support, random stuff.
  1009. */
  1010. if (0) print("qstatus p %p pid %d req %p\n", p, p->pid, p->req);
  1011. readnum(0,statbuf+j+NUMSIZE*15, NUMSIZE, p->req ? 1 : 0, NUMSIZE);
  1012. readnum(0,statbuf+j+NUMSIZE*16, NUMSIZE, p->resp ? 1 : 0, NUMSIZE);
  1013. statbuf[j+NUMSIZE*17] = '\n';
  1014. if(offset+n > j+NUMSIZE*17+1)
  1015. n = j+NUMSIZE*17+1-offset;
  1016. /*
  1017. if (p->strace)
  1018. s = seprintf(s, e, " %d trace users %d traced procs",
  1019. kref_refcnt(&p->strace->users),
  1020. kref_refcnt(&p->strace->procs));
  1021. */
  1022. memmove(va, statbuf+offset, n);
  1023. free(statbuf);
  1024. psdecref(p);
  1025. return n;
  1026. case Qsegment:
  1027. j = 0;
  1028. statbuf = smalloc(STATSIZE);
  1029. for(i = 0; i < NSEG; i++) {
  1030. sg = p->seg[i];
  1031. if(sg == 0)
  1032. continue;
  1033. j += sprint(statbuf+j, "%-6s %c%c%c%c %p %p %4d\n",
  1034. segtypes[sg->type&SG_TYPE],
  1035. (sg->type&SG_READ) != 0 ? 'r' : '-',
  1036. (sg->type&SG_WRITE) != 0 ? 'w' : '-',
  1037. (sg->type&SG_EXEC) != 0 ? 'x' : '-',
  1038. sg->profile ? 'P' : '-',
  1039. sg->base, sg->top, sg->r.ref);
  1040. }
  1041. psdecref(p);
  1042. if(offset >= j){
  1043. free(statbuf);
  1044. return 0;
  1045. }
  1046. if(offset+n > j)
  1047. n = j-offset;
  1048. if(n == 0 && offset == 0){
  1049. free(statbuf);
  1050. exhausted("segments");
  1051. }
  1052. memmove(va, statbuf+offset, n);
  1053. free(statbuf);
  1054. return n;
  1055. case Qwait:
  1056. if(!canqlock(&p->qwaitr)){
  1057. psdecref(p);
  1058. error(Einuse);
  1059. }
  1060. if(waserror()) {
  1061. qunlock(&p->qwaitr);
  1062. psdecref(p);
  1063. nexterror();
  1064. }
  1065. lock(&p->exl);
  1066. if(up == p && p->nchild == 0 && p->waitq == 0) {
  1067. unlock(&p->exl);
  1068. error(Enochild);
  1069. }
  1070. pid = p->pid;
  1071. while(p->waitq == 0) {
  1072. unlock(&p->exl);
  1073. sleep(&p->waitr, haswaitq, p);
  1074. if(p->pid != pid)
  1075. error(Eprocdied);
  1076. lock(&p->exl);
  1077. }
  1078. wq = p->waitq;
  1079. p->waitq = wq->next;
  1080. p->nwait--;
  1081. unlock(&p->exl);
  1082. poperror();
  1083. qunlock(&p->qwaitr);
  1084. psdecref(p);
  1085. n = snprint(va, n, "%d %lu %lu %lu %q",
  1086. wq->w.pid,
  1087. wq->w.time[TUser], wq->w.time[TSys], wq->w.time[TReal],
  1088. wq->w.msg);
  1089. free(wq);
  1090. return n;
  1091. case Qns:
  1092. qlock(&p->debug);
  1093. if(waserror()){
  1094. qunlock(&p->debug);
  1095. psdecref(p);
  1096. nexterror();
  1097. }
  1098. if(p->pgrp == nil || p->pid != PID(c->qid))
  1099. error(Eprocdied);
  1100. mw = c->aux;
  1101. if(mw->cddone){
  1102. poperror();
  1103. qunlock(&p->debug);
  1104. psdecref(p);
  1105. return 0;
  1106. }
  1107. mntscan(mw, p);
  1108. if(mw->mh == 0){
  1109. mw->cddone = 1;
  1110. i = snprint(va, n, "cd %s\n", p->dot->path->s);
  1111. poperror();
  1112. qunlock(&p->debug);
  1113. psdecref(p);
  1114. return i;
  1115. }
  1116. int2flag(mw->cm->mflag, flag);
  1117. if(strcmp(mw->cm->to->path->s, "#M") == 0){
  1118. srv = srvname(mw->cm->to->mchan);
  1119. i = snprint(va, n, "mount %s %s %s %s\n", flag,
  1120. srv==nil? mw->cm->to->mchan->path->s : srv,
  1121. mw->mh->from->path->s, mw->cm->spec? mw->cm->spec : "");
  1122. free(srv);
  1123. }else
  1124. i = snprint(va, n, "bind %s %s %s\n", flag,
  1125. mw->cm->to->path->s, mw->mh->from->path->s);
  1126. poperror();
  1127. qunlock(&p->debug);
  1128. psdecref(p);
  1129. return i;
  1130. case Qnoteid:
  1131. r = readnum(offset, va, n, p->noteid, NUMSIZE);
  1132. psdecref(p);
  1133. return r;
  1134. case Qfd:
  1135. r = procfds(p, va, n, offset);
  1136. psdecref(p);
  1137. return r;
  1138. case Qtls:
  1139. statbuf = smalloc(STATSIZE);
  1140. j = snprint(statbuf, STATSIZE, "tls 0x%p\n", p->tls);
  1141. psdecref(p);
  1142. if(offset >= j){
  1143. free(statbuf);
  1144. return 0;
  1145. }
  1146. if(offset+n > j)
  1147. n = j-offset;
  1148. memmove(va, statbuf+offset, n);
  1149. free(statbuf);
  1150. return n;
  1151. case Qpager:
  1152. p = c->aux;
  1153. n = qread(p->req, va, n);
  1154. print("read pager: %p\n", n);
  1155. break;
  1156. }
  1157. error(Egreg);
  1158. return 0; /* not reached */
  1159. }
  1160. static void
  1161. mntscan(Mntwalk *mw, Proc *p)
  1162. {
  1163. Pgrp *pg;
  1164. Mount *t;
  1165. Mhead *f;
  1166. int best, i, last, nxt;
  1167. pg = p->pgrp;
  1168. rlock(&pg->ns);
  1169. nxt = 0;
  1170. best = (int)(~0U>>1); /* largest 2's complement int */
  1171. last = 0;
  1172. if(mw->mh)
  1173. last = mw->cm->mountid;
  1174. for(i = 0; i < MNTHASH; i++) {
  1175. for(f = pg->mnthash[i]; f; f = f->hash) {
  1176. for(t = f->mount; t; t = t->next) {
  1177. if(mw->mh == 0 ||
  1178. (t->mountid > last && t->mountid < best)) {
  1179. mw->cm = t;
  1180. mw->mh = f;
  1181. best = mw->cm->mountid;
  1182. nxt = 1;
  1183. }
  1184. }
  1185. }
  1186. }
  1187. if(nxt == 0)
  1188. mw->mh = 0;
  1189. runlock(&pg->ns);
  1190. }
  1191. static int32_t
  1192. procwrite(Chan *c, void *va, int32_t n, int64_t off)
  1193. {
  1194. Proc *up = externup();
  1195. Proc *p, *t;
  1196. int i, id, l;
  1197. char *args, buf[ERRMAX];
  1198. uintptr_t offset;
  1199. if(c->qid.type & QTDIR)
  1200. error(Eisdir);
  1201. /* Use the remembered noteid in the channel rather
  1202. * than the process pgrpid
  1203. */
  1204. if(QID(c->qid) == Qnotepg) {
  1205. pgrpnote(NOTEID(c->pgrpid), va, n, NUser);
  1206. return n;
  1207. }
  1208. if((p = psincref(SLOT(c->qid))) == nil)
  1209. error(Eprocdied);
  1210. qlock(&p->debug);
  1211. if(waserror()){
  1212. qunlock(&p->debug);
  1213. psdecref(p);
  1214. nexterror();
  1215. }
  1216. if(p->pid != PID(c->qid))
  1217. error(Eprocdied);
  1218. offset = off;
  1219. switch(QID(c->qid)){
  1220. case Qargs:
  1221. if(n == 0)
  1222. error(Eshort);
  1223. if(n >= ERRMAX)
  1224. error(Etoobig);
  1225. memmove(buf, va, n);
  1226. args = malloc(n+1);
  1227. if(args == nil)
  1228. error(Enomem);
  1229. memmove(args, buf, n);
  1230. l = n;
  1231. if(args[l-1] != 0)
  1232. args[l++] = 0;
  1233. free(p->args);
  1234. p->nargs = l;
  1235. p->args = args;
  1236. p->setargs = 1;
  1237. break;
  1238. case Qmem:
  1239. if(p->state != Stopped)
  1240. error(Ebadctl);
  1241. n = procctlmemio(p, offset, n, va, 0);
  1242. break;
  1243. case Qregs:
  1244. if(offset >= sizeof(Ureg))
  1245. n = 0;
  1246. else if(offset+n > sizeof(Ureg))
  1247. n = sizeof(Ureg) - offset;
  1248. if(p->dbgreg == 0)
  1249. error(Enoreg);
  1250. setregisters(p->dbgreg, (char*)(p->dbgreg)+offset, va, n);
  1251. break;
  1252. case Qfpregs:
  1253. n = fpudevprocio(p, va, n, offset, 1);
  1254. break;
  1255. case Qctl:
  1256. procctlreq(p, va, n);
  1257. break;
  1258. case Qnote:
  1259. if(p->kp)
  1260. error(Eperm);
  1261. if(n >= ERRMAX-1)
  1262. error(Etoobig);
  1263. memmove(buf, va, n);
  1264. buf[n] = 0;
  1265. if(!postnote(p, 0, buf, NUser))
  1266. error("note not posted");
  1267. break;
  1268. case Qnoteid:
  1269. id = atoi(va);
  1270. if(id == p->pid) {
  1271. p->noteid = id;
  1272. break;
  1273. }
  1274. for(i = 0; (t = psincref(i)) != nil; i++){
  1275. if(t->state == Dead || t->noteid != id){
  1276. psdecref(t);
  1277. continue;
  1278. }
  1279. if(strcmp(p->user, t->user) != 0){
  1280. psdecref(t);
  1281. error(Eperm);
  1282. }
  1283. psdecref(t);
  1284. p->noteid = id;
  1285. break;
  1286. }
  1287. if(p->noteid != id)
  1288. error(Ebadarg);
  1289. break;
  1290. case Qtls:
  1291. if(n >= sizeof buf)
  1292. error(Etoobig);
  1293. memmove(buf, va, n);
  1294. buf[n] = '\0';
  1295. if(memcmp(buf, "tls ", 4) == 0){
  1296. char *s;
  1297. for(s = buf; *s != '\0' && (*s < '0' || *s > '9'); s++)
  1298. ;
  1299. if(*s >= '0' && *s <= '9'){
  1300. p->tls = (uintptr_t)strtoull(s, nil, 0); // a-tol-whex! a-tol-whex!
  1301. poperror();
  1302. qunlock(&p->debug);
  1303. psdecref(p);
  1304. return n;
  1305. }
  1306. }
  1307. error(Ebadarg);
  1308. case Qpager:
  1309. p = c->aux;
  1310. if (p && p->resp)
  1311. n = qwrite(p->resp, va, n);
  1312. break;
  1313. /* this lets your write a marker into the data stream,
  1314. * which is a very powerful tool. */
  1315. case Qstrace:
  1316. assert(c->aux);
  1317. /* it is possible that the q hungup and is closed. that would be
  1318. * the case if all of the procs closed and decref'd. if the q is
  1319. * closed, qwrite() will throw an error. */
  1320. n = qwrite(((Strace*)c->aux)->q, va, n);
  1321. break;
  1322. default:
  1323. poperror();
  1324. qunlock(&p->debug);
  1325. psdecref(p);
  1326. pprint("unknown qid %#llx in procwrite\n", c->qid.path);
  1327. error(Egreg);
  1328. }
  1329. poperror();
  1330. qunlock(&p->debug);
  1331. psdecref(p);
  1332. return n;
  1333. }
  1334. Dev procdevtab = {
  1335. .dc = 'p',
  1336. .name = "proc",
  1337. .reset = devreset,
  1338. .init = procinit,
  1339. .shutdown = devshutdown,
  1340. .attach = procattach,
  1341. .walk = procwalk,
  1342. .stat = procstat,
  1343. .open = procopen,
  1344. .create = devcreate,
  1345. .close = procclose,
  1346. .read = procread,
  1347. .bread = devbread,
  1348. .write = procwrite,
  1349. .bwrite = devbwrite,
  1350. .remove = devremove,
  1351. .wstat = procwstat,
  1352. };
  1353. static Chan*
  1354. proctext(Chan *c, Proc *p)
  1355. {
  1356. Proc *up = externup();
  1357. Chan *tc;
  1358. Image *i;
  1359. Segment *s;
  1360. int sno;
  1361. for(sno = 0; sno < NSEG; sno++)
  1362. if(p->seg[sno] != nil)
  1363. if((p->seg[sno]->type & SG_EXEC) != 0)
  1364. break;
  1365. if(sno == NSEG)
  1366. error(Enonexist);
  1367. s = p->seg[sno];
  1368. if(p->state==Dead)
  1369. error(Eprocdied);
  1370. lock(&s->r.l);
  1371. i = s->image;
  1372. if(i == 0) {
  1373. unlock(&s->r.l);
  1374. error(Eprocdied);
  1375. }
  1376. unlock(&s->r.l);
  1377. lock(&i->r.l);
  1378. if(waserror()) {
  1379. unlock(&i->r.l);
  1380. nexterror();
  1381. }
  1382. tc = i->c;
  1383. if(tc == 0)
  1384. error(Eprocdied);
  1385. if(incref(&tc->r) == 1 || (tc->flag&COPEN) == 0 || tc->mode!=OREAD) {
  1386. cclose(tc);
  1387. error(Eprocdied);
  1388. }
  1389. if(p->pid != PID(c->qid)){
  1390. cclose(tc);
  1391. error(Eprocdied);
  1392. }
  1393. poperror();
  1394. unlock(&i->r.l);
  1395. return tc;
  1396. }
  1397. void
  1398. procstopwait(Proc *p, int ctl)
  1399. {
  1400. Proc *up = externup();
  1401. int pid;
  1402. if(p->pdbg)
  1403. error(Einuse);
  1404. if(procstopped(p) || p->state == Broken)
  1405. return;
  1406. if(ctl != 0)
  1407. p->procctl = ctl;
  1408. p->pdbg = up;
  1409. pid = p->pid;
  1410. qunlock(&p->debug);
  1411. up->psstate = "Stopwait";
  1412. if(waserror()) {
  1413. p->pdbg = 0;
  1414. qlock(&p->debug);
  1415. nexterror();
  1416. }
  1417. sleep(&up->sleep, procstopped, p);
  1418. poperror();
  1419. qlock(&p->debug);
  1420. if(p->pid != pid)
  1421. error(Eprocdied);
  1422. }
  1423. static void
  1424. procctlcloseone(Proc *p, Fgrp *f, int fd)
  1425. {
  1426. Chan *c;
  1427. c = f->fd[fd];
  1428. if(c == nil)
  1429. return;
  1430. f->fd[fd] = nil;
  1431. unlock(&f->r.l);
  1432. qunlock(&p->debug);
  1433. cclose(c);
  1434. qlock(&p->debug);
  1435. lock(&f->r.l);
  1436. }
  1437. void
  1438. procctlclosefiles(Proc *p, int all, int fd)
  1439. {
  1440. int i;
  1441. Fgrp *f;
  1442. f = p->fgrp;
  1443. if(f == nil)
  1444. error(Eprocdied);
  1445. lock(&f->r.l);
  1446. f->r.ref++;
  1447. if(all)
  1448. for(i = 0; i < f->maxfd; i++)
  1449. procctlcloseone(p, f, i);
  1450. else
  1451. procctlcloseone(p, f, fd);
  1452. unlock(&f->r.l);
  1453. closefgrp(f);
  1454. }
  1455. static char *
  1456. parsetime(int64_t *rt, char *s)
  1457. {
  1458. uint64_t ticks;
  1459. uint32_t l;
  1460. char *e, *p;
  1461. static int p10[] = {100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};
  1462. if (s == nil)
  1463. return("missing value");
  1464. ticks=strtoul(s, &e, 10);
  1465. if (*e == '.'){
  1466. p = e+1;
  1467. l = strtoul(p, &e, 10);
  1468. if(e-p > nelem(p10))
  1469. return "too many digits after decimal point";
  1470. if(e-p == 0)
  1471. return "ill-formed number";
  1472. l *= p10[e-p-1];
  1473. }else
  1474. l = 0;
  1475. if (*e == '\0' || strcmp(e, "s") == 0){
  1476. ticks = 1000000000 * ticks + l;
  1477. }else if (strcmp(e, "ms") == 0){
  1478. ticks = 1000000 * ticks + l/1000;
  1479. }else if (strcmp(e, "µs") == 0 || strcmp(e, "us") == 0){
  1480. ticks = 1000 * ticks + l/1000000;
  1481. }else if (strcmp(e, "ns") != 0)
  1482. return "unrecognized unit";
  1483. *rt = ticks;
  1484. return nil;
  1485. }
  1486. #if 0
  1487. static void strace_shutdown(Strace *strace)
  1488. {
  1489. qhangup(strace->q, "No more traces");
  1490. }
  1491. static void strace_release(Strace *strace)
  1492. {
  1493. qfree(strace->q);
  1494. free(strace);
  1495. }
  1496. #endif
  1497. static void
  1498. procctlreq(Proc *p, char *va, int n)
  1499. {
  1500. Strace *strace;
  1501. Proc *up = externup();
  1502. Segment *s;
  1503. int npc, pri, core, sno;
  1504. Cmdbuf *cb;
  1505. Cmdtab *ct;
  1506. int64_t time;
  1507. char *e;
  1508. if(p->kp) /* no ctl requests to kprocs */
  1509. error(Eperm);
  1510. cb = parsecmd(va, n);
  1511. if(waserror()){
  1512. free(cb);
  1513. nexterror();
  1514. }
  1515. ct = lookupcmd(cb, proccmd, nelem(proccmd));
  1516. switch(ct->index){
  1517. case CMstraceall:
  1518. case CMstraceme:
  1519. /* common allocation. if we inherited, we might have one already */
  1520. if (!p->strace) {
  1521. print("Set up tracing for pid %d\n", p->pid);
  1522. strace = mallocz(sizeof(*p->strace), 1);
  1523. strace->q = qopen(65536, Qmsg|Qcoalesce, 0, 0);
  1524. /* both of these refs are put when the proc is freed. procs is for
  1525. * every process that has this p->strace. users is procs + every
  1526. * user (e.g. from open()).
  1527. */
  1528. incref(&strace->procs);
  1529. incref(&strace->users);
  1530. /* Do we need to worry about this? Or does the kernel protect it.
  1531. * I suspect that DMEXCL might help here.
  1532. * I.e. what if two procs do this command? */
  1533. p->strace = strace;
  1534. }
  1535. break;
  1536. }
  1537. switch(ct->index){
  1538. case CMclose:
  1539. procctlclosefiles(p, 0, atoi(cb->f[1]));
  1540. break;
  1541. case CMclosefiles:
  1542. procctlclosefiles(p, 1, 0);
  1543. break;
  1544. case CMhang:
  1545. p->hang = 1;
  1546. break;
  1547. case CMkill:
  1548. switch(p->state) {
  1549. case Broken:
  1550. unbreak(p);
  1551. break;
  1552. case Stopped:
  1553. case Semdown:
  1554. p->procctl = Proc_exitme;
  1555. postnote(p, 0, "sys: killed", NExit);
  1556. ready(p);
  1557. break;
  1558. default:
  1559. p->procctl = Proc_exitme;
  1560. postnote(p, 0, "sys: killed", NExit);
  1561. }
  1562. break;
  1563. case CMnohang:
  1564. p->hang = 0;
  1565. break;
  1566. case CMnoswap:
  1567. p->noswap = 1;
  1568. break;
  1569. case CMpri:
  1570. pri = atoi(cb->f[1]);
  1571. if(pri > PriNormal && !iseve())
  1572. error(Eperm);
  1573. procpriority(p, pri, 0);
  1574. break;
  1575. case CMfixedpri:
  1576. pri = atoi(cb->f[1]);
  1577. if(pri > PriNormal && !iseve())
  1578. error(Eperm);
  1579. procpriority(p, pri, 1);
  1580. break;
  1581. case CMprivate:
  1582. p->privatemem = 1;
  1583. break;
  1584. case CMprofile:
  1585. for(sno = 0; sno < NSEG; sno++){
  1586. if(p->seg[sno] != nil && (p->seg[sno]->type & SG_EXEC) != 0){
  1587. s = p->seg[sno];
  1588. if(s->profile != 0)
  1589. free(s->profile);
  1590. npc = (s->top-s->base)>>LRESPROF;
  1591. s->profile = malloc(npc * sizeof s->profile[0]);
  1592. if(s->profile == 0)
  1593. error(Enomem);
  1594. }
  1595. }
  1596. break;
  1597. case CMstart:
  1598. if(p->state != Stopped)
  1599. error(Ebadctl);
  1600. ready(p);
  1601. break;
  1602. case CMstartstop:
  1603. if(p->state != Stopped)
  1604. error(Ebadctl);
  1605. p->procctl = Proc_traceme;
  1606. ready(p);
  1607. procstopwait(p, Proc_traceme);
  1608. break;
  1609. case CMstartsyscall:
  1610. if(p->state != Stopped)
  1611. error(Ebadctl);
  1612. p->procctl = Proc_tracesyscall;
  1613. ready(p);
  1614. procstopwait(p, Proc_tracesyscall);
  1615. break;
  1616. case CMstop:
  1617. procstopwait(p, Proc_stopme);
  1618. break;
  1619. case CMwaitstop:
  1620. procstopwait(p, 0);
  1621. break;
  1622. case CMwired:
  1623. core = atoi(cb->f[1]);
  1624. procwired(p, core);
  1625. sched();
  1626. break;
  1627. case CMtrace:
  1628. switch(cb->nf){
  1629. case 1:
  1630. p->trace ^= 1;
  1631. break;
  1632. case 2:
  1633. p->trace = (atoi(cb->f[1]) != 0);
  1634. break;
  1635. default:
  1636. error("args");
  1637. }
  1638. break;
  1639. /* real time */
  1640. case CMperiod:
  1641. if(p->edf == nil)
  1642. edfinit(p);
  1643. if((e=parsetime(&time, cb->f[1])) != nil) /* time in ns */
  1644. error(e);
  1645. edfstop(p);
  1646. p->edf->T = time/1000; /* Edf times are in µs */
  1647. break;
  1648. case CMdeadline:
  1649. if(p->edf == nil)
  1650. edfinit(p);
  1651. if((e=parsetime(&time, cb->f[1])) != nil)
  1652. error(e);
  1653. edfstop(p);
  1654. p->edf->D = time/1000;
  1655. break;
  1656. case CMcost:
  1657. if(p->edf == nil)
  1658. edfinit(p);
  1659. if((e=parsetime(&time, cb->f[1])) != nil)
  1660. error(e);
  1661. edfstop(p);
  1662. p->edf->C = time/1000;
  1663. break;
  1664. case CMsporadic:
  1665. if(p->edf == nil)
  1666. edfinit(p);
  1667. p->edf->flags |= Sporadic;
  1668. break;
  1669. case CMdeadlinenotes:
  1670. if(p->edf == nil)
  1671. edfinit(p);
  1672. p->edf->flags |= Sendnotes;
  1673. break;
  1674. case CMadmit:
  1675. if(p->edf == 0)
  1676. error("edf params");
  1677. if((e = edfadmit(p)) != nil)
  1678. error(e);
  1679. break;
  1680. case CMextra:
  1681. if(p->edf == nil)
  1682. edfinit(p);
  1683. p->edf->flags |= Extratime;
  1684. break;
  1685. case CMexpel:
  1686. if(p->edf)
  1687. edfstop(p);
  1688. break;
  1689. case CMevent:
  1690. if(up->trace)
  1691. proctrace(up, SUser, 0);
  1692. break;
  1693. case CMcore:
  1694. core = atoi(cb->f[1]);
  1695. if(core >= MACHMAX)
  1696. error("wrong core number");
  1697. else if(core == 0){
  1698. if(p->ac == nil)
  1699. error("not running in an ac");
  1700. p->procctl = Proc_totc;
  1701. if(p != up && p->state == Exotic){
  1702. /* see the comment in postnote */
  1703. intrac(p);
  1704. }
  1705. }else{
  1706. if(p->ac != nil)
  1707. error("running in an ac");
  1708. if(core < 0)
  1709. p->ac = getac(p, -1);
  1710. else
  1711. p->ac = getac(p, core);
  1712. p->procctl = Proc_toac;
  1713. p->prepagemem = 1;
  1714. }
  1715. break;
  1716. case CMstraceme:
  1717. p->strace_on = 1;
  1718. p->strace_inherit = 0;
  1719. break;
  1720. case CMstraceall:
  1721. p->strace_on = 1;
  1722. p->strace_inherit = 1;
  1723. break;
  1724. case CMstraceoff:
  1725. p->strace_on = 0;
  1726. p->strace_inherit = 0;
  1727. break;
  1728. }
  1729. poperror();
  1730. free(cb);
  1731. }
  1732. static int
  1733. procstopped(void *a)
  1734. {
  1735. Proc *p = a;
  1736. return p->state == Stopped;
  1737. }
  1738. static int
  1739. procctlmemio(Proc *p, uintptr_t offset, int n, void *va, int read)
  1740. {
  1741. Proc *up = externup();
  1742. KMap *k;
  1743. Pte *pte;
  1744. Page *pg;
  1745. Segment *s;
  1746. uintptr_t soff, l; /* hmmmm */
  1747. uint8_t *b;
  1748. uintmem pgsz;
  1749. for(;;) {
  1750. s = seg(p, offset, 1);
  1751. if(s == 0)
  1752. error(Ebadarg);
  1753. if(offset+n >= s->top)
  1754. n = s->top-offset;
  1755. if(!read && ((s->type&SG_TYPE) == SG_TEXT || (s->type&SG_TYPE) == SG_LOAD))
  1756. s = txt2data(p, s);
  1757. s->steal++;
  1758. soff = offset-s->base;
  1759. if(waserror()) {
  1760. s->steal--;
  1761. nexterror();
  1762. }
  1763. if(fixfault(s, offset, read, 0, s->color) == 0)
  1764. break;
  1765. poperror();
  1766. s->steal--;
  1767. }
  1768. poperror();
  1769. pte = s->map[soff/PTEMAPMEM];
  1770. if(pte == 0)
  1771. panic("procctlmemio");
  1772. pgsz = sys->pgsz[s->pgszi];
  1773. pg = pte->pages[(soff&(PTEMAPMEM-1))/pgsz];
  1774. if(pagedout(pg))
  1775. panic("procctlmemio1");
  1776. l = pgsz - (offset&(pgsz-1));
  1777. if(n > l)
  1778. n = l;
  1779. k = kmap(pg);
  1780. if(waserror()) {
  1781. s->steal--;
  1782. kunmap(k);
  1783. nexterror();
  1784. }
  1785. b = (uint8_t*)VA(k);
  1786. b += offset&(pgsz-1);
  1787. if(read == 1)
  1788. memmove(va, b, n); /* This can fault */
  1789. else
  1790. memmove(b, va, n);
  1791. poperror();
  1792. kunmap(k);
  1793. /* Ensure the process sees text page changes */
  1794. if(s->flushme)
  1795. memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
  1796. s->steal--;
  1797. if(read == 0)
  1798. p->newtlb = 1;
  1799. return n;
  1800. }
  1801. static Segment*
  1802. txt2data(Proc *p, Segment *s)
  1803. {
  1804. int i;
  1805. Segment *ps;
  1806. ps = newseg(SG_DATA|SG_READ|SG_WRITE|SG_EXEC, s->base, s->size);
  1807. ps->image = s->image;
  1808. incref(&ps->image->r);
  1809. ps->ldseg = s->ldseg;
  1810. ps->flushme = 1;
  1811. qlock(&p->seglock);
  1812. for(i = 0; i < NSEG; i++)
  1813. if(p->seg[i] == s)
  1814. break;
  1815. if(i == NSEG)
  1816. panic("segment gone");
  1817. qunlock(&s->lk);
  1818. putseg(s);
  1819. qlock(&ps->lk);
  1820. p->seg[i] = ps;
  1821. qunlock(&p->seglock);
  1822. return ps;
  1823. }
  1824. Segment*
  1825. data2txt(Segment *s)
  1826. {
  1827. Segment *ps;
  1828. ps = newseg(SG_TEXT|SG_READ|SG_EXEC, s->base, s->size);
  1829. ps->image = s->image;
  1830. incref(&ps->image->r);
  1831. ps->ldseg = s->ldseg;
  1832. ps->flushme = 1;
  1833. return ps;
  1834. }