summaryrefslogtreecommitdiff
path: root/libports/introspection.c
blob: 05aee92f30e235e66441d666d11f86aaec65d13f (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
/* Hurd server introspection.

   Copyright (C) 2014 Free Software Foundation, Inc.

   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 2, 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 the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.  */

#include <error.h>
#include <hurd/introspection.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>

#include "ports.h"
#include "hurd_port_U.h"

/* We service introspection requests on this port.  */
static mach_port_t introspection_port;

/* We use a separate thread to service the introspection requests.  It
   is a straight forward Mach server for the hurd_port protocol.  */
static void *
service_introspection_requests (void *arg)
{
  error_t err;

  err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE,
			    &introspection_port);
  if (err)
    {
      error (0, err, "mach_port_allocate");
      return NULL;
    }

  err = mach_port_insert_right (mach_task_self (),
				introspection_port, introspection_port,
				MACH_MSG_TYPE_MAKE_SEND);
  if (err)
    {
      error (0, err, "mach_port_insert_right");
      return NULL;
    }

  err = introspection_set_port (mach_task_self (), introspection_port);
  if (err)
    {
      error (0, err, "introspection_set_port");
      return NULL;
    }

  /* XXX mig should emit this declaration.  */
  boolean_t ports_hurd_port_server (mach_msg_header_t *InHeadP,
				    mach_msg_header_t *OutHeadP);

  while (1)
    mach_msg_server (ports_hurd_port_server, 0, introspection_port);

  /* Not reached.  */
  return NULL;
}

/* Start the introspection server before main is called.  */
static void __attribute__ ((constructor))
init (void)
{
  error_t err;

  pthread_t thread;
  pthread_attr_t attr;
#define STACK_SIZE (64 * 1024)
  pthread_attr_init (&attr);
  pthread_attr_setstacksize (&attr, STACK_SIZE);
#undef STACK_SIZE

  err = pthread_create (&thread, &attr,
			service_introspection_requests, NULL);
  if (err)
    error (1, err, "pthread_create");
  pthread_detach (thread);
}

/* Return the number of hard and weak references of the object
   directly associated with the receive right NAME.

   Return EINVAL if NAME does not denote a receive right managed by
   the port-to-object mapper, or if the concept of reference counting
   simply does not apply.  */
error_t
ports_S_hurd_port_get_refcounts (mach_port_t port,
				 mach_port_t name,
				 natural_t *hard,
				 natural_t *weak)
{
  struct references result;
  struct port_info *pi;

  if (port != introspection_port)
    return EOPNOTSUPP;

  pi = ports_lookup_port (0, name, 0);
  if (pi == NULL)
    return EINVAL;

  refcounts_references (&pi->refcounts, &result);

  *hard = result.hard - 1;
  *weak = result.weak;
  ports_port_deref (pi);
  return 0;
}

static error_t
default_debug_info (const void *port, char *buffer, size_t size)
{
  const struct port_info *pi = port;
  snprintf (buffer, size,
	    "bucket: %s, class: %s",
	    pi->bucket->label, pi->class->label);
  return 0;
}

/* Return a compact, human-readable description of the object related
   with the receive right NAME.

   This description is meant for debugging purposes and should include
   relevant internal state.  If possible, it should include
   information that is meaningful in other contexts (like a file name,
   or the inode number).

   Return EINVAL if NAME does not denote a receive right managed by
   the port-to-object mapper.  */
error_t
ports_S_hurd_port_debug_info (mach_port_t port,
			      mach_port_t name,
			      char *info)
{
  error_t err;
  struct port_info *pi;

  if (port != introspection_port)
    return EOPNOTSUPP;

  pi = ports_lookup_port (0, name, 0);
  if (pi == NULL)
    return EINVAL;

  if (pi->class->debug_info)
    err = pi->class->debug_info (pi, info, 1024 /* XXX */);
  else
    err = default_debug_info (pi, info, 1024 /* XXX */);
  info[1023] = 0;

  ports_port_deref (pi);
  return err;
}

error_t
ports_S_hurd_port_trace_class_rpcs (mach_port_t port,
				    mach_port_t name,
				    mach_port_t trace_port)
{
  struct port_info *pi;

  if (port != introspection_port)
    return EOPNOTSUPP;

  pi = ports_lookup_port (0, name, 0);
  if (pi == NULL)
    return EINVAL;

  if (MACH_PORT_VALID (pi->class->trace_port))
    mach_port_deallocate (mach_task_self (), pi->class->trace_port);

  pi->class->trace_port = trace_port;

  return 0;
}