Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/mysqlfs/debug.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include <bio.h>
#include "mysql.h"

Dbgname Cmds[] = {
	{ CMDsleep,			"sleep" },
	{ CMDquit,			"quit" },
	{ CMDinit_db,			"init db" },
	{ CMDquery,			"query" },
	{ CMDfield_list,		"field list" },
	{ CMDcreate_db,			"create db" },
	{ CMDdrop_db,			"drop db" },
	{ CMDrefresh,			"refresh" },
	{ CMDshutdown,			"shutdown" },
	{ CMDstatistics,		"statistics" },
	{ CMDprocess_info,		"process info" },
	{ CMDconnect,			"connect" },
	{ CMDprocess_kill,		"process kill" },
	{ CMDdebug,			"debug" },
	{ CMDping,			"ping" },
	{ CMDtime,			"time" },
	{ CMDdelayed_insert,		"delayed insert" },
	{ CMDchange_user,		"change user" },
	{ CMDbinlog_dump,		"binlog dump" },
	{ CMDtable_dump,		"table dump" },
	{ CMDconnect_out,		"connect out" },
	{ CMDregister_slave,		"register slave" },
	{ CMDstmt_prepare,		"stmt prepare" },
	{ CMDstmt_execute,		"stmt execute" },
	{ CMDstmt_send_long_data,	"stmt send long data" },
	{ CMDstmt_close,		"stmt close" },
	{ CMDstmt_reset,		"stmt reset" },
	{ CMDset_option,		"set option" },
	{ CMDstmt_fetch,		"stmt fetch" },
	{ 0,				nil },
};

Dbgname Caps[] = {
	{ CAPlong_passwords,		"long password" },
	{ CAPfound_rows,		"found rows" },
	{ CAPlong_flag,			"long flag" },
	{ CAPconnect_with_db,		"connect with db" },
	{ CAPno_schema,			"no schema" },
	{ CAPcompression,		"compression" },
	{ CAPodbc,			"odbc" },
	{ CAPlocal_files,		"local files" },
	{ CAPignore_space,		"ignore space" },
	{ CAPprotocol_41,		"protocol 4.1" },
	{ CAPinteractive,		"interactive" },
	{ CAPssl,			"ssl" },
	{ CAPignore_sigpipe,		"ignore sigpipe" },
	{ CAPtransactions,		"transactions" },
	{ CAPreserved,			"reserved" },
	{ CAPauthentication_41,		"authentication 4.1" },
	{ CAPmulti_statements,		"multi statements" },
	{ CAPmulti_results,		"multi results" },
	{ 0,				nil },
};

Dbgname Fields[] = {
	{ FLDdecimal,			"decimal" },
	{ FLDtiny,			"tiny" },
	{ FLDshort,			"short" },
	{ FLDlong,			"long" },
	{ FLDfloat,			"float" },
	{ FLDdouble,			"double" },
	{ FLDnull,			"null" },
	{ FLDtimestamp,			"timestamp" },
	{ FLDlonglong,			"longlong" },
	{ FLDint24,			"int24" },
	{ FLDdate,			"date" },
	{ FLDtime,			"time" },
	{ FLDdatetime,			"datetime" },
	{ FLDyear,			"year" },
	{ FLDnewdate,			"newdate" },
	{ FLDvarchar,			"varchar" },
	{ FLDbit,			"bit" },
	{ FLDnewdecimal,		"newdecimal" },
	{ FLDenum,			"enum" },
	{ FLDset,			"set" },
	{ FLDtiny_blob,			"tiny blob" },
	{ FLDmedium_blob,		"medium blob" },
	{ FLDlong_blob,			"long blob" },
	{ FLDblob,			"blob" },
	{ FLDvar_string,		"var string" },
	{ FLDstring,			"string" },
	{ FLDgeometry,			"geometry" },

	{ FLDcom_register_slave,	"com register slave" },
	{ FLDcom_stmt_prepare,		"com stmt prepare" },
	{ FLDcom_stmt_execute,		"com stmt execute" },
	{ FLDcom_stmt_send_long_data,	"com stmt send long data" },
	{ FLDcom_stmt_close,		"com stmt close" },
	{ FLDcom_stmt_reset,		"com stmt reset" },
	{ FLDcom_set_option,		"com set option" },
	{ FLDcom_stmt_fetch,		"com stmt fetch" },
	{ 0,				nil },
};

Dbgname Flags[] = {
	{ FLGnot_null,			"not null" },
	{ FLGpri_key,			"pri key" },
	{ FLGunique_key,		"unique key" },
	{ FLGmultiple_key,		"multiple key" },
	{ FLGblob,			"blob" },
	{ FLGunsigned,			"unsigned" },
	{ FLGzerofill,			"zerofill" },
	{ FLGbinary,			"binary" },
	{ FLGenum,			"enum" },
	{ FLGauto_increment,		"auto increment" },
	{ FLGtimestamp,			"timestamp" },
	{ FLGset,			"set" },
	{ FLGno_default,		"no default value" },
	{ 0,				nil },
};

Dbgname Status[] = {
	{ STin_trans,			"in trans" },
	{ STautocommit,			"autocommit" },
	{ STmore,			"more results" },
	{ STmore_exists,		"more results exists" },
	{ STbad_index,			"query no good index used" },
	{ STindex_unused,		"query no index used" },
	{ STcursor_exists,		"cursor exists" },
	{ STlast_sent,			"last row sent" },
	{ STdropped,			"db dropped" },
	{ STno_esc,			"no backslash escapes" },
	{ 0,				nil },
};

void
prbits(int fd, int n, Dbgname *db)
{
	int i;

	for(i = 0; db[i].s; i++)
		if((n & db[i].n) == db[i].n)
			fprint(fd, "%s, ", db[i].s);
}


char *
fldtype(int type, int flags)
{
	int n, i;
	static char buf[1024];

	n = 0;
	for(i = 0; Fields[i].s; i++)
		if(Fields[i].n == type)
			n += snprint(buf+n, sizeof(buf)-n, "%s, ", Fields[i].s);

	for(i = 0; Flags[i].s; i++)
		if((Flags[i].n & flags) == Flags[i].n)
			n += snprint(buf+n, sizeof(buf)-n, "%s, ", Flags[i].s);
	return buf;
}

void
xd(void *base, int len)
{
	enum { chunk = 16 };
	int addr, skip, got, i, j;
	unsigned char *buf, *prev;

	skip = 0;
	addr = 0;
	buf = prev = base;
	do{

		got = (len >= chunk)? chunk: len;
		if (prev != buf && memcmp(buf, prev, got) == 0){
			skip++;
			addr += chunk;
			buf += got;
			len -= got;
			continue;
		}
		if (skip)
			fprint(2, "\t****\n");

		fprint(2, "\t%06x  ", addr);
		for(i = 0; i < got; i++)
			fprint(2, "%02x ", buf[i]);

		for(j = i; j < chunk; j++)
			fprint(2, "   ");
		fprint(2, "  ");
		for(i = 0; i < got; i++)
			if(buf[i] < ' ' || buf[i] > '~')
				fprint(2, ".");
			else
				fprint(2, "%c", buf[i]);
		fprint(2, "\n");
		addr += chunk;
		prev = buf;
		buf +=  got;
		skip = 0;
		len -= got;
	} while(len > 0);

	if (skip){
		fprint(2, "\t****\n");

		fprint(2, "\t%06x  ", addr-chunk);
		for(i = 0; i < got; i++)
			fprint(2, "%02x ", buf[i]);

		for(j = i; j < chunk; j++)
			fprint(2, "   ");
		fprint(2, "  ");
		for(i = 0; i < got; i++)
			if(buf[i] < ' ' || buf[i] > '~')
				fprint(2, ".");
			else
				fprint(2, "%c", buf[i]);
		fprint(2, "\n");
	}
	fprint(2, "\n");
}

void
dumpfield(Field *f)
{
	fprint(2, "catalogue:  %s\n", f->cat);
	fprint(2, "database:   %s\n", f->db);
	fprint(2, "table:      %s\n", f->tab);
	fprint(2, "otab:       %s\n", f->otab);
	fprint(2, "name:       %s\n", f->name);
	fprint(2, "oname:      %s\n", f->oname);
	fprint(2, "lang:       %d\n", f->lang);
	fprint(2, "width:      %d\n", f->width);
	fprint(2, "type/flags: %s (%d,%d)\n", fldtype(f->type, f->flags), f->type, f->flags);
	fprint(2, "precision:  %d\n", f->prec);
	if(f->hasdef)
		fprint(2, "default:   %lld\n", f->def);
	else
		fprint(2, "default:   <nil>\n");
	fprint(2, "\n");
}

void
dumpresults(Results *res)
{
	Field *f;
	Row *r;
	Col *c;
	Biobuf out;
	int l, i, j, *widths;

	if(res == nil)
		return;

	Binit(&out, 1, OWRITE);

	/* find the maximul widths of fields */
	widths = emalloc9p(res->nf * sizeof(int));
	for(i = 0, f = res->fields; f; f = f->next, i++){
		l = strlen(f->name);
		if(widths[i] < l)
			widths[i] = l;
	}

	for(r = res->rows; r; r = r->next)
		for(i = 0, c = r->cols; c; c = c->next, i++){
			if(c->str)
				l = strlen(c->str);
			else
				l = 5;		/* strlen("<nil>") */
			if(widths[i] < l)
				widths[i] = l;
		}

	/* real work */

	for(i = 0; i < res->nf; i++)
		for(j = 0; j < widths[i]+3; j++)
			Bprint(&out, "-");
	Bprint(&out, "-\n");

	Bprint(&out, "| ");
	for(i = 0, f = res->fields; f; f = f->next, i++)
		Bprint(&out, "%*s | ", -widths[i], f->name);
	Bprint(&out, "\n");

#ifdef SHOW_TYPES
	Bprint(&out, "| ");
	for(i = 0, f = res->fields; f; f = f->next, i++)
		Bprint(&out, "%*s | ", -widths[i], fldtype(f->type, f->flags));
	Bprint(&out, "\n");
#endif

	for(i = 0; i < res->nf; i++)
		for(j = 0; j < widths[i]+3; j++)
			Bprint(&out, "-");
	Bprint(&out, "-\n");

	for(r = res->rows; r; r = r->next){
		Bprint(&out, "| ");
		for(i = 0, c = r->cols; c; c = c->next, i++)
			Bprint(&out, "%*s | ", -widths[i], c->str);
		Bprint(&out, "\n");
	}

	for(i = 0; i < res->nf; i++)
		for(j = 0; j < widths[i]+3; j++)
			Bprint(&out, "-");
	Bprint(&out, "-\n");

	free(widths);
	Bterm(&out);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].