Xserver dtrace provider 0.2 for Xorg server 1.2

/* Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, and/or sell copies of the Software, and to permit persons
 * to whom the Software is furnished to do so, provided that the above
 * copyright notice(s) and this permission notice appear in all copies of
 * the Software and that both the above copyright notice(s) and this
 * permission notice appear in supporting documentation.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * Except as contained in this notice, the name of a copyright holder
 * shall not be used in advertising or otherwise to promote the sale, use
 * or other dealings in this Software without prior written authorization
 * of the copyright holder.
 */

This patch is built against the Xorg server 1.2 RC1 (aka X11R7.2 RC1).
Download the base source from 
http://xorg.freedesktop.org/releases/individual/xserver/xorg-server-1.1.99.901.tar.bz2
To build, do:
	bzcat xorg-server-1.1.99.901.tar.bz2 | tar -xf -
	cd xorg-server-1.1.99.901
	gpatch -p0 < 7.2-dtrace.patch
	./configure ; make 

Once you're done, you should be able to drop in hw/xfree86/Xorg
as a replacement for /usr/X11/bin/Xorg on a Solaris 10 or Nevada/Express
system.

--- dix/Xserver.d	1969-12-31 16:00:00.000000000 -0800
+++ dix/Xserver.d	2005-07-04 11:09:17.267106000 -0700
@@ -0,0 +1,59 @@
+/* Copyright 2005-2006 Sun Microsystems, Inc.  All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons
+ * to whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+ * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
+ * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
+ * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ * 
+ * Except as contained in this notice, the name of a copyright holder
+ * shall not be used in advertising or otherwise to promote the sale, use
+ * or other dealings in this Software without prior written authorization
+ * of the copyright holder.
+ */
+
+/*
+ * Xserver dtrace provider definition
+ */
+
+#include <sys/types.h>
+
+provider Xserver {
+	/* reqType, data, length, client id, request buffer */
+	probe request__start(string, uint8_t, uint16_t, int, void *);
+	/* reqType, data, sequence, client id, result */
+	probe request__done(string, uint8_t, uint32_t, int, int);
+	/* client id, client fd */
+	probe client__connect(int, int);
+	/* client id, client address, client pid, client zone id */
+	probe client__auth(int, string, pid_t, zoneid_t);
+	/* client id */
+	probe client__disconnect(int);
+	/* resource id, resource type, value, resource type name */
+	probe resource__alloc(uint32_t, uint32_t, void *, string);
+	/* resource id, resource type, value, resource type name */
+	probe resource__free(uint32_t, uint32_t, void *, string);
+	/* client id, event type, event* */
+	probe send__event(int, uint8_t, void *);
+};
+
+#pragma D attributes Unstable/Unstable/Common provider Xserver provider
+#pragma D attributes Private/Private/Unknown  provider Xserver module
+#pragma D attributes Private/Private/Unknown  provider Xserver function
+#pragma D attributes Unstable/Unstable/Common provider Xserver name
+#pragma D attributes Unstable/Unstable/Common provider Xserver args
+

diff -urp -x '*~' configure.ac configure.ac
--- configure.ac	2006-10-13 15:53:18.000000000 -0700
+++ configure.ac	2006-10-30 14:16:57.837404000 -0800
@@ -64,6 +64,15 @@ AC_PROG_LEX
 AC_PROG_YACC
 XORG_PROG_RAWCPP
 
+dnl Check for dtrace program (needed to build Xserver dtrace probes)
+AC_PATH_PROG(DTRACE, [dtrace], [not_found], [$PATH:/usr/sbin])
+if test "x$DTRACE" != "xnot_found" ; then
+  AC_DEFINE(XSERVER_DTRACE, 1, 
+      [Define to 1 if the DTrace Xserver provider probes should be built in.])
+fi
+AM_CONDITIONAL(XSERVER_DTRACE, [test "x$DTRACE" != "xnot_found"])
+AC_DEFINE_DIR(XERRORDB_PATH, libdir/XErrorDB, [Path to XErrorDB file])
+
 AC_HEADER_DIRENT
 AC_HEADER_STDC
 AC_CHECK_HEADERS([fcntl.h stdlib.h string.h unistd.h])
@@ -852,8 +861,8 @@ AC_DEFINE(XCMISC, 1, [Support XCMisc ext
 AC_DEFINE(BIGREQS, 1, [Support BigRequests extension])
 AC_DEFINE(PIXPRIV, 1, [Support pixmap privates])
 
-DIX_LIB='$(top_builddir)/dix/libdix.la'
-OS_LIB='$(top_builddir)/os/libos.la'
+DIX_LIB='$(top_builddir)/dix/dix.O'
+OS_LIB='$(top_builddir)/os/os.O'
 CWRAP_LIB='$(top_builddir)/os/libcwrapper.la'
 MI_LIB='$(top_builddir)/mi/libmi.la'
 MINIMI_LIB='$(top_builddir)/mi/libminimi.la'
diff -urp -x '*~' dix/Makefile.am dix/Makefile.am
--- dix/Makefile.am	2006-10-11 15:42:19.000000000 -0700
+++ dix/Makefile.am	2006-10-30 14:16:57.801240000 -0800
@@ -37,4 +37,21 @@ libxpstubs_la_SOURCES =	\
 
 INCLUDES = -I$(top_srcdir)/Xprint
 
-EXTRA_DIST = buildatoms BuiltInAtoms CHANGES
+EXTRA_DIST = buildatoms BuiltInAtoms CHANGES Xserver.d
+
+if XSERVER_DTRACE
+# Generate dtrace header file for C sources to include
+BUILT_SOURCES = Xserver-dtrace.h
+
+Xserver-dtrace.h: $(srcdir)/Xserver.d
+	$(DTRACE) -C -h -o $@ -s $(srcdir)/Xserver.d
+
+# Generate dtrace object code for probes in libdix
+dtrace-os.o: $(top_srcdir)/dix/Xserver.d $(am_libdix_la_OBJECTS)
+	$(DTRACE) -G -C -o $@ -s $(top_srcdir)/dix/Xserver.d .libs/*.o
+
+noinst_LIBRARIES = dix.O
+
+dix.O: dtrace-os.o $(am_libdix_la_OBJECTS)
+	ld -r -o $@ dtrace-os.o .libs/*.o
+endif
diff -urp -x '*~' dix/dispatch.c dix/dispatch.c
--- dix/dispatch.c	2006-10-11 15:42:19.000000000 -0700
+++ dix/dispatch.c	2006-10-30 14:16:57.798697000 -0800
@@ -74,6 +74,36 @@ Equipment Corporation.
 
 ******************************************************************/
 
+/* XSERVER_DTRACE additions:
+ * Copyright 2005-2006 Sun Microsystems, Inc.  All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons
+ * to whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+ * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
+ * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
+ * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ * 
+ * Except as contained in this notice, the name of a copyright holder
+ * shall not be used in advertising or otherwise to promote the sale, use
+ * or other dealings in this Software without prior written authorization
+ * of the copyright holder.
+ */
+
+
 
 #ifdef HAVE_DIX_CONFIG_H
 #include <dix-config.h>
@@ -118,6 +148,17 @@ int ProcInitialConnection();
 #include <X11/extensions/XKBsrv.h>
 #endif
 
+#ifdef XSERVER_DTRACE
+#include <sys/types.h>
+typedef const char *string;
+#include "Xserver-dtrace.h"
+
+char *RequestNames[256];
+static void LoadRequestNames(void);
+static void FreeRequestNames(void);
+#define GetRequestName(i) (RequestNames[i])
+#endif
+
 #define mskcnt ((MAXCLIENTS + 31) / 32)
 #define BITMASK(i) (1U << ((i) & 31))
 #define MASKIDX(i) ((i) >> 5)
@@ -372,6 +413,10 @@ Dispatch(void)
     if (!clientReady)
 	return;
 
+#ifdef XSERVER_DTRACE
+    LoadRequestNames();
+#endif
+
     while (!dispatchException)
     {
         if (*icheck[0] != *icheck[1])
@@ -448,6 +493,11 @@ Dispatch(void)
 		client->requestLog[client->requestLogIndex] = MAJOROP;
 		client->requestLogIndex++;
 #endif
+#ifdef XSERVER_DTRACE
+		XSERVER_REQUEST_START(GetRequestName(MAJOROP), MAJOROP,
+			      ((xReq *)client->requestBuffer)->length,
+			      client->index, client->requestBuffer);
+#endif
 		if (result > (maxBigRequestSize << 2))
 		    result = BadLength;
 		else
@@ -460,7 +510,11 @@ Dispatch(void)
 #else
     		    result = (* client->requestVector[MAJOROP])(client);
 #endif /* XACE */
-	    
+#ifdef XSERVER_DTRACE
+		XSERVER_REQUEST_DONE(GetRequestName(MAJOROP), MAJOROP,
+			      client->sequence, client->index, result);
+#endif
+
 		if (result != Success) 
 		{
 		    if (client->noClientException != Success)
@@ -491,6 +545,9 @@ Dispatch(void)
     KillAllClients();
     DEALLOCATE_LOCAL(clientReady);
     dispatchException &= ~DE_RESET;
+#ifdef XSERVER_DTRACE
+    FreeRequestNames();
+#endif
 }
 
 #undef MAJOROP
@@ -3565,6 +3622,9 @@ CloseDownClient(register ClientPtr clien
 	    CallCallbacks((&ClientStateCallback), (pointer)&clientinfo);
 	} 	    
 	FreeClientResources(client);
+#ifdef XSERVER_DTRACE
+	XSERVER_CLIENT_DISCONNECT(client->index);
+#endif	
 	if (client->index < nextFreeClientID)
 	    nextFreeClientID = client->index;
 	clients[client->index] = NullClient;
@@ -3991,3 +4051,60 @@ MarkClientException(ClientPtr client)
 {
     client->noClientException = -1;
 }
+
+#ifdef XSERVER_DTRACE
+#include <ctype.h>
+
+/* Load table of request names for dtrace probes */
+static void LoadRequestNames(void)
+{
+    int i;
+    FILE *xedb;
+    extern void LoadExtensionNames(char **RequestNames);
+
+    bzero(RequestNames, 256 * sizeof(char *));
+
+    xedb = fopen(XERRORDB_PATH, "r");
+    if (xedb != NULL) {
+	char buf[256];
+	while (fgets(buf, sizeof(buf), xedb)) {
+	    if ((strncmp("XRequest.", buf, 9) == 0) && (isdigit(buf[9]))) {
+		char *name;
+		i = strtol(buf + 9, &name, 10);
+		if (RequestNames[i] == 0) {
+		    char *end = strchr(name, '\n');
+		    if (end) { *end = '\0'; }
+		    RequestNames[i] = strdup(name + 1);
+		}
+	    }
+	}
+	fclose(xedb);
+    }
+
+    LoadExtensionNames(RequestNames);
+
+    for (i = 0; i < 256; i++) {
+	if (RequestNames[i] == 0) {
+#define RN_SIZE 12 /* "Request#' + up to 3 digits + \0 */
+	    RequestNames[i] = xalloc(RN_SIZE);
+	    if (RequestNames[i]) {
+		snprintf(RequestNames[i], RN_SIZE, "Request#%d", i);
+	    }
+	}
+	/* fprintf(stderr, "%d: %s\n", i, RequestNames[i]); */
+    }
+}
+
+static void FreeRequestNames(void)
+{
+    int i;
+
+    for (i = 0; i < 256; i++) {
+	if (RequestNames[i] != 0) {
+	    free(RequestNames[i]);
+	    RequestNames[i] = 0;
+	}
+    }
+}
+
+#endif
diff -urp -x '*~' dix/events.c dix/events.c
--- dix/events.c	2006-10-11 15:42:19.000000000 -0700
+++ dix/events.c	2006-10-30 14:16:57.839461000 -0800
@@ -139,6 +139,12 @@ extern Bool XkbFilterEvents(ClientPtr, i
 #include "xace.h"
 #endif
 
+#ifdef XSERVER_DTRACE
+#include <sys/types.h>
+typedef const char *string;
+#include "Xserver-dtrace.h"
+#endif
+
 #ifdef XEVIE
 extern WindowPtr *WindowTable;
 extern int       xevieFlag;
@@ -4580,6 +4586,14 @@ WriteEventsToClient(ClientPtr pClient, i
 	eventinfo.count = count;
 	CallCallbacks(&EventCallback, (pointer)&eventinfo);
     }
+#ifdef XSERVER_DTRACE
+    if (XSERVER_SEND_EVENT_ENABLED()) {
+	for (i = 0; i < count; i++)
+	{
+	    XSERVER_SEND_EVENT(pClient->index, events[i].u.u.type, &events[i]);
+	}
+    }
+#endif	
     if(pClient->swapped)
     {
 	for(i = 0; i < count; i++)
diff -urp -x '*~' dix/extension.c dix/extension.c
--- dix/extension.c	2006-10-11 15:42:19.000000000 -0700
+++ dix/extension.c	2006-10-30 14:16:57.799190000 -0800
@@ -482,3 +482,17 @@ RegisterScreenProc(char *name, ScreenPtr
     }
     return TRUE;
 }
+
+#ifdef XSERVER_DTRACE
+void LoadExtensionNames(char **RequestNames) {
+    int i;
+
+    for (i=0; i<NumExtensions; i++) {
+	int r = extensions[i]->base;
+
+	if (RequestNames[r] == NULL) {
+	    RequestNames[r] = strdup(extensions[i]->name);
+	}
+    }
+}
+#endif
diff -urp -x '*~' dix/resource.c dix/resource.c
--- dix/resource.c	2006-10-11 15:42:19.000000000 -0700
+++ dix/resource.c	2006-10-30 14:16:57.799909000 -0800
@@ -72,6 +72,34 @@ dealings in this Software without prior 
 Equipment Corporation.
 
 ******************************************************************/
+/* XSERVER_DTRACE additions:
+ * Copyright 2005-2006 Sun Microsystems, Inc.  All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons
+ * to whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+ * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
+ * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
+ * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ * 
+ * Except as contained in this notice, the name of a copyright holder
+ * shall not be used in advertising or otherwise to promote the sale, use
+ * or other dealings in this Software without prior written authorization
+ * of the copyright holder.
+ */
 
 /* $TOG: resource.c /main/41 1998/02/09 14:20:31 kaleb $ */
 
@@ -125,6 +153,14 @@ Equipment Corporation.
 #endif
 #include <assert.h>
 
+#ifdef XSERVER_DTRACE
+#include <sys/types.h>
+typedef const char *string;
+#include "Xserver-dtrace.h"
+
+#define TypeNameString(t) NameForAtom(ResourceNames[t & TypeMask])
+#endif
+
 static void RebuildTable(
     int /*client*/
 );
@@ -429,6 +465,9 @@ AddResource(XID id, RESTYPE type, pointe
     register ClientResourceRec *rrec;
     register ResourcePtr res, *head;
     	
+#ifdef XSERVER_DTRACE
+    XSERVER_RESOURCE_ALLOC(id, type, value, TypeNameString(type));
+#endif
     client = CLIENT_ID(id);
     rrec = &clientTable[client];
     if (!rrec->buckets)
@@ -528,6 +567,11 @@ FreeResource(XID id, RESTYPE skipDeleteF
 	    if (res->id == id)
 	    {
 		RESTYPE rtype = res->type;
+
+#ifdef XSERVER_DTRACE
+		XSERVER_RESOURCE_FREE(res->id, res->type,
+			      res->value, TypeNameString(res->type));
+#endif		    
 		*prev = res->next;
 		elements = --*eltptr;
 		if (rtype & RC_CACHED)
@@ -569,6 +613,10 @@ FreeResourceByType(XID id, RESTYPE type,
 	{
 	    if (res->id == id && res->type == type)
 	    {
+#ifdef XSERVER_DTRACE
+		XSERVER_RESOURCE_FREE(res->id, res->type,
+			      res->value, TypeNameString(res->type));
+#endif		    		    
 		*prev = res->next;
 		if (type & RC_CACHED)
 		    FlushClientCaches(res->id);
@@ -731,6 +779,10 @@ FreeClientNeverRetainResources(ClientPtr
 	    RESTYPE rtype = this->type;
 	    if (rtype & RC_NEVERRETAIN)
 	    {
+#ifdef XSERVER_DTRACE
+		XSERVER_RESOURCE_FREE(this->id, this->type,
+			      this->value, TypeNameString(this->type));
+#endif		    
 		*prev = this->next;
 		if (rtype & RC_CACHED)
 		    FlushClientCaches(this->id);
@@ -777,6 +829,10 @@ FreeClientResources(ClientPtr client)
         for (this = *head; this; this = *head)
 	{
 	    RESTYPE rtype = this->type;
+#ifdef XSERVER_DTRACE
+	    XSERVER_RESOURCE_FREE(this->id, this->type,
+			  this->value, TypeNameString(this->type));
+#endif		    
 	    *head = this->next;
 	    if (rtype & RC_CACHED)
 		FlushClientCaches(this->id);
diff -urp -x '*~' include/dix-config.h.in include/dix-config.h.in
--- include/dix-config.h.in	2006-10-11 15:42:19.000000000 -0700
+++ include/dix-config.h.in	2006-10-30 14:16:57.801020000 -0800
@@ -442,4 +442,10 @@
 /* Define to 1 if modules should avoid the libcwrapper */
 #undef NO_LIBCWRAPPER
 
+/* Define to 1 if the DTrace Xserver provider probes should be built in */
+#undef XSERVER_DTRACE
+
+/* Path to XErrorDB file */
+#undef XERRORDB_PATH
+
 #endif /* _DIX_CONFIG_H_ */
diff -urp -x '*~' os/Makefile.am os/Makefile.am
--- os/Makefile.am	2006-10-11 15:42:19.000000000 -0700
+++ os/Makefile.am	2006-10-30 14:16:57.836238000 -0800
@@ -50,3 +50,15 @@ libcwrapper_la_CFLAGS = \
 
 EXTRA_DIST = $(K5AUTH_SOURCES) $(SECURERPC_SOURCES) $(INTERNALMALLOC_SOURCES) \
      $(XCSECURITY_SOURCES) $(XDMCP_SOURCES) $(LBX_SOURCES) $(STRLCAT_SOURCES)
+
+if XSERVER_DTRACE
+# Generate dtrace object code for probes in libos
+dtrace-os.o: $(top_srcdir)/dix/Xserver.d $(am_libos_la_OBJECTS)
+	$(DTRACE) -G -C -o $@ -s $(top_srcdir)/dix/Xserver.d .libs/*.o
+
+noinst_LIBRARIES = os.O
+
+os.O: dtrace-os.o $(am_libos_la_OBJECTS)
+	ld -r -o $@ dtrace-os.o .libs/*.o
+
+endif
diff -urp -x '*~' os/connection.c os/connection.c
--- os/connection.c	2006-10-11 15:42:19.000000000 -0700
+++ os/connection.c	2006-10-30 14:16:57.800659000 -0800
@@ -165,6 +165,13 @@ extern __const__ int _nfiles;
 #include <netdnet/dn.h>
 #endif /* DNETCONN */
 
+#ifdef XSERVER_DTRACE
+# include <sys/types.h>
+typedef const char *string;
+# include "../dix/Xserver-dtrace.h"
+# include <ucred.h>
+#endif
+
 int lastfdesc;			/* maximum file descriptor */
 
 fd_set WellKnownConnections;	/* Listener mask */
@@ -585,6 +592,10 @@ AuthAudit (ClientPtr client, Bool letin,
 	default:
 	    strcpy(out, "unknown address");
 	}
+
+#ifdef XSERVER_DTRACE
+  if (auditTrailLevel > 1) {
+#endif
     
     if (proto_n)
 	AuditF("client %d %s from %s\n  Auth name: %.*s ID: %d\n", 
@@ -593,6 +604,24 @@ AuthAudit (ClientPtr client, Bool letin,
     else 
 	AuditF("client %d %s from %s\n", 
 	       client->index, letin ? "connected" : "rejected", addr);
+#ifdef XSERVER_DTRACE
+  }
+  if (XSERVER_CLIENT_AUTH_ENABLED())
+  {
+      ucred_t *peercred = NULL;
+      pid_t pid = -1;
+      zoneid_t zid = -1;
+      
+      if (getpeerucred(((OsCommPtr)client->osPrivate)->fd, &peercred) >= 0) {
+	  pid = ucred_getpid(peercred);
+	  zid = ucred_getzoneid(peercred);
+	  ucred_free(peercred);
+      }
+    
+      XSERVER_CLIENT_AUTH(client->index, addr, pid, zid);
+  }
+#endif	
+
 }
 
 XID
@@ -659,7 +688,9 @@ ClientAuthorized(ClientPtr client, 
 	    else
 	    {
 		auth_id = (XID) 0;
+#ifndef XSERVER_DTRACE
 		if (auditTrailLevel > 1)
+#endif
 		    AuthAudit(client, TRUE,
 			(struct sockaddr *) from, fromlen,
 			proto_n, auth_proto, auth_id);
@@ -675,7 +706,11 @@ ClientAuthorized(ClientPtr client, 
 		return "Client is not authorized to connect to Server";
 	}
     }
+#ifdef XSERVER_DTRACE
+    else
+#else
     else if (auditTrailLevel > 1)
+#endif
     {
 	if (_XSERVTransGetPeerAddr (trans_conn,
 	    &family, &fromlen, &from) != -1)
@@ -753,6 +788,9 @@ AllocNewConnection (XtransConnInfo trans
     ErrorF("AllocNewConnection: client index = %d, socket fd = %d\n",
 	   client->index, fd);
 #endif
+#ifdef XSERVER_DTRACE
+    XSERVER_CLIENT_CONNECT(client->index, fd);
+#endif	
 
     return client;
 }
