3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/str_join.h"
32 #include <grpc/support/alloc.h>
33 #include <grpc/support/string_util.h>
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"
40 /* These routines are here to facilitate debugging - they produce string
41 representations of various transport data structures */
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);
48 out->push_back(" value=");
49 dump = grpc_dump_slice(GRPC_MDVALUE(md), GPR_DUMP_HEX | GPR_DUMP_ASCII);
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);
61 if (md.deadline != GRPC_MILLIS_INF_FUTURE) {
62 out->push_back(absl::StrFormat(" deadline=%" PRId64, md.deadline));
66 std::string grpc_transport_stream_op_batch_string(
67 grpc_transport_stream_op_batch* op) {
68 std::vector<std::string> out;
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,
77 if (op->send_message) {
78 if (op->payload->send_message.send_message != nullptr) {
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()));
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)");
90 if (op->send_trailing_metadata) {
91 out.push_back(" SEND_TRAILING_METADATA{");
93 *op->payload->send_trailing_metadata.send_trailing_metadata, &out);
97 if (op->recv_initial_metadata) {
98 out.push_back(" RECV_INITIAL_METADATA");
101 if (op->recv_message) {
102 out.push_back(" RECV_MESSAGE");
105 if (op->recv_trailing_metadata) {
106 out.push_back(" RECV_TRAILING_METADATA");
109 if (op->cancel_stream) {
110 out.push_back(absl::StrCat(
112 grpc_error_std_string(op->payload->cancel_stream.cancel_error)));
115 return absl::StrJoin(out, "");
118 std::string grpc_transport_op_string(grpc_transport_op* op) {
119 std::vector<std::string> out;
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)));
128 if (op->stop_connectivity_watch != nullptr) {
129 out.push_back(absl::StrFormat(" STOP_CONNECTIVITY_WATCH:watcher=%p",
130 op->stop_connectivity_watch));
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)));
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)));
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));
149 if (op->bind_pollset != nullptr) {
150 out.push_back(" BIND_POLLSET");
153 if (op->bind_pollset_set != nullptr) {
154 out.push_back(" BIND_POLLSET_SET");
157 if (op->send_ping.on_initiate != nullptr || op->send_ping.on_ack != nullptr) {
158 out.push_back(" SEND_PING");
161 return absl::StrJoin(out, "");
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());