blob: f62c340e97f4d21df96080fece50c3b3ead1ded6 [file] [log] [blame]
Craig Tiller83f88d92015-04-21 16:02:05 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/channel/channel_stack.h"
35
36#include <stdarg.h>
37#include <stdio.h>
38#include <string.h>
39
40#include "src/core/support/string.h"
41#include <grpc/support/alloc.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070042#include <grpc/support/string_util.h>
Craig Tiller83f88d92015-04-21 16:02:05 -070043#include <grpc/support/useful.h>
44
Craig Tiller816791c2015-04-28 16:55:08 -070045/* These routines are here to facilitate debugging - they produce string
46 representations of various transport data structures */
47
Craig Tiller83f88d92015-04-21 16:02:05 -070048static void put_metadata(gpr_strvec *b, grpc_mdelem *md) {
Craig Tiller6e84aba2015-04-23 15:08:17 -070049 gpr_strvec_add(b, gpr_strdup("key="));
Julien Boeufda13cd22015-06-29 19:25:32 +020050 gpr_strvec_add(b,
51 gpr_dump_slice(md->key->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
Craig Tiller83f88d92015-04-21 16:02:05 -070052
53 gpr_strvec_add(b, gpr_strdup(" value="));
Julien Boeufda13cd22015-06-29 19:25:32 +020054 gpr_strvec_add(
55 b, gpr_dump_slice(md->value->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
Craig Tiller83f88d92015-04-21 16:02:05 -070056}
57
58static void put_metadata_list(gpr_strvec *b, grpc_metadata_batch md) {
59 grpc_linked_mdelem *m;
60 for (m = md.list.head; m != NULL; m = m->next) {
Craig Tiller6e84aba2015-04-23 15:08:17 -070061 if (m != md.list.head) gpr_strvec_add(b, gpr_strdup(", "));
Craig Tiller83f88d92015-04-21 16:02:05 -070062 put_metadata(b, m->md);
63 }
Craig Tiller6a7626c2015-07-19 22:21:41 -070064 if (gpr_time_cmp(md.deadline, gpr_inf_future(md.deadline.clock_type)) != 0) {
Craig Tiller83f88d92015-04-21 16:02:05 -070065 char *tmp;
66 gpr_asprintf(&tmp, " deadline=%d.%09d", md.deadline.tv_sec,
67 md.deadline.tv_nsec);
68 gpr_strvec_add(b, tmp);
69 }
70}
71
Craig Tillerfbf5be22015-04-22 16:17:09 -070072char *grpc_sopb_string(grpc_stream_op_buffer *sopb) {
73 char *out;
74 char *tmp;
75 size_t i;
76 gpr_strvec b;
77 gpr_strvec_init(&b);
78
79 for (i = 0; i < sopb->nops; i++) {
80 grpc_stream_op *op = &sopb->ops[i];
Craig Tiller6e84aba2015-04-23 15:08:17 -070081 if (i > 0) gpr_strvec_add(&b, gpr_strdup(", "));
Craig Tillerfbf5be22015-04-22 16:17:09 -070082 switch (op->type) {
83 case GRPC_NO_OP:
84 gpr_strvec_add(&b, gpr_strdup("NO_OP"));
85 break;
86 case GRPC_OP_BEGIN_MESSAGE:
87 gpr_asprintf(&tmp, "BEGIN_MESSAGE:%d", op->data.begin_message.length);
88 gpr_strvec_add(&b, tmp);
89 break;
90 case GRPC_OP_SLICE:
91 gpr_asprintf(&tmp, "SLICE:%d", GPR_SLICE_LENGTH(op->data.slice));
Craig Tiller65f9f812015-04-24 16:53:20 -070092 gpr_strvec_add(&b, tmp);
Craig Tillerfbf5be22015-04-22 16:17:09 -070093 break;
94 case GRPC_OP_METADATA:
Craig Tiller6e84aba2015-04-23 15:08:17 -070095 gpr_strvec_add(&b, gpr_strdup("METADATA{"));
Craig Tillerfbf5be22015-04-22 16:17:09 -070096 put_metadata_list(&b, op->data.metadata);
Craig Tiller6e84aba2015-04-23 15:08:17 -070097 gpr_strvec_add(&b, gpr_strdup("}"));
Craig Tillerfbf5be22015-04-22 16:17:09 -070098 break;
99 }
100 }
101
102 out = gpr_strvec_flatten(&b, NULL);
103 gpr_strvec_destroy(&b);
104
105 return out;
106}
107
Craig Tillerb7959a02015-06-25 08:50:54 -0700108char *grpc_transport_stream_op_string(grpc_transport_stream_op *op) {
Craig Tiller83f88d92015-04-21 16:02:05 -0700109 char *tmp;
110 char *out;
Craig Tillerfbf5be22015-04-22 16:17:09 -0700111 int first = 1;
Craig Tiller83f88d92015-04-21 16:02:05 -0700112
113 gpr_strvec b;
114 gpr_strvec_init(&b);
115
Craig Tillerfbf5be22015-04-22 16:17:09 -0700116 if (op->send_ops) {
117 if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
118 first = 0;
Craig Tiller05cc0c42015-07-23 16:01:27 -0700119 gpr_asprintf(&tmp, "SEND%s:%p", op->is_last_send ? "_LAST" : "",
120 op->on_done_send);
121 gpr_strvec_add(&b, tmp);
Craig Tillerfbf5be22015-04-22 16:17:09 -0700122 gpr_strvec_add(&b, gpr_strdup("["));
123 gpr_strvec_add(&b, grpc_sopb_string(op->send_ops));
124 gpr_strvec_add(&b, gpr_strdup("]"));
Craig Tiller83f88d92015-04-21 16:02:05 -0700125 }
Craig Tillerfbf5be22015-04-22 16:17:09 -0700126
127 if (op->recv_ops) {
128 if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
129 first = 0;
Craig Tiller05cc0c42015-07-23 16:01:27 -0700130 gpr_asprintf(&tmp, "RECV:%p:max_recv_bytes=%d", op->on_done_recv,
131 op->max_recv_bytes);
Craig Tiller4efb6962015-06-03 09:32:41 -0700132 gpr_strvec_add(&b, tmp);
Craig Tiller83f88d92015-04-21 16:02:05 -0700133 }
Craig Tillerfbf5be22015-04-22 16:17:09 -0700134
Craig Tiller83f88d92015-04-21 16:02:05 -0700135 if (op->bind_pollset) {
Craig Tillerfbf5be22015-04-22 16:17:09 -0700136 if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
137 first = 0;
138 gpr_strvec_add(&b, gpr_strdup("BIND"));
139 }
140
141 if (op->cancel_with_status != GRPC_STATUS_OK) {
142 if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
143 first = 0;
144 gpr_asprintf(&tmp, "CANCEL:%d", op->cancel_with_status);
145 gpr_strvec_add(&b, tmp);
Craig Tiller83f88d92015-04-21 16:02:05 -0700146 }
147
Craig Tillerb9a46ae2015-07-01 09:45:21 -0700148 if (op->on_consumed != NULL) {
149 if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
150 first = 0;
151 gpr_asprintf(&tmp, "ON_CONSUMED:%p", op->on_consumed);
152 gpr_strvec_add(&b, tmp);
153 }
154
Craig Tiller83f88d92015-04-21 16:02:05 -0700155 out = gpr_strvec_flatten(&b, NULL);
156 gpr_strvec_destroy(&b);
157
158 return out;
159}
160
161void grpc_call_log_op(char *file, int line, gpr_log_severity severity,
Craig Tillerb7959a02015-06-25 08:50:54 -0700162 grpc_call_element *elem, grpc_transport_stream_op *op) {
163 char *str = grpc_transport_stream_op_string(op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700164 gpr_log(file, line, severity, "OP[%s:%p]: %s", elem->filter->name, elem, str);
165 gpr_free(str);
166}