LIBCOUCHBASE_API bool libcouchbase_packet_debug(libcouchbase_t instance, const void *ptr) { (void)instance; const protocol_binary_request_header *req = ptr; assert((req->request.magic == PROTOCOL_BINARY_REQ) || (req->request.magic == PROTOCOL_BINARY_RES)); print_header(); dump_bytes((void*)req, ntohl(req->request.bodylen) + sizeof(*req)); printf("\n\nHeader breakdown\n"); printf("Field (offset) (value)\n"); printf("Magic (0): 0x%02x (%s)\n", req->request.magic, req->request.magic == PROTOCOL_BINARY_REQ ? "PROTOCOL_BINARY_REQ" : "PROTOCOL_BINARY_RES"); printf("Opcode (1): 0x%02x (%s)\n", req->request.opcode, get_command_name(req->request.opcode)); printf("Key length (2-3): "); print_uint16(req->request.keylen, true, true); printf("Extra length (4): 0x%02x\n", req->request.extlen); printf("Data type (5): 0x%02x\n", req->request.datatype); if (req->request.magic == PROTOCOL_BINARY_REQ) { printf("vbucket (6-7): "); print_uint16(req->request.vbucket, true, true); } else { printf("Status (6-7): "); print_uint16(req->request.vbucket, false, false); printf(" (%s)\n", get_response_name(req->request.vbucket)); } printf("Total body (8-11): "); print_uint32(req->request.bodylen, true, true); printf("Opaque (12-15): "); print_uint32(req->request.opaque, true, true); printf("CAS (16-23): "); print_uint64(req->request.cas, true, true); if (req->request.magic == PROTOCOL_BINARY_REQ) { if (packets[req->request.opcode].req != NULL) { packets[req->request.opcode].req((void*)req); } } else { if (packets[req->request.opcode].res != NULL) { packets[req->request.opcode].res((void*)req); } } return true; }
/** * The simple implementation of printf. */ void term_printf(const char* fmt, ...) { va_list ap; int32_t d; uint32_t u; uint32_t h; char c, *s; va_start(ap, fmt); while(*fmt != '\0') { if(*fmt == '%') { fmt++; if(*fmt == 'd') { d = va_arg(ap, int32_t); print_int32(d); } else if(*fmt == 'u') { u = va_arg(ap, uint32_t); print_uint32(u); } else if(*fmt == 'h') { h = va_arg(ap, uint32_t); print_hex(h); } else if(*fmt == 's') { s = va_arg(ap, char*); print_string(s); } else if(*fmt == 'c') {
static void print_int32(int32_t x) { if(x < 0) { put_char('-'); x *= -1; } print_uint32((uint32_t) x); }
static void debug_tap_vbucket_set_request(protocol_binary_request_header *req) { protocol_binary_request_tap_vbucket_set *t = (void*)req; debug_tap_common_body((void*)req); int nextbyte = sizeof(protocol_binary_request_tap_no_extras); printf("VB State (%02d-%02d): ", nextbyte, nextbyte + (int)sizeof(uint32_t) - 1); uint32_t state; memcpy(&state, t + 1, sizeof(state)); print_uint32(state, false, false); printf(" (%s)\n", get_vbucket_state(state)); nextbyte += sizeof(state); }
static void debug_tap_mutation_request(protocol_binary_request_header *req) { debug_tap_common_body((void*)req); protocol_binary_request_tap_mutation *t = (void*)req; int nextbyte = sizeof(protocol_binary_request_tap_no_extras); printf("Item Flags (%02d-%02d): ", nextbyte, nextbyte + (int)sizeof(t->message.body.item.flags) - 1); print_uint32(t->message.body.item.flags, false, true); nextbyte += sizeof(t->message.body.item.flags); printf("Item Expiry (%02d-%02d): ", nextbyte, nextbyte + (int)sizeof(t->message.body.item.expiration) - 1); print_uint32(t->message.body.item.expiration, false, true); nextbyte += sizeof(t->message.body.item.expiration); uint8_t *ptr = (uint8_t*)(t + 1); uint16_t keylen = ntohs(req->request.keylen); if (keylen > 0) { printf("Key (%02d-%02d): [", nextbyte, nextbyte + keylen - 1); print_string(ptr, (size_t)keylen); printf("]\n"); ptr += keylen; nextbyte += keylen; } uint32_t bodylen = ntohl(req->request.bodylen); bodylen -= keylen; bodylen -= req->request.extlen; bodylen -= ntohs(t->message.body.tap.enginespecific_length); if (bodylen > 0) { printf("Value (%02d-%02d): [", nextbyte, nextbyte + keylen - 1); print_string(ptr, (size_t)(bodylen > 20 ? 20 : bodylen)); printf("%s]\n", bodylen > 20 ? " ...cut..." : ""); ptr += bodylen; nextbyte += bodylen; } }
int http_send_content_length_header(http_t *ctx) { if ((uint64_t) -1 != ctx->content_length) { static const char length_hdr[] = "Content-Length: "; char buf[UINT64_DEC_BUFLEN]; http_send_data(ctx, length_hdr, sizeof length_hdr - 1); if (ctx->content_length > (uint32_t) -1) { print_uint64(buf, sizeof buf, ctx->content_length); } else { print_uint32(buf, sizeof buf, ctx->content_length); } return http_send_line(ctx, buf); } return 0; }
/********************************************************************** * Description: * Prints Formatted strings for STD out. Format specifiers * provide what type of data to be printed and the data value is taken * from the argument list. * * Input: * One or more Formatted string messages to be printed allong with * other data values to be printed. * * Output: * Number of characters actually printed. -1 if failure. *********************************************************************/ int printk(char *fmt, ...) { va_list arg; int ret = 0; uint32_t val; uint64_t val64; //Check if valid putc() is registered. if (_putc == NULL) return -1; // Variable argument processing va_start(arg, fmt); // Till the end of string while(*fmt != '\0') { // Format the data to be printed if(*fmt == '%') { fmt++; switch(*fmt) { case 'd': // Print the integer val = va_arg(arg,int); ret += print_int32((int)val); break; case 'x': // Print Hex val = va_arg(arg, uint32_t); ret += print_hex32(val, 1); break; case 'c': // Print the char val = va_arg(arg, int); _putc((char)val); ret++; break; case 'u': // Print unsigned int val = va_arg(arg, uint32_t); ret += print_uint32(val); break; case 's': // Print the string val = va_arg(arg, uint32_t); ret += print_string((char*)val); break; case 'l': // Long data switch(*(++fmt)) { case 'u': val64 = va_arg(arg, uint64_t); ret += print_uint64(val64); break; case 'd': val64 = va_arg(arg, int64_t); ret += print_int64(val64); break; case 'x': val64 = va_arg(arg, uint64_t); ret += print_hex64(val64); break; default: val64 = va_arg(arg, int64_t); ret += print_int64(val64); break; } break; default: _putc(*fmt); ret++; break; } } else {
static void debug_tap_connect_request(protocol_binary_request_header *req) { if (req->request.extlen < sizeof(uint32_t)) { printf("Unknown extras field\n"); return ; } int nextbyte = sizeof(*req); protocol_binary_request_tap_connect *t = (void*)req; printf("Flags (%02d-%02d): ", nextbyte, nextbyte + (int)sizeof(t->message.body.flags) - 1); print_uint32(t->message.body.flags, false, true); nextbyte += sizeof(t->message.body.flags); char buffer[1024]; buffer[0] = '\0'; uint32_t flags = ntohl(t->message.body.flags); if (flags & TAP_CONNECT_FLAG_BACKFILL) { strcat(buffer, ", backfill"); } if (flags & TAP_CONNECT_FLAG_DUMP) { strcat(buffer, ", dump"); } if (flags & TAP_CONNECT_FLAG_LIST_VBUCKETS) { strcat(buffer, ", list vbuckets"); } if (flags & TAP_CONNECT_FLAG_TAKEOVER_VBUCKETS) { strcat(buffer, ", takeover vbuckets"); } if (flags & TAP_CONNECT_SUPPORT_ACK) { strcat(buffer, ", support ack"); } if (flags & TAP_CONNECT_REQUEST_KEYS_ONLY) { strcat(buffer, ", request keys only"); } if (buffer[0] != '\0') { printf(" %s\n", buffer + 2); } uint8_t *ptr = (void*)(t->bytes + sizeof(t->bytes)); uint16_t keylen = ntohs(req->request.keylen); if (keylen > 0) { printf("Name (%02d-%02d): [", nextbyte, nextbyte + keylen - 1); print_string((void*)ptr, (size_t)keylen); printf("]\n"); nextbyte += keylen; ptr += keylen; } if (flags & TAP_CONNECT_FLAG_BACKFILL) { uint64_t backfill; memcpy(&backfill, ptr, sizeof(backfill)); printf("Backfill date(%02d-%02d): ", nextbyte, nextbyte + (int)sizeof(backfill) - 1); print_uint64(backfill, true, true); nextbyte += sizeof(backfill); ptr += sizeof(backfill); } if (flags & TAP_CONNECT_FLAG_LIST_VBUCKETS) { uint16_t num; uint16_t val; memcpy(&val, ptr, sizeof(val)); num = ntohs(val); printf("VBucket list (%02d-%02d): \n", nextbyte, nextbyte + (2 * num) + 1); printf(" # listed (%02d-%02d): ", nextbyte, nextbyte + (int)sizeof(num) - 1); print_uint16(val, true, true); nextbyte += sizeof(num); ptr += sizeof(num); for (uint16_t ii = 0; ii < num; ++ii) { memcpy(&val, ptr, sizeof(num)); if (ii < 5 || num < 10) { printf(" vbucket (%02d-%02d): ", nextbyte, nextbyte + (int)sizeof(val) - 1); print_uint16(val, true, true); } else if (ii == num - 1) { printf(" (skipped %d)\n", num - 5); } nextbyte += sizeof(val); ptr += sizeof(val); } } fflush(stdout); }