Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 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> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 42 | #include <grpc/support/string_util.h> |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 43 | #include <grpc/support/useful.h> |
| 44 | |
Craig Tiller | 816791c | 2015-04-28 16:55:08 -0700 | [diff] [blame] | 45 | /* These routines are here to facilitate debugging - they produce string |
| 46 | representations of various transport data structures */ |
| 47 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 48 | static void put_metadata(gpr_strvec *b, grpc_mdelem *md) { |
Craig Tiller | 6e84aba | 2015-04-23 15:08:17 -0700 | [diff] [blame] | 49 | gpr_strvec_add(b, gpr_strdup("key=")); |
Julien Boeuf | da13cd2 | 2015-06-29 19:25:32 +0200 | [diff] [blame] | 50 | gpr_strvec_add(b, |
| 51 | gpr_dump_slice(md->key->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII)); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 52 | |
| 53 | gpr_strvec_add(b, gpr_strdup(" value=")); |
Julien Boeuf | da13cd2 | 2015-06-29 19:25:32 +0200 | [diff] [blame] | 54 | gpr_strvec_add( |
| 55 | b, gpr_dump_slice(md->value->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII)); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static 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 Tiller | 6e84aba | 2015-04-23 15:08:17 -0700 | [diff] [blame] | 61 | if (m != md.list.head) gpr_strvec_add(b, gpr_strdup(", ")); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 62 | put_metadata(b, m->md); |
| 63 | } |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 64 | if (gpr_time_cmp(md.deadline, gpr_inf_future(md.deadline.clock_type)) != 0) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 65 | 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 Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 72 | char *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 Tiller | 6e84aba | 2015-04-23 15:08:17 -0700 | [diff] [blame] | 81 | if (i > 0) gpr_strvec_add(&b, gpr_strdup(", ")); |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 82 | 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 Tiller | 65f9f81 | 2015-04-24 16:53:20 -0700 | [diff] [blame] | 92 | gpr_strvec_add(&b, tmp); |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 93 | break; |
| 94 | case GRPC_OP_METADATA: |
Craig Tiller | 6e84aba | 2015-04-23 15:08:17 -0700 | [diff] [blame] | 95 | gpr_strvec_add(&b, gpr_strdup("METADATA{")); |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 96 | put_metadata_list(&b, op->data.metadata); |
Craig Tiller | 6e84aba | 2015-04-23 15:08:17 -0700 | [diff] [blame] | 97 | gpr_strvec_add(&b, gpr_strdup("}")); |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | out = gpr_strvec_flatten(&b, NULL); |
| 103 | gpr_strvec_destroy(&b); |
| 104 | |
| 105 | return out; |
| 106 | } |
| 107 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 108 | char *grpc_transport_stream_op_string(grpc_transport_stream_op *op) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 109 | char *tmp; |
| 110 | char *out; |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 111 | int first = 1; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 112 | |
| 113 | gpr_strvec b; |
| 114 | gpr_strvec_init(&b); |
| 115 | |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 116 | if (op->send_ops) { |
| 117 | if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); |
| 118 | first = 0; |
Craig Tiller | 05cc0c4 | 2015-07-23 16:01:27 -0700 | [diff] [blame] | 119 | gpr_asprintf(&tmp, "SEND%s:%p", op->is_last_send ? "_LAST" : "", |
| 120 | op->on_done_send); |
| 121 | gpr_strvec_add(&b, tmp); |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 122 | 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 Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 125 | } |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 126 | |
| 127 | if (op->recv_ops) { |
| 128 | if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); |
| 129 | first = 0; |
Craig Tiller | 05cc0c4 | 2015-07-23 16:01:27 -0700 | [diff] [blame] | 130 | gpr_asprintf(&tmp, "RECV:%p:max_recv_bytes=%d", op->on_done_recv, |
| 131 | op->max_recv_bytes); |
Craig Tiller | 4efb696 | 2015-06-03 09:32:41 -0700 | [diff] [blame] | 132 | gpr_strvec_add(&b, tmp); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 133 | } |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 134 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 135 | if (op->bind_pollset) { |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 136 | 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 Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Craig Tiller | b9a46ae | 2015-07-01 09:45:21 -0700 | [diff] [blame] | 148 | 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 Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 155 | out = gpr_strvec_flatten(&b, NULL); |
| 156 | gpr_strvec_destroy(&b); |
| 157 | |
| 158 | return out; |
| 159 | } |
| 160 | |
| 161 | void grpc_call_log_op(char *file, int line, gpr_log_severity severity, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 162 | grpc_call_element *elem, grpc_transport_stream_op *op) { |
| 163 | char *str = grpc_transport_stream_op_string(op); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 164 | gpr_log(file, line, severity, "OP[%s:%p]: %s", elem->filter->name, elem, str); |
| 165 | gpr_free(str); |
| 166 | } |