debuggers.hg

view tools/xenstore/xenstored_solaris.c @ 16986:a6c037d8cba3

Add DTrace support to xenstored

Add USDT probes for significant xenstore operations to allow dynamic
tracing.

Signed-off-by: John Levon <john.levon@sun.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jan 31 09:13:27 2008 +0000 (2008-01-31)
parents 398ab18ffed1
children cb1f41538756
line source
1 /******************************************************************************
2 *
3 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
4 * Use is subject to license terms.
5 *
6 * Copyright (C) 2005 Rusty Russell IBM Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2 of the
11 * License.
12 */
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <sys/mman.h>
18 #include <strings.h>
19 #include <ucred.h>
20 #include <stdio.h>
22 #include <xen/sys/xenbus.h>
24 #include "talloc.h"
25 #include "xenstored_core.h"
26 #include "xenstored_probes.h"
28 evtchn_port_t xenbus_evtchn(void)
29 {
30 int fd;
31 evtchn_port_t port;
33 fd = open("/dev/xen/xenbus", O_RDONLY);
34 if (fd == -1)
35 return -1;
37 port = ioctl(fd, IOCTL_XENBUS_XENSTORE_EVTCHN);
39 close(fd);
40 return port;
41 }
43 void *xenbus_map(void)
44 {
45 int fd;
46 void *addr;
48 fd = open("/dev/xen/xenbus", O_RDWR);
49 if (fd == -1)
50 return NULL;
52 addr = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
53 MAP_SHARED, fd, 0);
55 if (addr == MAP_FAILED)
56 addr = NULL;
58 close(fd);
60 return addr;
61 }
63 void xenbus_notify_running(void)
64 {
65 int fd;
67 fd = open("/dev/xen/xenbus", O_RDONLY);
69 (void) ioctl(fd, IOCTL_XENBUS_NOTIFY_UP);
71 close(fd);
72 }
74 static pid_t cred(const struct connection *conn)
75 {
76 ucred_t *ucred = NULL;
77 pid_t pid;
79 if (conn->domain)
80 return (0);
82 if (getpeerucred(conn->fd, &ucred) == -1)
83 return (0);
85 pid = ucred_getpid(ucred);
87 ucred_free(ucred);
88 return (pid);
89 }
91 /*
92 * The strings are often a number of nil-separated strings. We'll just
93 * replace the separators with spaces - not quite right, but good
94 * enough.
95 */
96 static char *
97 mangle(const struct connection *conn, const struct buffered_data *in)
98 {
99 char *str;
100 int i;
102 if (in->hdr.msg.len == 0)
103 return (talloc_strdup(conn, ""));
105 if ((str = talloc_zero_size(conn, in->hdr.msg.len + 1)) == NULL)
106 return (NULL);
108 memcpy(str, in->buffer, in->hdr.msg.len);
110 /*
111 * The protocol is absurdly inconsistent in whether the length
112 * includes the terminating nil or not; replace all nils that
113 * aren't the last one.
114 */
115 for (i = 0; i < (in->hdr.msg.len - 1); i++) {
116 if (str[i] == '\0')
117 str[i] = ' ';
118 }
120 return (str);
121 }
123 void
124 dtrace_io(const struct connection *conn, const struct buffered_data *in,
125 int io_out)
126 {
127 if (!io_out) {
128 if (XENSTORE_MSG_ENABLED()) {
129 char *mangled = mangle(conn, in);
130 XENSTORE_MSG(in->hdr.msg.tx_id, conn->id, cred(conn),
131 in->hdr.msg.type, mangled);
132 }
134 goto out;
135 }
137 switch (in->hdr.msg.type) {
138 case XS_ERROR:
139 if (XENSTORE_ERROR_ENABLED()) {
140 char *mangled = mangle(conn, in);
141 XENSTORE_ERROR(in->hdr.msg.tx_id, conn->id,
142 cred(conn), mangled);
143 }
144 break;
146 case XS_WATCH_EVENT:
147 if (XENSTORE_WATCH_EVENT_ENABLED()) {
148 char *mangled = mangle(conn, in);
149 XENSTORE_WATCH_EVENT(conn->id, cred(conn), mangled);
150 }
151 break;
153 default:
154 if (XENSTORE_REPLY_ENABLED()) {
155 char *mangled = mangle(conn, in);
156 XENSTORE_REPLY(in->hdr.msg.tx_id, conn->id, cred(conn),
157 in->hdr.msg.type, mangled);
158 }
159 break;
160 }
162 out:
163 /*
164 * 6589130 dtrace -G fails for certain tail-calls on x86
165 */
166 asm("nop");
167 }