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 3710 : static void put_metadata(gpr_strvec *b, grpc_mdelem *md) {
49 3710 : gpr_strvec_add(b, gpr_strdup("key="));
50 3710 : gpr_strvec_add(b,
51 3710 : gpr_dump_slice(md->key->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
52 :
53 3710 : gpr_strvec_add(b, gpr_strdup(" value="));
54 3710 : gpr_strvec_add(
55 3710 : b, gpr_dump_slice(md->value->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
56 3710 : }
57 :
58 1788 : static void put_metadata_list(gpr_strvec *b, grpc_metadata_batch md) {
59 : grpc_linked_mdelem *m;
60 5498 : for (m = md.list.head; m != NULL; m = m->next) {
61 3710 : if (m != md.list.head) gpr_strvec_add(b, gpr_strdup(", "));
62 3710 : put_metadata(b, m->md);
63 : }
64 1788 : if (gpr_time_cmp(md.deadline, gpr_inf_future(md.deadline.clock_type)) != 0) {
65 : char *tmp;
66 924 : gpr_asprintf(&tmp, " deadline=%d.%09d", md.deadline.tv_sec,
67 : md.deadline.tv_nsec);
68 924 : gpr_strvec_add(b, tmp);
69 : }
70 1788 : }
71 :
72 2719 : char *grpc_transport_stream_op_string(grpc_transport_stream_op *op) {
73 : char *tmp;
74 : char *out;
75 2719 : int first = 1;
76 :
77 : gpr_strvec b;
78 2719 : gpr_strvec_init(&b);
79 :
80 2719 : if (op->send_initial_metadata != NULL) {
81 928 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
82 928 : first = 0;
83 928 : gpr_strvec_add(&b, gpr_strdup("SEND_INITIAL_METADATA{"));
84 928 : put_metadata_list(&b, *op->send_initial_metadata);
85 928 : gpr_strvec_add(&b, gpr_strdup("}"));
86 : }
87 :
88 2719 : if (op->send_message != NULL) {
89 728 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
90 728 : first = 0;
91 1456 : gpr_asprintf(&tmp, "SEND_MESSAGE:flags=0x%08x:len=%d",
92 1456 : op->send_message->flags, op->send_message->length);
93 728 : gpr_strvec_add(&b, tmp);
94 : }
95 :
96 2719 : if (op->send_trailing_metadata != NULL) {
97 860 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
98 860 : first = 0;
99 860 : gpr_strvec_add(&b, gpr_strdup("SEND_TRAILING_METADATA{"));
100 860 : put_metadata_list(&b, *op->send_trailing_metadata);
101 860 : gpr_strvec_add(&b, gpr_strdup("}"));
102 : }
103 :
104 2719 : if (op->recv_initial_metadata != NULL) {
105 954 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
106 954 : first = 0;
107 954 : gpr_strvec_add(&b, gpr_strdup("RECV_INITIAL_METADATA"));
108 : }
109 :
110 2719 : if (op->recv_message != NULL) {
111 698 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
112 698 : first = 0;
113 698 : gpr_strvec_add(&b, gpr_strdup("RECV_MESSAGE"));
114 : }
115 :
116 2719 : if (op->recv_trailing_metadata != NULL) {
117 960 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
118 960 : first = 0;
119 960 : gpr_strvec_add(&b, gpr_strdup("RECV_TRAILING_METADATA"));
120 : }
121 :
122 2719 : if (op->cancel_with_status != GRPC_STATUS_OK) {
123 245 : if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
124 245 : first = 0;
125 245 : gpr_asprintf(&tmp, "CANCEL:%d", op->cancel_with_status);
126 245 : gpr_strvec_add(&b, tmp);
127 : }
128 :
129 2719 : out = gpr_strvec_flatten(&b, NULL);
130 2719 : gpr_strvec_destroy(&b);
131 :
132 2719 : return out;
133 : }
134 :
135 2719 : void grpc_call_log_op(char *file, int line, gpr_log_severity severity,
136 : grpc_call_element *elem, grpc_transport_stream_op *op) {
137 2719 : char *str = grpc_transport_stream_op_string(op);
138 2719 : gpr_log(file, line, severity, "OP[%s:%p]: %s", elem->filter->name, elem, str);
139 2719 : gpr_free(str);
140 2719 : }
|