summaryrefslogtreecommitdiff
path: root/libhurd-mm/as-dump.c
blob: c802a934553b603e606919a75a9eb875525a4766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* as-dump.c - Address space dumper.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include <viengoos/cap.h>
#include <viengoos/folio.h>
#include <viengoos/thread.h>
#include <viengoos/messenger.h>
#include <hurd/as.h>
#include <hurd/stddef.h>
#include <assert.h>
#include <backtrace.h>

#ifdef RM_INTERN
#include <md5.h>

#include "../viengoos/cap.h"
#include "../viengoos/activity.h"
#endif

static void
print_nr (int width, int64_t nr, bool hex)
{
  int base = 10;
  if (hex)
    base = 16;

  int64_t v = nr;
  int w = 0;
  if (v < 0)
    {
      v = -v;
      w ++;
    }
  do
    {
      w ++;
      v /= base;
    }
  while (v > 0);

  int i;
  for (i = w; i < width; i ++)
    S_PUTCHAR (' ');

  if (hex)
    S_PRINTF ("0x%llx", nr);
  else
    S_PRINTF ("%lld", nr);
}

static void
do_walk (vg_activity_t activity, int index,
	 struct vg_cap *root, vg_addr_t addr,
	 int indent, bool descend, const char *output_prefix)
{
  int i;

  struct vg_cap vg_cap = as_cap_lookup_rel (activity, root, addr, -1, NULL);
  if (vg_cap.type == vg_cap_void)
    return;

  if (! vg_cap_to_object (activity, &vg_cap))
    /* Cap is there but the object has been deallocated.  */
    return;

  if (output_prefix && *output_prefix)
    S_PRINTF ("%s: ", output_prefix);
  for (i = 0; i < indent; i ++)
    S_PRINTF (".");

  S_PRINTF ("[ ");
  if (index != -1)
    print_nr (3, index, false);
  else
    S_PRINTF ("root");
  S_PRINTF (" ] ");

  print_nr (12, vg_addr_prefix (addr), true);
  S_PRINTF ("/%d ", vg_addr_depth (addr));
  if (VG_CAP_GUARD_BITS (&vg_cap))
    S_PRINTF ("| 0x%llx/%d ", VG_CAP_GUARD (&vg_cap), VG_CAP_GUARD_BITS (&vg_cap));
  if (VG_CAP_SUBPAGES (&vg_cap) != 1)
    S_PRINTF ("(%d/%d) ", VG_CAP_SUBPAGE (&vg_cap), VG_CAP_SUBPAGES (&vg_cap));

  if (VG_CAP_GUARD_BITS (&vg_cap)
      && VG_ADDR_BITS - vg_addr_depth (addr) >= VG_CAP_GUARD_BITS (&vg_cap))
    S_PRINTF ("=> 0x%llx/%d ",
	    vg_addr_prefix (vg_addr_extend (addr,
				      VG_CAP_GUARD (&vg_cap),
				      VG_CAP_GUARD_BITS (&vg_cap))),
	    vg_addr_depth (addr) + VG_CAP_GUARD_BITS (&vg_cap));

#ifdef RM_INTERN
  S_PRINTF ("@" VG_OID_FMT " ", VG_OID_PRINTF (vg_cap.oid));
#endif
  S_PRINTF ("%s", vg_cap_type_string (vg_cap.type));

#ifdef RM_INTERN
  if (vg_cap.type == vg_cap_page || vg_cap.type == vg_cap_rpage)
    {
      struct vg_object *object = cap_to_object_soft (root_activity, &vg_cap);
      if (object)
	{
	  struct md5_ctx ctx;
	  unsigned char result[16];

	  md5_init_ctx (&ctx);
	  md5_process_bytes (object, PAGESIZE, &ctx);
	  md5_finish_ctx (&ctx, result);

	  S_PRINTF (" ");
	  int i;
	  for (i = 0; i < 16; i ++)
	    printf ("%x%x", result[i] & 0xf, result[i] >> 4);

	  for (i = 0; i < PAGESIZE / sizeof (int); i ++)
	    if (((int *) (object))[i] != 0)
	      break;
	  if (i == PAGESIZE / sizeof (int))
	    S_PRINTF (" zero page");
	}
    }
#endif

  if (! descend)
    S_PRINTF ("...");

  S_PRINTF ("\n");

  if (! descend)
    return;

  if (vg_addr_depth (addr) + VG_CAP_GUARD_BITS (&vg_cap) > VG_ADDR_BITS)
    return;

  addr = vg_addr_extend (addr, VG_CAP_GUARD (&vg_cap), VG_CAP_GUARD_BITS (&vg_cap));

  switch (vg_cap.type)
    {
    case vg_cap_cappage:
    case vg_cap_rcappage:
      if (vg_addr_depth (addr) + VG_CAP_SUBPAGE_SIZE_LOG2 (&vg_cap) > VG_ADDR_BITS)
	return;

      for (i = 0; i < VG_CAP_SUBPAGE_SIZE (&vg_cap); i ++)
	do_walk (activity, i, root,
		 vg_addr_extend (addr, i, VG_CAP_SUBPAGE_SIZE_LOG2 (&vg_cap)),
		 indent + 1, true, output_prefix);

      return;

    case vg_cap_folio:
      if (vg_addr_depth (addr) + VG_FOLIO_OBJECTS_LOG2 > VG_ADDR_BITS)
	return;

      for (i = 0; i < VG_FOLIO_OBJECTS; i ++)
	do_walk (activity, i, root,
		 vg_addr_extend (addr, i, VG_FOLIO_OBJECTS_LOG2),
		 indent + 1, false, output_prefix);

      return;

    case vg_cap_thread:
      if (vg_addr_depth (addr) + VG_THREAD_SLOTS_LOG2 > VG_ADDR_BITS)
	return;

      for (i = 0; i < VG_THREAD_SLOTS; i ++)
	do_walk (activity, i, root,
		 vg_addr_extend (addr, i, VG_THREAD_SLOTS_LOG2),
		 indent + 1, true, output_prefix);

      return;

    case vg_cap_messenger:
      /* rmessenger's don't expose their capability slots.  */
      if (vg_addr_depth (addr) + VG_MESSENGER_SLOTS_LOG2 > VG_ADDR_BITS)
	return;

      for (i = 0; i < VG_MESSENGER_SLOTS; i ++)
	do_walk (activity, i, root,
		 vg_addr_extend (addr, i, VG_MESSENGER_SLOTS_LOG2),
		 indent + 1, true, output_prefix);

      return;

    default:
      return;
    }
}

/* AS_LOCK must not be held.  */
void
as_dump_from (vg_activity_t activity, struct vg_cap *root, const char *prefix)
{
  debug (0, "Dumping address space.");
  backtrace_print ();

  if (0)
    do_walk (activity, -1, root, VG_ADDR (0, 0), 0, true, prefix);
}