xref: /aosp_15_r20/external/wayland/patches/0001-connection-Simplify-wl_closure_print.diff (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd PiqueFrom 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2*84e872a0SLloyd PiqueFrom: Lloyd Pique <[email protected]>
3*84e872a0SLloyd PiqueDate: Thu, 10 Mar 2022 14:56:02 -0800
4*84e872a0SLloyd PiqueSubject: [PATCH 1/6] connection: Simplify wl_closure_print
5*84e872a0SLloyd Pique
6*84e872a0SLloyd PiqueClient message observers 1/6
7*84e872a0SLloyd Pique
8*84e872a0SLloyd PiqueCleans up wl_closure_print(), and adds a client-private closure_log()
9*84e872a0SLloyd Piqueintermediate function.
10*84e872a0SLloyd Pique
11*84e872a0SLloyd PiqueThis patch simplifies wl_closure_print() slightly by moving some client-only
12*84e872a0SLloyd Piquedetails to a new closure_log() intermediate function. This new function will
13*84e872a0SLloyd Piquealso handle delivering messages to the new listener callback in a subsequent
14*84e872a0SLloyd Piquepatch.
15*84e872a0SLloyd Pique
16*84e872a0SLloyd Piqueclosure_log() internally handles the check for logging being enabled,
17*84e872a0SLloyd Piquesimplifying its callers, and returns early if logging is not enabled. This check
18*84e872a0SLloyd Piquebecomes a bit more complex when there can be listeners.
19*84e872a0SLloyd Pique
20*84e872a0SLloyd Piqueclosure_log() also handles the work same transformation performed by
21*84e872a0SLloyd Piqueid_from_object(), by making a copy of the args, and applying the transform to
22*84e872a0SLloyd Piquethe copy before passing the arguments to wl_closure_print(). Doing it this way
23*84e872a0SLloyd Piquemeans the same arguments can also be passed to the eventual listeners.
24*84e872a0SLloyd Pique
25*84e872a0SLloyd PiqueThe boolean "discarded" argument for wl_closure_print() has been replaced by a
26*84e872a0SLloyd Pique"discarded_reason" string argument, allowing an arbitrary reason string to be
27*84e872a0SLloyd Piquepassed in. For now only "discarded[]" is printed as an empty reason string is
28*84e872a0SLloyd Piquepassed if the message was discarded, but that will also change.
29*84e872a0SLloyd Pique
30*84e872a0SLloyd PiqueSigned-off-by: Lloyd Pique <[email protected]>
31*84e872a0SLloyd Pique
32*84e872a0SLloyd Piquediff --git a/COPYING b/COPYING
33*84e872a0SLloyd Piqueindex eb25a4e..843b844 100644
34*84e872a0SLloyd Pique--- a/COPYING
35*84e872a0SLloyd Pique+++ b/COPYING
36*84e872a0SLloyd Pique@@ -2,6 +2,7 @@ Copyright © 2008-2012 Kristian Høgsberg
37*84e872a0SLloyd Pique Copyright © 2010-2012 Intel Corporation
38*84e872a0SLloyd Pique Copyright © 2011 Benjamin Franzke
39*84e872a0SLloyd Pique Copyright © 2012 Collabora, Ltd.
40*84e872a0SLloyd Pique+Copyright 2022 Google LLC
41*84e872a0SLloyd Pique
42*84e872a0SLloyd Pique Permission is hereby granted, free of charge, to any person obtaining a
43*84e872a0SLloyd Pique copy of this software and associated documentation files (the "Software"),
44*84e872a0SLloyd Piquediff --git a/src/connection.c b/src/connection.c
45*84e872a0SLloyd Piqueindex ceaeac1..110b614 100644
46*84e872a0SLloyd Pique--- a/src/connection.c
47*84e872a0SLloyd Pique+++ b/src/connection.c
48*84e872a0SLloyd Pique@@ -1264,7 +1264,7 @@ wl_closure_queue(struct wl_closure *closure, struct wl_connection *connection)
49*84e872a0SLloyd Pique
50*84e872a0SLloyd Pique void
51*84e872a0SLloyd Pique wl_closure_print(struct wl_closure *closure, struct wl_object *target,
52*84e872a0SLloyd Pique-		 int send, int discarded, uint32_t (*n_parse)(union wl_argument *arg))
53*84e872a0SLloyd Pique+		 bool send, const char *discarded_reason)
54*84e872a0SLloyd Pique {
55*84e872a0SLloyd Pique 	int i;
56*84e872a0SLloyd Pique 	struct argument_details arg;
57*84e872a0SLloyd Pique@@ -1283,9 +1283,11 @@ wl_closure_print(struct wl_closure *closure, struct wl_object *target,
58*84e872a0SLloyd Pique 	clock_gettime(CLOCK_REALTIME, &tp);
59*84e872a0SLloyd Pique 	time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
60*84e872a0SLloyd Pique
61*84e872a0SLloyd Pique-	fprintf(f, "[%7u.%03u] %s%s%s@%u.%s(",
62*84e872a0SLloyd Pique+	fprintf(f, "[%7u.%03u] %s%s%s%s%s@%u.%s(",
63*84e872a0SLloyd Pique 		time / 1000, time % 1000,
64*84e872a0SLloyd Pique-		discarded ? "discarded " : "",
65*84e872a0SLloyd Pique+		(discarded_reason != NULL) ? "discarded[" : "",
66*84e872a0SLloyd Pique+		(discarded_reason != NULL) ? discarded_reason : "",
67*84e872a0SLloyd Pique+		(discarded_reason != NULL) ? "] " : "",
68*84e872a0SLloyd Pique 		send ? " -> " : "",
69*84e872a0SLloyd Pique 		target->interface->name, target->id,
70*84e872a0SLloyd Pique 		closure->message->name);
71*84e872a0SLloyd Pique@@ -1330,10 +1332,7 @@ wl_closure_print(struct wl_closure *closure, struct wl_object *target,
72*84e872a0SLloyd Pique 				fprintf(f, "nil");
73*84e872a0SLloyd Pique 			break;
74*84e872a0SLloyd Pique 		case 'n':
75*84e872a0SLloyd Pique-			if (n_parse)
76*84e872a0SLloyd Pique-				nval = n_parse(&closure->args[i]);
77*84e872a0SLloyd Pique-			else
78*84e872a0SLloyd Pique-				nval = closure->args[i].n;
79*84e872a0SLloyd Pique+			nval = closure->args[i].n;
80*84e872a0SLloyd Pique
81*84e872a0SLloyd Pique 			fprintf(f, "new id %s@",
82*84e872a0SLloyd Pique 				(closure->message->types[i]) ?
83*84e872a0SLloyd Piquediff --git a/src/wayland-client.c b/src/wayland-client.c
84*84e872a0SLloyd Piqueindex 105f9be..ae47307 100644
85*84e872a0SLloyd Pique--- a/src/wayland-client.c
86*84e872a0SLloyd Pique+++ b/src/wayland-client.c
87*84e872a0SLloyd Pique@@ -115,6 +115,73 @@ struct wl_display {
88*84e872a0SLloyd Pique
89*84e872a0SLloyd Pique static int debug_client = 0;
90*84e872a0SLloyd Pique
91*84e872a0SLloyd Pique+/**
92*84e872a0SLloyd Pique+ * This helper function adjusts the closure arguments before they are logged.
93*84e872a0SLloyd Pique+ * On the client, after the call to create_proxies(), NEW_ID arguments will
94*84e872a0SLloyd Pique+ * point to a wl_proxy accessible via arg.o instead of being an int32
95*84e872a0SLloyd Pique+ * accessible by arg.n, which is what wl_closure_print() attempts to print.
96*84e872a0SLloyd Pique+ * This helper transforms the argument back into an id, so wl_closure_print()
97*84e872a0SLloyd Pique+ * doesn't need to handle that as a special case.
98*84e872a0SLloyd Pique+ *
99*84e872a0SLloyd Pique+ * \param closure  closure to adjust
100*84e872a0SLloyd Pique+ * \param send     if this is closure is for a request
101*84e872a0SLloyd Pique+ *
102*84e872a0SLloyd Pique+ */
103*84e872a0SLloyd Pique+static void
104*84e872a0SLloyd Pique+adjust_closure_args_for_logging(struct wl_closure *closure, bool send)
105*84e872a0SLloyd Pique+{
106*84e872a0SLloyd Pique+	int i;
107*84e872a0SLloyd Pique+	struct argument_details arg;
108*84e872a0SLloyd Pique+	const struct wl_proxy *proxy;
109*84e872a0SLloyd Pique+	const char *signature = closure->message->signature;
110*84e872a0SLloyd Pique+
111*84e872a0SLloyd Pique+	// No adjustment needed for a send.
112*84e872a0SLloyd Pique+	if (send)
113*84e872a0SLloyd Pique+		return;
114*84e872a0SLloyd Pique+
115*84e872a0SLloyd Pique+	for (i = 0; i < closure->count; i++) {
116*84e872a0SLloyd Pique+		signature = get_next_argument(signature, &arg);
117*84e872a0SLloyd Pique+
118*84e872a0SLloyd Pique+		switch (arg.type) {
119*84e872a0SLloyd Pique+		case 'n':
120*84e872a0SLloyd Pique+			proxy = (struct wl_proxy *)closure->args[i].o;
121*84e872a0SLloyd Pique+			closure->args[i].n = proxy ? proxy->object.id : 0;
122*84e872a0SLloyd Pique+			break;
123*84e872a0SLloyd Pique+		}
124*84e872a0SLloyd Pique+	}
125*84e872a0SLloyd Pique+}
126*84e872a0SLloyd Pique+
127*84e872a0SLloyd Pique+/**
128*84e872a0SLloyd Pique+ * This function helps log closures from the client, assuming logging is
129*84e872a0SLloyd Pique+ * enabled.
130*84e872a0SLloyd Pique+ *
131*84e872a0SLloyd Pique+ * \param closure    closure for the message
132*84e872a0SLloyd Pique+ * \param proxy      proxy for the message
133*84e872a0SLloyd Pique+ * \param send       true if this is closure is for a request
134*84e872a0SLloyd Pique+ * \param discarded  true if this is message is being discarded
135*84e872a0SLloyd Pique+ *
136*84e872a0SLloyd Pique+ */
137*84e872a0SLloyd Pique+static void
138*84e872a0SLloyd Pique+closure_log(struct wl_closure *closure, struct wl_proxy *proxy, bool send,
139*84e872a0SLloyd Pique+	    bool discarded)
140*84e872a0SLloyd Pique+{
141*84e872a0SLloyd Pique+	struct wl_closure adjusted_closure = { 0 };
142*84e872a0SLloyd Pique+
143*84e872a0SLloyd Pique+	if (!debug_client)
144*84e872a0SLloyd Pique+		return;
145*84e872a0SLloyd Pique+
146*84e872a0SLloyd Pique+	// Note: The real closure has extra data (referenced by its args
147*84e872a0SLloyd Pique+	// immediately following the structure in memory, but we don't
148*84e872a0SLloyd Pique+	// need to duplicate that.
149*84e872a0SLloyd Pique+	memcpy(&adjusted_closure, closure, sizeof(struct wl_closure));
150*84e872a0SLloyd Pique+
151*84e872a0SLloyd Pique+	// Adjust the closure arguments.
152*84e872a0SLloyd Pique+	adjust_closure_args_for_logging(&adjusted_closure, send);
153*84e872a0SLloyd Pique+
154*84e872a0SLloyd Pique+	wl_closure_print(&adjusted_closure, &proxy->object, send,
155*84e872a0SLloyd Pique+			 discarded ? "" : NULL);
156*84e872a0SLloyd Pique+}
157*84e872a0SLloyd Pique+
158*84e872a0SLloyd Pique /**
159*84e872a0SLloyd Pique  * This helper function wakes up all threads that are
160*84e872a0SLloyd Pique  * waiting for display->reader_cond (i. e. when reading is done,
161*84e872a0SLloyd Pique@@ -885,8 +952,7 @@ wl_proxy_marshal_array_flags(struct wl_proxy *proxy, uint32_t opcode,
162*84e872a0SLloyd Pique 		goto err_unlock;
163*84e872a0SLloyd Pique 	}
164*84e872a0SLloyd Pique
165*84e872a0SLloyd Pique-	if (debug_client)
166*84e872a0SLloyd Pique-		wl_closure_print(closure, &proxy->object, true, false, NULL);
167*84e872a0SLloyd Pique+	closure_log(closure, proxy, true, false);
168*84e872a0SLloyd Pique
169*84e872a0SLloyd Pique 	if (wl_closure_send(closure, proxy->display->connection)) {
170*84e872a0SLloyd Pique 		wl_log("Error sending request: %s\n", strerror(errno));
171*84e872a0SLloyd Pique@@ -1579,19 +1645,6 @@ queue_event(struct wl_display *display, int len)
172*84e872a0SLloyd Pique 	return size;
173*84e872a0SLloyd Pique }
174*84e872a0SLloyd Pique
175*84e872a0SLloyd Pique-static uint32_t
176*84e872a0SLloyd Pique-id_from_object(union wl_argument *arg)
177*84e872a0SLloyd Pique-{
178*84e872a0SLloyd Pique-	struct wl_proxy *proxy;
179*84e872a0SLloyd Pique-
180*84e872a0SLloyd Pique-	if (arg->o) {
181*84e872a0SLloyd Pique-		proxy = (struct wl_proxy *)arg->o;
182*84e872a0SLloyd Pique-		return proxy->object.id;
183*84e872a0SLloyd Pique-	}
184*84e872a0SLloyd Pique-
185*84e872a0SLloyd Pique-	return 0;
186*84e872a0SLloyd Pique-}
187*84e872a0SLloyd Pique-
188*84e872a0SLloyd Pique static void
189*84e872a0SLloyd Pique dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
190*84e872a0SLloyd Pique {
191*84e872a0SLloyd Pique@@ -1610,8 +1663,7 @@ dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
192*84e872a0SLloyd Pique 	proxy = closure->proxy;
193*84e872a0SLloyd Pique 	proxy_destroyed = !!(proxy->flags & WL_PROXY_FLAG_DESTROYED);
194*84e872a0SLloyd Pique 	if (proxy_destroyed) {
195*84e872a0SLloyd Pique-		if (debug_client)
196*84e872a0SLloyd Pique-			wl_closure_print(closure, &proxy->object, false, true, id_from_object);
197*84e872a0SLloyd Pique+		closure_log(closure, proxy, false, true);
198*84e872a0SLloyd Pique 		destroy_queued_closure(closure);
199*84e872a0SLloyd Pique 		return;
200*84e872a0SLloyd Pique 	}
201*84e872a0SLloyd Pique@@ -1619,15 +1671,11 @@ dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
202*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
203*84e872a0SLloyd Pique
204*84e872a0SLloyd Pique 	if (proxy->dispatcher) {
205*84e872a0SLloyd Pique-		if (debug_client)
206*84e872a0SLloyd Pique-			wl_closure_print(closure, &proxy->object, false, false, id_from_object);
207*84e872a0SLloyd Pique-
208*84e872a0SLloyd Pique+		closure_log(closure, proxy, false, false);
209*84e872a0SLloyd Pique 		wl_closure_dispatch(closure, proxy->dispatcher,
210*84e872a0SLloyd Pique 				    &proxy->object, opcode);
211*84e872a0SLloyd Pique 	} else if (proxy->object.implementation) {
212*84e872a0SLloyd Pique-		if (debug_client)
213*84e872a0SLloyd Pique-			wl_closure_print(closure, &proxy->object, false, false, id_from_object);
214*84e872a0SLloyd Pique-
215*84e872a0SLloyd Pique+		closure_log(closure, proxy, false, false);
216*84e872a0SLloyd Pique 		wl_closure_invoke(closure, WL_CLOSURE_INVOKE_CLIENT,
217*84e872a0SLloyd Pique 				  &proxy->object, opcode, proxy->user_data);
218*84e872a0SLloyd Pique 	}
219*84e872a0SLloyd Piquediff --git a/src/wayland-private.h b/src/wayland-private.h
220*84e872a0SLloyd Piqueindex 9274f1b..66fc78f 100644
221*84e872a0SLloyd Pique--- a/src/wayland-private.h
222*84e872a0SLloyd Pique+++ b/src/wayland-private.h
223*84e872a0SLloyd Pique@@ -211,9 +211,8 @@ int
224*84e872a0SLloyd Pique wl_closure_queue(struct wl_closure *closure, struct wl_connection *connection);
225*84e872a0SLloyd Pique
226*84e872a0SLloyd Pique void
227*84e872a0SLloyd Pique-wl_closure_print(struct wl_closure *closure,
228*84e872a0SLloyd Pique-		 struct wl_object *target, int send, int discarded,
229*84e872a0SLloyd Pique-		 uint32_t (*n_parse)(union wl_argument *arg));
230*84e872a0SLloyd Pique+wl_closure_print(struct wl_closure *closure, struct wl_object *target,
231*84e872a0SLloyd Pique+		 bool send, const char *discarded_reason);
232*84e872a0SLloyd Pique
233*84e872a0SLloyd Pique void
234*84e872a0SLloyd Pique wl_closure_destroy(struct wl_closure *closure);
235*84e872a0SLloyd Piquediff --git a/src/wayland-server.c b/src/wayland-server.c
236*84e872a0SLloyd Piqueindex d51acc6..be98f7d 100644
237*84e872a0SLloyd Pique--- a/src/wayland-server.c
238*84e872a0SLloyd Pique+++ b/src/wayland-server.c
239*84e872a0SLloyd Pique@@ -157,7 +157,7 @@ log_closure(struct wl_resource *resource,
240*84e872a0SLloyd Pique 	struct wl_protocol_logger_message message;
241*84e872a0SLloyd Pique
242*84e872a0SLloyd Pique 	if (debug_server)
243*84e872a0SLloyd Pique-		wl_closure_print(closure, object, send, false, NULL);
244*84e872a0SLloyd Pique+		wl_closure_print(closure, object, send, NULL);
245*84e872a0SLloyd Pique
246*84e872a0SLloyd Pique 	if (!wl_list_empty(&display->protocol_loggers)) {
247*84e872a0SLloyd Pique 		message.resource = resource;
248