Line data Source code
1 : /*
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>
42 : #include <grpc/support/string_util.h>
43 : #include <grpc/support/useful.h>
44 :
45 : /* These routines are here to facilitate debugging - they produce string
46 : representations of various transport data structures */
47 :
48 3622 : static void put_metadata(gpr_strvec *b, grpc_mdelem *md) {
49 3622 : gpr_strvec_add(b, gpr_strdup("key="));
50 3622 : gpr_strvec_add(b,
51 3622 : gpr_dump_slice(md->key->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
52 :
53 3622 : gpr_strvec_add(b, gpr_strdup(" value="));
54 3622 : gpr_strvec_add(
55 3622 : b, gpr_dump_slice(md->value->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
56 3622 : }
57 :
58 1392 : static void put_metadata_list(gpr_strvec *b, grpc_metadata_batch md) {
59 : grpc_linked_mdelem *m;
60 5014 : for (m = md.list.head; m != NULL; m = m->next) {
61 3622 : if (m != md.list.head) gpr_strvec_add(b, gpr_strdup(", "));
62 3622 : put_metadata(b, m->md);
63 : }
64 1392 : if (gpr_time_cmp(md.deadline, gpr_inf_future(md.deadline.clock_type)) != 0) {
65 : char *tmp;
66 908 : gpr_asprintf(&tmp, " deadline=%d.%09d", md.deadline.tv_sec,
67 : md.deadline.tv_nsec);
68 908 : gpr_strvec_add(b, tmp);
69 : }
70 1392 : }
71 :
72 1590 : char *grpc_sopb_string(grpc_stream_op_buffer *sopb) {
73 : char *out;
74 : char *tmp;
75 : size_t i;
76 : gpr_strvec b;
77 1590 : gpr_strvec_init(&b);
78 :
79 4438 : for (i = 0; i < sopb->nops; i++) {
80 2848 : grpc_stream_op *op = &sopb->ops[i];
81 2848 : if (i > 0) gpr_strvec_add(&b, gpr_strdup(", "));
82 2848 : switch (op->type) {
83 : case GRPC_NO_OP:
84 0 : gpr_strvec_add(&b, gpr_strdup("NO_OP"));
85 0 : break;
86 : case GRPC_OP_BEGIN_MESSAGE:
87 728 : gpr_asprintf(&tmp, "BEGIN_MESSAGE:%d", op->data.begin_message.length);
88 728 : gpr_strvec_add(&b, tmp);
89 728 : break;
90 : case GRPC_OP_SLICE:
91 728 : gpr_asprintf(&tmp, "SLICE:%d", GPR_SLICE_LENGTH(op->data.slice));
92 728 : gpr_strvec_add(&b, tmp);
93 728 : break;
94 : case GRPC_OP_METADATA:
95 1392 : gpr_strvec_add(&b, gpr_strdup("METADATA{"));
96 1392 : put_metadata_list(&b, op->data.metadata);
97 1392 : gpr_strvec_add(&b, gpr_strdup("}"));
98 1392 : break;
99 : }
100 : }
101 :
102 1590 : out = gpr_strvec_flatten(&b, NULL);
103 1590 : gpr_strvec_destroy(&b);
104 :
105 1590 : return out;
106 : }
107 :
108 3518 : char *grpc_transport_stream_op_string(grpc_transport_stream_op *op) {
109 : char *tmp;
110 : char *out;
111 3518 : int first = 1;
112 :
113 : gpr_strvec b;
114 3518 : gpr_strvec_init(&b);
115 :
116 3518 : if (op->send_ops) {
117 1590 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
118 1590 : first = 0;
119 1590 : gpr_asprintf(&tmp, "SEND%s:%p", op->is_last_send ? "_LAST" : "",
120 : op->on_done_send);
121 1590 : gpr_strvec_add(&b, tmp);
122 1590 : gpr_strvec_add(&b, gpr_strdup("["));
123 1590 : gpr_strvec_add(&b, grpc_sopb_string(op->send_ops));
124 1590 : gpr_strvec_add(&b, gpr_strdup("]"));
125 : }
126 :
127 3518 : if (op->recv_ops) {
128 1654 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
129 1654 : first = 0;
130 1654 : gpr_asprintf(&tmp, "RECV:%p:max_recv_bytes=%d", op->on_done_recv,
131 : op->max_recv_bytes);
132 1654 : gpr_strvec_add(&b, tmp);
133 : }
134 :
135 3518 : if (op->bind_pollset) {
136 960 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
137 960 : first = 0;
138 960 : gpr_strvec_add(&b, gpr_strdup("BIND"));
139 : }
140 :
141 3518 : if (op->cancel_with_status != GRPC_STATUS_OK) {
142 176 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
143 176 : first = 0;
144 176 : gpr_asprintf(&tmp, "CANCEL:%d", op->cancel_with_status);
145 176 : gpr_strvec_add(&b, tmp);
146 : }
147 :
148 3518 : if (op->on_consumed != NULL) {
149 1108 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
150 1108 : first = 0;
151 1108 : gpr_asprintf(&tmp, "ON_CONSUMED:%p", op->on_consumed);
152 1108 : gpr_strvec_add(&b, tmp);
153 : }
154 :
155 3518 : out = gpr_strvec_flatten(&b, NULL);
156 3518 : gpr_strvec_destroy(&b);
157 :
158 3518 : return out;
159 : }
160 :
161 3518 : void grpc_call_log_op(char *file, int line, gpr_log_severity severity,
162 : grpc_call_element *elem, grpc_transport_stream_op *op) {
163 3518 : char *str = grpc_transport_stream_op_string(op);
164 3518 : gpr_log(file, line, severity, "OP[%s:%p]: %s", elem->filter->name, elem, str);
165 3518 : gpr_free(str);
166 3518 : }
|