• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/channel/channel_stack.h"
22 
23 #include <inttypes.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/string_util.h>
30 #include "src/core/lib/gpr/string.h"
31 #include "src/core/lib/slice/slice_string_helpers.h"
32 #include "src/core/lib/transport/connectivity_state.h"
33 
34 /* These routines are here to facilitate debugging - they produce string
35    representations of various transport data structures */
36 
put_metadata(gpr_strvec * b,grpc_mdelem md)37 static void put_metadata(gpr_strvec* b, grpc_mdelem md) {
38   gpr_strvec_add(b, gpr_strdup("key="));
39   gpr_strvec_add(
40       b, grpc_dump_slice(GRPC_MDKEY(md), GPR_DUMP_HEX | GPR_DUMP_ASCII));
41 
42   gpr_strvec_add(b, gpr_strdup(" value="));
43   gpr_strvec_add(
44       b, grpc_dump_slice(GRPC_MDVALUE(md), GPR_DUMP_HEX | GPR_DUMP_ASCII));
45 }
46 
put_metadata_list(gpr_strvec * b,grpc_metadata_batch md)47 static void put_metadata_list(gpr_strvec* b, grpc_metadata_batch md) {
48   grpc_linked_mdelem* m;
49   for (m = md.list.head; m != nullptr; m = m->next) {
50     if (m != md.list.head) gpr_strvec_add(b, gpr_strdup(", "));
51     put_metadata(b, m->md);
52   }
53   if (md.deadline != GRPC_MILLIS_INF_FUTURE) {
54     char* tmp;
55     gpr_asprintf(&tmp, " deadline=%" PRId64, md.deadline);
56     gpr_strvec_add(b, tmp);
57   }
58 }
59 
grpc_transport_stream_op_batch_string(grpc_transport_stream_op_batch * op)60 char* grpc_transport_stream_op_batch_string(
61     grpc_transport_stream_op_batch* op) {
62   char* tmp;
63   char* out;
64 
65   gpr_strvec b;
66   gpr_strvec_init(&b);
67 
68   if (op->send_initial_metadata) {
69     gpr_strvec_add(&b, gpr_strdup(" "));
70     gpr_strvec_add(&b, gpr_strdup("SEND_INITIAL_METADATA{"));
71     put_metadata_list(
72         &b, *op->payload->send_initial_metadata.send_initial_metadata);
73     gpr_strvec_add(&b, gpr_strdup("}"));
74   }
75 
76   if (op->send_message) {
77     gpr_strvec_add(&b, gpr_strdup(" "));
78     if (op->payload->send_message.send_message != nullptr) {
79       gpr_asprintf(&tmp, "SEND_MESSAGE:flags=0x%08x:len=%d",
80                    op->payload->send_message.send_message->flags(),
81                    op->payload->send_message.send_message->length());
82     } else {
83       // This can happen when we check a batch after the transport has
84       // processed and cleared the send_message op.
85       tmp =
86           gpr_strdup("SEND_MESSAGE(flag and length unknown, already orphaned)");
87     }
88     gpr_strvec_add(&b, tmp);
89   }
90 
91   if (op->send_trailing_metadata) {
92     gpr_strvec_add(&b, gpr_strdup(" "));
93     gpr_strvec_add(&b, gpr_strdup("SEND_TRAILING_METADATA{"));
94     put_metadata_list(
95         &b, *op->payload->send_trailing_metadata.send_trailing_metadata);
96     gpr_strvec_add(&b, gpr_strdup("}"));
97   }
98 
99   if (op->recv_initial_metadata) {
100     gpr_strvec_add(&b, gpr_strdup(" "));
101     gpr_strvec_add(&b, gpr_strdup("RECV_INITIAL_METADATA"));
102   }
103 
104   if (op->recv_message) {
105     gpr_strvec_add(&b, gpr_strdup(" "));
106     gpr_strvec_add(&b, gpr_strdup("RECV_MESSAGE"));
107   }
108 
109   if (op->recv_trailing_metadata) {
110     gpr_strvec_add(&b, gpr_strdup(" "));
111     gpr_strvec_add(&b, gpr_strdup("RECV_TRAILING_METADATA"));
112   }
113 
114   if (op->cancel_stream) {
115     gpr_strvec_add(&b, gpr_strdup(" "));
116     const char* msg =
117         grpc_error_string(op->payload->cancel_stream.cancel_error);
118     gpr_asprintf(&tmp, "CANCEL:%s", msg);
119 
120     gpr_strvec_add(&b, tmp);
121   }
122 
123   out = gpr_strvec_flatten(&b, nullptr);
124   gpr_strvec_destroy(&b);
125 
126   return out;
127 }
128 
grpc_transport_op_string(grpc_transport_op * op)129 char* grpc_transport_op_string(grpc_transport_op* op) {
130   char* tmp;
131   char* out;
132   bool first = true;
133 
134   gpr_strvec b;
135   gpr_strvec_init(&b);
136 
137   if (op->on_connectivity_state_change != nullptr) {
138     if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
139     first = false;
140     if (op->connectivity_state != nullptr) {
141       gpr_asprintf(&tmp, "ON_CONNECTIVITY_STATE_CHANGE:p=%p:from=%s",
142                    op->on_connectivity_state_change,
143                    grpc_connectivity_state_name(*op->connectivity_state));
144       gpr_strvec_add(&b, tmp);
145     } else {
146       gpr_asprintf(&tmp, "ON_CONNECTIVITY_STATE_CHANGE:p=%p:unsubscribe",
147                    op->on_connectivity_state_change);
148       gpr_strvec_add(&b, tmp);
149     }
150   }
151 
152   if (op->disconnect_with_error != GRPC_ERROR_NONE) {
153     if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
154     first = false;
155     const char* err = grpc_error_string(op->disconnect_with_error);
156     gpr_asprintf(&tmp, "DISCONNECT:%s", err);
157     gpr_strvec_add(&b, tmp);
158   }
159 
160   if (op->goaway_error) {
161     if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
162     first = false;
163     const char* msg = grpc_error_string(op->goaway_error);
164     gpr_asprintf(&tmp, "SEND_GOAWAY:%s", msg);
165 
166     gpr_strvec_add(&b, tmp);
167   }
168 
169   if (op->set_accept_stream) {
170     if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
171     first = false;
172     gpr_asprintf(&tmp, "SET_ACCEPT_STREAM:%p(%p,...)", op->set_accept_stream_fn,
173                  op->set_accept_stream_user_data);
174     gpr_strvec_add(&b, tmp);
175   }
176 
177   if (op->bind_pollset != nullptr) {
178     if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
179     first = false;
180     gpr_strvec_add(&b, gpr_strdup("BIND_POLLSET"));
181   }
182 
183   if (op->bind_pollset_set != nullptr) {
184     if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
185     first = false;
186     gpr_strvec_add(&b, gpr_strdup("BIND_POLLSET_SET"));
187   }
188 
189   if (op->send_ping.on_initiate != nullptr || op->send_ping.on_ack != nullptr) {
190     if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
191     // first = false;
192     gpr_strvec_add(&b, gpr_strdup("SEND_PING"));
193   }
194 
195   out = gpr_strvec_flatten(&b, nullptr);
196   gpr_strvec_destroy(&b);
197 
198   return out;
199 }
200 
grpc_call_log_op(const char * file,int line,gpr_log_severity severity,grpc_call_element * elem,grpc_transport_stream_op_batch * op)201 void grpc_call_log_op(const char* file, int line, gpr_log_severity severity,
202                       grpc_call_element* elem,
203                       grpc_transport_stream_op_batch* op) {
204   char* str = grpc_transport_stream_op_batch_string(op);
205   gpr_log(file, line, severity, "OP[%s:%p]: %s", elem->filter->name, elem, str);
206   gpr_free(str);
207 }
208