write.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <bio.h>
  12. #include "snap.h"
  13. char *pfile[Npfile] = {
  14. [Psegment] "segment",
  15. [Pfd] "fd",
  16. [Pfpregs] "fpregs",
  17. [Pnoteid] "noteid",
  18. [Pkregs] "kregs",
  19. [Pns] "ns",
  20. [Pproc] "proc",
  21. [Pregs] "regs",
  22. [Pstatus] "status",
  23. };
  24. static void
  25. writeseg(Biobuf *b, Proc *proc, Seg *s)
  26. {
  27. int i, npg;
  28. Page **pp, *p;
  29. int type;
  30. if(s == nil){
  31. Bprint(b, "%-11ud %-11ud ", 0, 0);
  32. return;
  33. }
  34. type = proc->text == s ? 't' : 'm';
  35. npg = (s->len+Pagesize-1)/Pagesize;
  36. if(npg != s->npg)
  37. abort();
  38. Bprint(b, "%-11llud %-11llud ", s->offset, s->len);
  39. if(s->len == 0)
  40. return;
  41. for(i=0, pp=s->pg, p=*pp; i<npg; i++, pp++, p=*pp) {
  42. if(p->written) {
  43. if(p->type == 'z') {
  44. Bprint(b, "z");
  45. continue;
  46. }
  47. Bprint(b, "%c%-11ld %-11llud ", p->type, p->pid, p->offset);
  48. } else {
  49. Bprint(b, "r");
  50. Bwrite(b, p->data, p->len);
  51. if(p->len != Pagesize && i != npg-1)
  52. abort();
  53. p->written = 1;
  54. p->type = type;
  55. p->offset = s->offset + i*Pagesize;
  56. p->pid = proc->pid;
  57. }
  58. }
  59. }
  60. void
  61. writesnap(Biobuf *b, Proc *p)
  62. {
  63. int i, n;
  64. Data *d;
  65. for(i=0; i<Npfile; i++)
  66. if(d = p->d[i]) {
  67. Bprint(b, "%-11ld %s\n%-11lud ", p->pid, pfile[i], d->len);
  68. Bwrite(b, d->data, d->len);
  69. }
  70. if(p->text) {
  71. Bprint(b, "%-11ld text\n", p->pid);
  72. writeseg(b, p, p->text);
  73. }
  74. if(n = p->nseg) {
  75. Bprint(b, "%-11ld mem\n%-11d ", p->pid, n);
  76. for(i=0; i<n; i++)
  77. writeseg(b, p, p->seg[i]);
  78. }
  79. }