Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / lib / transport / transport_op_string.cc
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 <inttypes.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <vector>
27
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/str_join.h"
31
32 #include <grpc/support/alloc.h>
33 #include <grpc/support/string_util.h>
34
35 #include "src/core/lib/channel/channel_stack.h"
36 #include "src/core/lib/gpr/string.h"
37 #include "src/core/lib/slice/slice_string_helpers.h"
38 #include "src/core/lib/transport/connectivity_state.h"
39
40 /* These routines are here to facilitate debugging - they produce string
41    representations of various transport data structures */
42
43 static void put_metadata(grpc_mdelem md, std::vector<std::string>* out) {
44   out->push_back("key=");
45   char* dump = grpc_dump_slice(GRPC_MDKEY(md), GPR_DUMP_HEX | GPR_DUMP_ASCII);
46   out->push_back(dump);
47   gpr_free(dump);
48   out->push_back(" value=");
49   dump = grpc_dump_slice(GRPC_MDVALUE(md), GPR_DUMP_HEX | GPR_DUMP_ASCII);
50   out->push_back(dump);
51   gpr_free(dump);
52 }
53
54 static void put_metadata_list(grpc_metadata_batch md,
55                               std::vector<std::string>* out) {
56   grpc_linked_mdelem* m;
57   for (m = md.list.head; m != nullptr; m = m->next) {
58     if (m != md.list.head) out->push_back(", ");
59     put_metadata(m->md, out);
60   }
61   if (md.deadline != GRPC_MILLIS_INF_FUTURE) {
62     out->push_back(absl::StrFormat(" deadline=%" PRId64, md.deadline));
63   }
64 }
65
66 std::string grpc_transport_stream_op_batch_string(
67     grpc_transport_stream_op_batch* op) {
68   std::vector<std::string> out;
69
70   if (op->send_initial_metadata) {
71     out.push_back(" SEND_INITIAL_METADATA{");
72     put_metadata_list(*op->payload->send_initial_metadata.send_initial_metadata,
73                       &out);
74     out.push_back("}");
75   }
76
77   if (op->send_message) {
78     if (op->payload->send_message.send_message != nullptr) {
79       out.push_back(
80           absl::StrFormat(" SEND_MESSAGE:flags=0x%08x:len=%d",
81                           op->payload->send_message.send_message->flags(),
82                           op->payload->send_message.send_message->length()));
83     } else {
84       // This can happen when we check a batch after the transport has
85       // processed and cleared the send_message op.
86       out.push_back(" SEND_MESSAGE(flag and length unknown, already orphaned)");
87     }
88   }
89
90   if (op->send_trailing_metadata) {
91     out.push_back(" SEND_TRAILING_METADATA{");
92     put_metadata_list(
93         *op->payload->send_trailing_metadata.send_trailing_metadata, &out);
94     out.push_back("}");
95   }
96
97   if (op->recv_initial_metadata) {
98     out.push_back(" RECV_INITIAL_METADATA");
99   }
100
101   if (op->recv_message) {
102     out.push_back(" RECV_MESSAGE");
103   }
104
105   if (op->recv_trailing_metadata) {
106     out.push_back(" RECV_TRAILING_METADATA");
107   }
108
109   if (op->cancel_stream) {
110     out.push_back(absl::StrCat(
111         " CANCEL:",
112         grpc_error_std_string(op->payload->cancel_stream.cancel_error)));
113   }
114
115   return absl::StrJoin(out, "");
116 }
117
118 std::string grpc_transport_op_string(grpc_transport_op* op) {
119   std::vector<std::string> out;
120
121   if (op->start_connectivity_watch != nullptr) {
122     out.push_back(absl::StrFormat(
123         " START_CONNECTIVITY_WATCH:watcher=%p:from=%s",
124         op->start_connectivity_watch.get(),
125         grpc_core::ConnectivityStateName(op->start_connectivity_watch_state)));
126   }
127
128   if (op->stop_connectivity_watch != nullptr) {
129     out.push_back(absl::StrFormat(" STOP_CONNECTIVITY_WATCH:watcher=%p",
130                                   op->stop_connectivity_watch));
131   }
132
133   if (op->disconnect_with_error != GRPC_ERROR_NONE) {
134     out.push_back(absl::StrCat(
135         " DISCONNECT:", grpc_error_std_string(op->disconnect_with_error)));
136   }
137
138   if (op->goaway_error != GRPC_ERROR_NONE) {
139     out.push_back(absl::StrCat(" SEND_GOAWAY:%s",
140                                grpc_error_std_string(op->goaway_error)));
141   }
142
143   if (op->set_accept_stream) {
144     out.push_back(absl::StrFormat(" SET_ACCEPT_STREAM:%p(%p,...)",
145                                   op->set_accept_stream_fn,
146                                   op->set_accept_stream_user_data));
147   }
148
149   if (op->bind_pollset != nullptr) {
150     out.push_back(" BIND_POLLSET");
151   }
152
153   if (op->bind_pollset_set != nullptr) {
154     out.push_back(" BIND_POLLSET_SET");
155   }
156
157   if (op->send_ping.on_initiate != nullptr || op->send_ping.on_ack != nullptr) {
158     out.push_back(" SEND_PING");
159   }
160
161   return absl::StrJoin(out, "");
162 }
163
164 void grpc_call_log_op(const char* file, int line, gpr_log_severity severity,
165                       grpc_call_element* elem,
166                       grpc_transport_stream_op_batch* op) {
167   gpr_log(file, line, severity, "OP[%s:%p]: %s", elem->filter->name, elem,
168           grpc_transport_stream_op_batch_string(op).c_str());
169 }