List:Internals« Previous MessageNext Message »
From:pem Date:November 17 2005 10:16am
Subject:bk commit into 5.0 tree (pem:1.1974)
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of pem. When pem does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet
  1.1974 05/11/17 11:11:48 pem@stripped +8 -0
  Background:
  Since long, the compiled code of stored routines has been printed in the trace file
  when starting mysqld with the "--debug" flag. (At creation time only, and only in
  debug builds of course.) This has been helpful when debugging stored procedure
  execution, but it's a bit awkward to use. Also, the printing of some of the
  instructions is a bit terse, in particular for sp_instr_stmt where only the command
  code was printed.
  
  This improves the printout of several of the instructions, and adds the debugging-
  only commands "show procedure code <name>" and "show function code <name>".
  (In non-debug builds they are not available.)

  sql/sql_yacc.yy
    1.441 05/11/17 11:11:40 pem@stripped +26 -2
    Added new commands for debugging purposes:
    "show procedure code ..." and "show function code ...".
    These are only enabled in debug builds, otherwise they result in a syntax error.
    (I.e. they don't exist)

  sql/sql_parse.cc
    1.513 05/11/17 11:11:40 pem@stripped +24 -0
    Added new commands for debugging, e.g. "show procedure code".

  sql/sql_lex.h
    1.208 05/11/17 11:11:40 pem@stripped +1 -0
    Added new sql_command codes for debugging.

  sql/sp_pcontext.h
    1.22 05/11/17 11:11:40 pem@stripped +5 -10
    Moved find_pvar(uint i) method to sp_pcontext.cc.
    Added a similar find_cursor(uint i, ...) method, for debugging.

  sql/sp_pcontext.cc
    1.25 05/11/17 11:11:40 pem@stripped +42 -0
    Moved find_pvar(uint i) method from sp_pcontext.h, and made it work for all
    frames, not just the first one. (For debugging purposes)
    Added a similar find_cursor(uint i, ...) method, for debugging.

  sql/sp_head.h
    1.75 05/11/17 11:11:40 pem@stripped +9 -2
    New debugging-only method in sp_head: show_routine_code().
    Added offset member to sp_instr_cpush for improved debug printing.

  sql/sp_head.cc
    1.195 05/11/17 11:11:40 pem@stripped +144 -25
    Fixed some minor debug-mode bugs in show_create_*().
    New method for debugging: sp_head::show_routine_code() - returns the "assembly code"
    for a stored routine as a result set.
    Improved the print() methods for many sp_instr* classes, particularly for
    sp_instr_stmt where the query string is printed as well (up to a max length, just
    to give a hint of which statement it is). Also print the names of variables and
    cursors in some instruction.

  sql/lex.h
    1.142 05/11/17 11:11:39 pem@stripped +1 -0
    New symbol for debug-only command (e.g. show procedure code).

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	pem
# Host:	mysql.comhem.se
# Root:	/usr/home/pem/show-sp-code/mysql-5.0

--- 1.141/sql/lex.h	2005-08-25 15:39:00 +02:00
+++ 1.142/sql/lex.h	2005-11-17 11:11:39 +01:00
@@ -110,6 +110,7 @@
   { "CIPHER",		SYM(CIPHER_SYM)},
   { "CLIENT",		SYM(CLIENT_SYM)},
   { "CLOSE",		SYM(CLOSE_SYM)},
+  { "CODE",             SYM(CODE_SYM)},
   { "COLLATE",		SYM(COLLATE_SYM)},
   { "COLLATION",	SYM(COLLATION_SYM)},
   { "COLUMN",		SYM(COLUMN_SYM)},

--- 1.207/sql/sql_lex.h	2005-11-10 20:24:53 +01:00
+++ 1.208/sql/sql_lex.h	2005-11-17 11:11:40 +01:00
@@ -90,6 +90,7 @@
   SQLCOM_CREATE_TRIGGER, SQLCOM_DROP_TRIGGER,
   SQLCOM_XA_START, SQLCOM_XA_END, SQLCOM_XA_PREPARE,
   SQLCOM_XA_COMMIT, SQLCOM_XA_ROLLBACK, SQLCOM_XA_RECOVER,
+  SQLCOM_SHOW_PROC_CODE, SQLCOM_SHOW_FUNC_CODE,
   /* This should be the last !!! */
 
   SQLCOM_END

--- 1.512/sql/sql_parse.cc	2005-11-14 15:41:59 +01:00
+++ 1.513/sql/sql_parse.cc	2005-11-17 11:11:40 +01:00
@@ -4560,6 +4560,30 @@
 					 lex->wild->ptr() : NullS));
       break;
     }
+#ifndef DBUG_OFF
+  case SQLCOM_SHOW_PROC_CODE:
+  case SQLCOM_SHOW_FUNC_CODE:
+    {
+      sp_head *sp;
+
+      if (lex->spname->m_name.length > NAME_LEN)
+      {
+	my_error(ER_TOO_LONG_IDENT, MYF(0), lex->spname->m_name.str);
+	goto error;
+      }
+      if (lex->sql_command == SQLCOM_SHOW_PROC_CODE)
+        sp= sp_find_procedure(thd, lex->spname);
+      else
+        sp= sp_find_function(thd, lex->spname);
+      if (!sp || !sp->show_routine_code(thd))
+      {                       /* We don't distinguish between errors for now */
+        my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
+                 SP_COM_STRING(lex), lex->spname->m_name.str);
+        goto error;
+      }
+      break;
+    }
+#endif // ifndef DBUG_OFF
   case SQLCOM_CREATE_VIEW:
     {
       if (end_active_trans(thd))

--- 1.440/sql/sql_yacc.yy	2005-11-10 20:24:54 +01:00
+++ 1.441/sql/sql_yacc.yy	2005-11-17 11:11:40 +01:00
@@ -175,6 +175,7 @@
 %token  CLIENT_SYM
 %token  CLOSE_SYM
 %token  COALESCE
+%token  CODE_SYM
 %token  COLLATE_SYM
 %token  COLLATION_SYM
 %token  COLUMNS
@@ -1698,7 +1699,8 @@
 	      delete $5;
 	      YYABORT;
 	    }
-            i= new sp_instr_cpush(sp->instructions(), ctx, $5);
+            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
+                                  ctx->current_cursors());
 	    sp->add_instr(i);
 	    ctx->push_cursor(&$2);
 	    $$.vars= $$.conds= $$.hndlrs= 0;
@@ -6625,7 +6627,28 @@
 	      YYABORT;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
               YYABORT;
-	  };
+	  }
+        | PROCEDURE CODE_SYM sp_name
+          {
+#ifdef DBUG_OFF
+            yyerror(ER(ER_SYNTAX_ERROR));
+            YYABORT;
+#else
+            Lex->sql_command= SQLCOM_SHOW_PROC_CODE;
+	    Lex->spname= $3;
+#endif
+          }
+        | FUNCTION_SYM CODE_SYM sp_name
+          {
+#ifdef DBUG_OFF
+            yyerror(ER(ER_SYNTAX_ERROR));
+            YYABORT;
+#else
+            Lex->sql_command= SQLCOM_SHOW_FUNC_CODE;
+	    Lex->spname= $3;
+#endif
+          }
+        ;
 
 show_engine_param:
 	STATUS_SYM
@@ -7534,6 +7557,7 @@
 	| CHANGED		{}
 	| CIPHER_SYM		{}
 	| CLIENT_SYM		{}
+        | CODE_SYM              {}
 	| COLLATION_SYM		{}
         | COLUMNS               {}
 	| COMMITTED_SYM		{}

--- 1.194/sql/sp_head.cc	2005-11-10 20:24:52 +01:00
+++ 1.195/sql/sp_head.cc	2005-11-17 11:11:40 +01:00
@@ -105,6 +105,8 @@
   case SQLCOM_SHOW_TABLES:
   case SQLCOM_SHOW_VARIABLES:
   case SQLCOM_SHOW_WARNS:
+  case SQLCOM_SHOW_PROC_CODE:
+  case SQLCOM_SHOW_FUNC_CODE:
     flags= sp_head::MULTI_RESULTS;
     break;
   /*
@@ -1698,7 +1700,7 @@
   LINT_INIT(sql_mode_len);
 
   if (check_show_routine_access(thd, this, &full_access))
-    return 1;
+    DBUG_RETURN(1);
 
   sql_mode_str=
     sys_var_thd_sql_mode::symbolic_mode_representation(thd,
@@ -1711,10 +1713,7 @@
 					     max(buffer.length(), 1024)));
   if (protocol->send_fields(&field_list, Protocol::SEND_NUM_ROWS |
                                          Protocol::SEND_EOF))
-  {
-    res= 1;
-    goto done;
-  }
+    DBUG_RETURN(1);
   protocol->prepare_for_resend();
   protocol->store(m_name.str, m_name.length, system_charset_info);
   protocol->store((char*) sql_mode_str, sql_mode_len, system_charset_info);
@@ -1723,7 +1722,6 @@
   res= protocol->write();
   send_eof(thd);
 
- done:
   DBUG_RETURN(res);
 }
 
@@ -1768,7 +1766,7 @@
   LINT_INIT(sql_mode_len);
 
   if (check_show_routine_access(thd, this, &full_access))
-    return 1;
+    DBUG_RETURN(1);
 
   sql_mode_str=
     sys_var_thd_sql_mode::symbolic_mode_representation(thd,
@@ -1780,10 +1778,7 @@
 					     max(buffer.length(),1024)));
   if (protocol->send_fields(&field_list,
                             Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
-  {
-    res= 1;
-    goto done;
-  }
+    DBUG_RETURN(1);
   protocol->prepare_for_resend();
   protocol->store(m_name.str, m_name.length, system_charset_info);
   protocol->store((char*) sql_mode_str, sql_mode_len, system_charset_info);
@@ -1792,7 +1787,6 @@
   res= protocol->write();
   send_eof(thd);
 
- done:
   DBUG_RETURN(res);
 }
 
@@ -1852,6 +1846,51 @@
 }
 
 
+#ifndef DBUG_OFF
+int
+sp_head::show_routine_code(THD *thd)
+{
+  Protocol *protocol= thd->protocol;
+  char buff[2048];
+  String buffer(buff, sizeof(buff), system_charset_info);
+  int res;
+  List<Item> field_list;
+  bool full_access;
+  uint ip;
+  sp_instr *i;
+
+  DBUG_ENTER("sp_head::show_routine_code");
+  DBUG_PRINT("info", ("procedure %s", m_name.str));
+
+  if (check_show_routine_access(thd, this, &full_access) || !full_access)
+    DBUG_RETURN(1);
+
+  field_list.push_back(new Item_uint("Pos", 9));
+  // 1024 is for not to confuse old clients
+  field_list.push_back(new Item_empty_string("Instruction",
+					     max(buffer.length(), 1024)));
+  if (protocol->send_fields(&field_list, Protocol::SEND_NUM_ROWS |
+                                         Protocol::SEND_EOF))
+    DBUG_RETURN(1);
+
+  for (ip= 0; (i = get_instr(ip)) ; ip++)
+  {
+    protocol->prepare_for_resend();
+    protocol->store((longlong)ip);
+
+    buffer.set("", 0, system_charset_info);
+    i->print(&buffer);
+    protocol->store(buffer.c_ptr_quick(), buffer.length(), system_charset_info);
+    if ((res= protocol->write()))
+      break;
+  }
+  send_eof(thd);
+
+  DBUG_RETURN(res);
+}
+#endif // ifndef DBUG_OFF
+
+
 /*
   Prepare LEX and thread for execution of instruction, if requested open
   and lock LEX's tables, execute instruction's core function, perform
@@ -2010,14 +2049,34 @@
   DBUG_RETURN(res);
 }
 
+#define STMT_PRINT_MAXLEN 40
 void
 sp_instr_stmt::print(String *str)
 {
-  str->reserve(12);
+  uint i, len;
+
+  str->reserve(STMT_PRINT_MAXLEN+20);
   str->append("stmt ");
   str->qs_append((uint)m_lex_keeper.sql_command());
+  str->append(" \"");
+  len= m_query.length;
+  /*
+    Print the query string (but not too much of it), just to indicate which
+    statement it is.
+  */
+  if (len > STMT_PRINT_MAXLEN)
+    len= STMT_PRINT_MAXLEN-3;
+  /* Copy the query string and replace '\n' with ' ' in the process */
+  for (i= 0 ; i < len ; i++)
+    if (m_query.str[i] == '\n')
+      str->append(' ');
+    else
+      str->append(m_query.str[i]);
+  if (m_query.length > STMT_PRINT_MAXLEN)
+    str->append("...");        /* Indicate truncated string */
+  str->append('"');
 }
-
+#undef STMT_PRINT_MAXLEN
 
 int
 sp_instr_stmt::exec_core(THD *thd, uint *nextp)
@@ -2054,8 +2113,19 @@
 void
 sp_instr_set::print(String *str)
 {
-  str->reserve(12);
+  int rsrv = 16;
+  sp_pvar_t *var = m_ctx->find_pvar(m_offset);
+
+  /* 'var' should always be non-null, but just in case... */
+  if (var)
+    rsrv+= var->name.length;
+  str->reserve(rsrv);
   str->append("set ");
+  if (var)
+  {
+    str->append(var->name.str, var->name.length);
+    str->append('@');
+  }
   str->qs_append(m_offset);
   str->append(' ');
   m_value->print(str);
@@ -2346,12 +2416,26 @@
   str->reserve(32);
   str->append("hpush_jump ");
   str->qs_append(m_dest);
-  str->append(" t=");
-  str->qs_append(m_type);
-  str->append(" f=");
+  str->append(' ');
   str->qs_append(m_frame);
-  str->append(" h=");
-  str->qs_append(m_ip+1);
+  switch (m_type)
+  {
+  case SP_HANDLER_NONE:
+    str->append(" NONE");       // This would be a bug
+    break;
+  case SP_HANDLER_EXIT:
+    str->append(" EXIT");
+    break;
+  case SP_HANDLER_CONTINUE:
+    str->append(" CONTINUE");
+    break;
+  case SP_HANDLER_UNDO:
+    str->append(" UNDO");
+    break;
+  default:
+    str->append(" UNKNOWN:");   // This would be a bug as well
+    str->qs_append(m_type);
+  }
 }
 
 uint
@@ -2474,7 +2558,17 @@
 void
 sp_instr_cpush::print(String *str)
 {
-  str->append("cpush");
+  LEX_STRING n;
+  my_bool found= m_ctx->find_cursor(m_cursor, &n);
+
+  str->reserve(32);
+  str->append("cpush ");
+  if (found)
+  {
+    str->append(n.str, n.length);
+    str->append('@');
+  }
+  str->qs_append(m_cursor);
 }
 
 
@@ -2570,8 +2664,16 @@
 void
 sp_instr_copen::print(String *str)
 {
-  str->reserve(12);
+  LEX_STRING n;
+  my_bool found= m_ctx->find_cursor(m_cursor, &n);
+
+  str->reserve(32);
   str->append("copen ");
+  if (found)
+  {
+    str->append(n.str, n.length);
+    str->append('@');
+  }
   str->qs_append(m_cursor);
 }
 
@@ -2599,8 +2701,16 @@
 void
 sp_instr_cclose::print(String *str)
 {
-  str->reserve(12);
+  LEX_STRING n;
+  my_bool found= m_ctx->find_cursor(m_cursor, &n);
+
+  str->reserve(32);
   str->append("cclose ");
+  if (found)
+  {
+    str->append(n.str, n.length);
+    str->append('@');
+  }
   str->qs_append(m_cursor);
 }
 
@@ -2629,14 +2739,23 @@
 {
   List_iterator_fast<struct sp_pvar> li(m_varlist);
   sp_pvar_t *pv;
+  LEX_STRING n;
+  my_bool found= m_ctx->find_cursor(m_cursor, &n);
 
-  str->reserve(12);
+  str->reserve(32);
   str->append("cfetch ");
+  if (found)
+  {
+    str->append(n.str, n.length);
+    str->append('@');
+  }
   str->qs_append(m_cursor);
   while ((pv= li++))
   {
-    str->reserve(8);
+    str->reserve(16);
     str->append(' ');
+    str->append(pv->name.str, pv->name.length);
+    str->append('@');
     str->qs_append(pv->offset);
   }
 }

--- 1.74/sql/sp_head.h	2005-11-10 20:24:52 +01:00
+++ 1.75/sql/sp_head.h	2005-11-17 11:11:40 +01:00
@@ -295,6 +295,12 @@
     return test(m_flags &
 		(CONTAINS_DYNAMIC_SQL|MULTI_RESULTS|HAS_SET_AUTOCOMMIT_STMT));
   }
+
+#ifndef DBUG_OFF
+  int show_routine_code(THD *thd);
+#endif
+
+
 private:
 
   MEM_ROOT *m_thd_root;		// Temp. store for thd's mem_root
@@ -856,8 +862,8 @@
 
 public:
 
-  sp_instr_cpush(uint ip, sp_pcontext *ctx, LEX *lex)
-    : sp_instr(ip, ctx), m_lex_keeper(lex, TRUE)
+  sp_instr_cpush(uint ip, sp_pcontext *ctx, LEX *lex, uint offset)
+    : sp_instr(ip, ctx), m_lex_keeper(lex, TRUE), m_cursor(offset)
   {}
 
   virtual ~sp_instr_cpush()
@@ -876,6 +882,7 @@
 private:
 
   sp_lex_keeper m_lex_keeper;
+  uint m_cursor;                /* Frame offset (for debugging) */
 
 }; // class sp_instr_cpush : public sp_instr
 

--- 1.24/sql/sp_pcontext.cc	2005-09-13 12:50:11 +02:00
+++ 1.25/sql/sp_pcontext.cc	2005-11-17 11:11:40 +01:00
@@ -169,6 +169,29 @@
   return NULL;
 }
 
+/*
+  Find a variable by offset from the top.
+  This used for two things:
+  - When evaluating parameters at the beginning, and setting out parameters
+    at the end, of invokation. (Top frame only, so no recursion then.)
+  - For printing of sp_instr_set. (Debug mode only.)
+ */
+sp_pvar_t *
+sp_pcontext::find_pvar(uint i)
+{
+  if (m_poffset <= i && i < m_poffset + m_pvar.elements)
+  {                           // This frame
+    sp_pvar_t *p;
+
+    get_dynamic(&m_pvar, (gptr)&p, i - m_poffset);
+    return p;
+  }
+  else if (m_parent)
+    return m_parent->find_pvar(i); // Some previous frame
+  else
+    return NULL;              // index out of bounds
+}
+
 void
 sp_pcontext::push_pvar(LEX_STRING *name, enum enum_field_types type,
 		       sp_param_mode_t mode)
@@ -331,3 +354,22 @@
     return m_parent->find_cursor(name, poff, scoped);
   return FALSE;
 }
+
+/*
+  Find a cursor by offset from the top.
+  This is only used for debugging.
+ */
+my_bool
+sp_pcontext::find_cursor(uint i, LEX_STRING *n)
+{
+  if (m_coffset <= i && i < m_coffset + m_cursor.elements)
+  {                           // This frame
+    get_dynamic(&m_cursor, (gptr)n, i - m_poffset);
+    return TRUE;
+  }
+  else if (m_parent)
+    return m_parent->find_cursor(i, n); // Some previous frame
+  else
+    return FALSE;               // index out of bounds
+}
+

--- 1.21/sql/sp_pcontext.h	2005-09-13 12:50:11 +02:00
+++ 1.22/sql/sp_pcontext.h	2005-11-17 11:11:40 +01:00
@@ -172,16 +172,7 @@
 
   // Find by index
   sp_pvar_t *
-  find_pvar(uint i)
-  {
-    sp_pvar_t *p;
-
-    if (i < m_pvar.elements)
-      get_dynamic(&m_pvar, (gptr)&p, i);
-    else
-      p= NULL;
-    return p;
-  }
+  find_pvar(uint i);
 
   //
   // Labels
@@ -260,6 +251,10 @@
 
   my_bool
   find_cursor(LEX_STRING *name, uint *poff, my_bool scoped=0);
+
+  /* Find by index (for debugging only) */
+  my_bool
+  find_cursor(uint i, LEX_STRING *n);
 
   inline uint
   max_cursors()
Thread
bk commit into 5.0 tree (pem:1.1974)pem17 Nov