PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/mysqlnd - mysqlnd_bt.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 95 0.0 %
Date: 2014-08-04 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 5                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2006-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Georg Richter <georg@mysql.com>                             |
      16             :   |          Andrey Hristov <andrey@mysql.com>                           |
      17             :   |          Ulf Wendel <uwendel@mysql.com>                              |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id: mysqlnd_debug.c 309303 2011-03-16 12:42:59Z andrey $ */
      22             : 
      23             : #include "php.h"
      24             : #include "Zend/zend_builtin_functions.h"
      25             : 
      26             : /* Follows code borrowed from zend_builtin_functions.c because the functions there are static */
      27             : 
      28             : /* {{{ gettraceasstring() macros */
      29             : #define TRACE_APPEND_CHR(chr)                                            \
      30             :         *str = (char*)erealloc(*str, *len + 1 + 1);                          \
      31             :         (*str)[(*len)++] = chr
      32             : 
      33             : #define TRACE_APPEND_STRL(val, vallen)                                   \
      34             :         {                                                                    \
      35             :                 int l = vallen;                                                  \
      36             :                 *str = (char*)erealloc(*str, *len + l + 1);                      \
      37             :                 memcpy((*str) + *len, val, l);                                   \
      38             :                 *len += l;                                                       \
      39             :         }
      40             : 
      41             : #define TRACE_APPEND_STR(val)                                            \
      42             :         TRACE_APPEND_STRL(val, sizeof(val)-1)
      43             : 
      44             : #define TRACE_APPEND_KEY(key)                                            \
      45             :         if (zend_hash_find(ht, key, sizeof(key), (void**)&tmp) == SUCCESS) { \
      46             :             TRACE_APPEND_STRL(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));           \
      47             :         }
      48             : 
      49             : /* }}} */
      50             : 
      51             : 
      52             : static int
      53           0 : mysqlnd_build_trace_args(zval **arg TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
      54             : {
      55             :         char **str;
      56             :         int *len;
      57             : 
      58           0 :         str = va_arg(args, char**);
      59           0 :         len = va_arg(args, int*);
      60             : 
      61             :         /* the trivial way would be to do:
      62             :          * conver_to_string_ex(arg);
      63             :          * append it and kill the now tmp arg.
      64             :          * but that could cause some E_NOTICE and also damn long lines.
      65             :          */
      66             : 
      67           0 :         switch (Z_TYPE_PP(arg)) {
      68             :                 case IS_NULL:
      69           0 :                         TRACE_APPEND_STR("NULL, ");
      70           0 :                         break;
      71             :                 case IS_STRING: {
      72             :                         int l_added;
      73           0 :                         TRACE_APPEND_CHR('\'');
      74           0 :                         if (Z_STRLEN_PP(arg) > 15) {
      75           0 :                                 TRACE_APPEND_STRL(Z_STRVAL_PP(arg), 15);
      76           0 :                                 TRACE_APPEND_STR("...', ");
      77           0 :                                 l_added = 15 + 6 + 1; /* +1 because of while (--l_added) */
      78             :                         } else {
      79           0 :                                 l_added = Z_STRLEN_PP(arg);
      80           0 :                                 TRACE_APPEND_STRL(Z_STRVAL_PP(arg), l_added);
      81           0 :                                 TRACE_APPEND_STR("', ");
      82           0 :                                 l_added += 3 + 1;
      83             :                         }
      84           0 :                         while (--l_added) {
      85           0 :                                 if ((*str)[*len - l_added] < 32) {
      86           0 :                                         (*str)[*len - l_added] = '?';
      87             :                                 }
      88             :                         }
      89           0 :                         break;
      90             :                 }
      91             :                 case IS_BOOL:
      92           0 :                         if (Z_LVAL_PP(arg)) {
      93           0 :                                 TRACE_APPEND_STR("true, ");
      94             :                         } else {
      95           0 :                                 TRACE_APPEND_STR("false, ");
      96             :                         }
      97           0 :                         break;
      98             :                 case IS_RESOURCE:
      99           0 :                         TRACE_APPEND_STR("Resource id #");
     100             :                         /* break; */
     101             :                 case IS_LONG: {
     102           0 :                         long lval = Z_LVAL_PP(arg);
     103             :                         char s_tmp[MAX_LENGTH_OF_LONG + 1];
     104           0 :                         int l_tmp = zend_sprintf(s_tmp, "%ld", lval);  /* SAFE */
     105           0 :                         TRACE_APPEND_STRL(s_tmp, l_tmp);
     106           0 :                         TRACE_APPEND_STR(", ");
     107           0 :                         break;
     108             :                 }
     109             :                 case IS_DOUBLE: {
     110           0 :                         double dval = Z_DVAL_PP(arg);
     111             :                         char *s_tmp;
     112             :                         int l_tmp;
     113             : 
     114           0 :                         s_tmp = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
     115           0 :                         l_tmp = zend_sprintf(s_tmp, "%.*G", (int) EG(precision), dval);  /* SAFE */
     116           0 :                         TRACE_APPEND_STRL(s_tmp, l_tmp);
     117             :                         /* %G already handles removing trailing zeros from the fractional part, yay */
     118           0 :                         efree(s_tmp);
     119           0 :                         TRACE_APPEND_STR(", ");
     120           0 :                         break;
     121             :                 }
     122             :                 case IS_ARRAY:
     123           0 :                         TRACE_APPEND_STR("Array, ");
     124           0 :                         break;
     125             :                 case IS_OBJECT: {
     126             :                         char *class_name;
     127             :                         zend_uint class_name_len;
     128             :                         int dupl;
     129             : 
     130           0 :                         TRACE_APPEND_STR("Object(");
     131             : 
     132           0 :                         dupl = zend_get_object_classname(*arg, (const char **)&class_name, &class_name_len TSRMLS_CC);
     133             : 
     134           0 :                         TRACE_APPEND_STRL(class_name, class_name_len);
     135           0 :                         if (!dupl) {
     136           0 :                                 efree(class_name);
     137             :                         }
     138             : 
     139           0 :                         TRACE_APPEND_STR("), ");
     140             :                         break;
     141             :                 }
     142             :                 default:
     143             :                         break;
     144             :         }
     145           0 :         return ZEND_HASH_APPLY_KEEP;
     146             : }
     147             : /* }}} */
     148             : 
     149             : static int
     150           0 : mysqlnd_build_trace_string(zval **frame TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
     151             : {
     152             :         char *s_tmp, **str;
     153             :         int *len, *num;
     154             :         long line;
     155           0 :         HashTable *ht = Z_ARRVAL_PP(frame);
     156             :         zval **file, **tmp;
     157             :         uint * level;
     158             : 
     159           0 :         level = va_arg(args, uint *);
     160           0 :         str = va_arg(args, char**);
     161           0 :         len = va_arg(args, int*);
     162           0 :         num = va_arg(args, int*);
     163             : 
     164           0 :         if (!*level) {
     165           0 :                 return ZEND_HASH_APPLY_KEEP;
     166             :         }
     167           0 :         --*level;
     168             : 
     169           0 :         s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 1 + 1);
     170           0 :         sprintf(s_tmp, "#%d ", (*num)++);
     171           0 :         TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     172           0 :         efree(s_tmp);
     173           0 :         if (zend_hash_find(ht, "file", sizeof("file"), (void**)&file) == SUCCESS) {
     174           0 :                 if (zend_hash_find(ht, "line", sizeof("line"), (void**)&tmp) == SUCCESS) {
     175           0 :                         line = Z_LVAL_PP(tmp);
     176             :                 } else {
     177           0 :                         line = 0;
     178             :                 }
     179           0 :                 s_tmp = emalloc(Z_STRLEN_PP(file) + MAX_LENGTH_OF_LONG + 4 + 1);
     180           0 :                 sprintf(s_tmp, "%s(%ld): ", Z_STRVAL_PP(file), line);
     181           0 :                 TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     182           0 :                 efree(s_tmp);
     183             :         } else {
     184           0 :                 TRACE_APPEND_STR("[internal function]: ");
     185             :         }
     186           0 :         TRACE_APPEND_KEY("class");
     187           0 :         TRACE_APPEND_KEY("type");
     188           0 :         TRACE_APPEND_KEY("function");
     189           0 :         TRACE_APPEND_CHR('(');
     190           0 :         if (zend_hash_find(ht, "args", sizeof("args"), (void**)&tmp) == SUCCESS) {
     191           0 :                 int last_len = *len;
     192           0 :                 zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp) TSRMLS_CC, (apply_func_args_t)mysqlnd_build_trace_args, 2, str, len);
     193           0 :                 if (last_len != *len) {
     194           0 :                         *len -= 2; /* remove last ', ' */
     195             :                 }
     196             :         }
     197           0 :         TRACE_APPEND_STR(")\n");
     198           0 :         return ZEND_HASH_APPLY_KEEP;
     199             : }
     200             : /* }}} */
     201             : 
     202             : 
     203             : PHPAPI char *
     204           0 : mysqlnd_get_backtrace(uint max_levels, size_t * length TSRMLS_DC)
     205             : {
     206             :         zval *trace;
     207           0 :         char *res = estrdup(""), **str = &res, *s_tmp;
     208           0 :         int res_len = 0, *len = &res_len, num = 0;
     209           0 :         if (max_levels == 0) {
     210           0 :                 max_levels = 99999;
     211             :         }
     212             : 
     213           0 :         MAKE_STD_ZVAL(trace);
     214           0 :         zend_fetch_debug_backtrace(trace, 0, 0, 0 TSRMLS_CC);
     215             : 
     216           0 :         zend_hash_apply_with_arguments(Z_ARRVAL_P(trace) TSRMLS_CC, (apply_func_args_t)mysqlnd_build_trace_string, 4, &max_levels, str, len, &num);
     217           0 :         zval_ptr_dtor(&trace);
     218             : 
     219           0 :         if (max_levels) {
     220           0 :                 s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 7 + 1);
     221           0 :                 sprintf(s_tmp, "#%d {main}", num);
     222           0 :                 TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     223           0 :                 efree(s_tmp);
     224             :         }
     225             : 
     226           0 :         res[res_len] = '\0';
     227           0 :         *length = res_len;
     228             : 
     229           0 :         return res;
     230             : }
     231             : 
     232             : 
     233             : /*
     234             :  * Local variables:
     235             :  * tab-width: 4
     236             :  * c-basic-offset: 4
     237             :  * End:
     238             :  * vim600: noet sw=4 ts=4 fdm=marker
     239             :  * vim<600: noet sw=4 ts=4
     240             :  */

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:07 +0000 (17 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.