]> granicus.if.org Git - php/commitdiff
stat before setting breakpoints on files
authorkrakjoe <joe.watkins@live.co.uk>
Sat, 16 Nov 2013 22:40:01 +0000 (22:40 +0000)
committerkrakjoe <joe.watkins@live.co.uk>
Sat, 16 Nov 2013 22:40:01 +0000 (22:40 +0000)
default switch case for command handlers
trace route through handler tree for debugging etc **selected

phpdbg_bp.c
phpdbg_break.c
phpdbg_list.c
phpdbg_print.c
phpdbg_prompt.c
phpdbg_prompt.h

index a1ce1816748ee0c8235ce87645b784eb603b5969..76d8b01764ab25ec9485625dd4675a3de142ce04 100644 (file)
@@ -45,32 +45,42 @@ static void phpdbg_class_breaks_dtor(void *data) /* {{{ */
 
 void phpdbg_set_breakpoint_file(const char *path, long line_num TSRMLS_DC) /* {{{ */
 {
-       phpdbg_breakfile_t new_break;
-       zend_llist *break_files_ptr;
-       size_t path_len = strlen(path);
+    struct stat sb;
 
-       new_break.filename = estrndup(path, path_len);
-       new_break.line = line_num;
+    if (VCWD_STAT(path, &sb) != FAILURE) {
+        if (sb.st_mode & S_IFREG|S_IFLNK) {
+            phpdbg_breakfile_t new_break;
+            zend_llist *break_files_ptr;
+            size_t path_len = strlen(path);
 
-       PHPDBG_G(flags) |= PHPDBG_HAS_FILE_BP;
+            new_break.filename = estrndup(path, path_len);
+            new_break.line = line_num;
 
-       if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],
-               new_break.filename, path_len, (void**)&break_files_ptr) == FAILURE) {
-               zend_llist break_files;
+            PHPDBG_G(flags) |= PHPDBG_HAS_FILE_BP;
 
-               zend_llist_init(&break_files, sizeof(phpdbg_breakfile_t),
-                       phpdbg_llist_breakfile_dtor, 0);
+            if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],
+               new_break.filename, path_len, (void**)&break_files_ptr) == FAILURE) {
+                zend_llist break_files;
 
-               zend_hash_update(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],
-                       new_break.filename, path_len, &break_files, sizeof(zend_llist),
-                       (void**)&break_files_ptr);
-       }
+                zend_llist_init(&break_files, sizeof(phpdbg_breakfile_t),
+                phpdbg_llist_breakfile_dtor, 0);
+
+                zend_hash_update(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],
+                    new_break.filename, path_len, &break_files, sizeof(zend_llist),
+                    (void**)&break_files_ptr);
+            }
 
-       new_break.id = PHPDBG_G(bp_count)++;
-       zend_llist_add_element(break_files_ptr, &new_break);
+            new_break.id = PHPDBG_G(bp_count)++;
+            zend_llist_add_element(break_files_ptr, &new_break);
 
-       phpdbg_notice("Breakpoint #%d added at %s:%ld",
-           new_break.id, new_break.filename, new_break.line);
+            phpdbg_notice("Breakpoint #%d added at %s:%ld",
+                new_break.id, new_break.filename, new_break.line);
+        } else {
+            phpdbg_error("Cannot set breakpoint in %s, it is not a regular file", path);
+        }
+    } else {
+        phpdbg_error("Cannot stat %s, it does not exist", path);
+    }
 } /* }}} */
 
 void phpdbg_set_breakpoint_symbol(const char *name TSRMLS_DC) /* {{{ */
index a866eb063ae8320d2328016904d8c6392112a2f7..21500436346d2bcf8455ae06b6cb1f1266473a95 100644 (file)
@@ -32,10 +32,8 @@ PHPDBG_BREAK(file) /* {{{ */
                case FILE_PARAM:
                        phpdbg_set_breakpoint_file(param->file.name, param->file.line TSRMLS_CC);
                        break;
-                       
-               default:
-                       phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(param TSRMLS_CC));
-                       break;
+
+               phpdbg_default_switch_case();
        }
 
        return SUCCESS;
@@ -47,10 +45,8 @@ PHPDBG_BREAK(method) /* {{{ */
                case METHOD_PARAM:
             phpdbg_set_breakpoint_method(param->method.class, param->method.name TSRMLS_CC);
             break;
-            
-               default:
-                       phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(param TSRMLS_CC));
-                       break;
+
+               phpdbg_default_switch_case();
     }
 
     return SUCCESS;
@@ -63,10 +59,7 @@ PHPDBG_BREAK(address) /* {{{ */
             phpdbg_set_breakpoint_opline(param->addr TSRMLS_CC);
             break;
             
-               default:
-                       phpdbg_error(
-                           "Unsupported parameter type (%s) for function", phpdbg_get_param_type(param TSRMLS_CC));
-               return FAILURE;
+               phpdbg_default_switch_case();
     }
 
     return SUCCESS;
@@ -74,32 +67,30 @@ PHPDBG_BREAK(address) /* {{{ */
 
 PHPDBG_BREAK(on) /* {{{ */
 {
-       if (param->type == STR_PARAM) {
-        phpdbg_set_breakpoint_expression(param->str, param->len TSRMLS_CC);
-       } else {
-           phpdbg_error(
-               "Unsupported parameter type (%s) for command", phpdbg_get_param_type(param TSRMLS_CC));
-           return FAILURE;
-       }
+    switch (param->type) {
+        case STR_PARAM:
+            phpdbg_set_breakpoint_expression(param->str, param->len TSRMLS_CC);
+        break;
+
+        phpdbg_default_switch_case();
+    }
+
        return SUCCESS;
 } /* }}} */
 
 PHPDBG_BREAK(lineno) /* {{{ */
 {
-       if (!PHPDBG_G(exec)) {
-               phpdbg_error("Not file context found!");
-               return SUCCESS;
-       }
-
        switch (param->type) {
-               case NUMERIC_PARAM:
-                       phpdbg_set_breakpoint_file(phpdbg_current_file(TSRMLS_C), param->num TSRMLS_CC);
-                       break;
-                       
-               default:
-                       phpdbg_error(
-                           "Unsupported parameter type (%s) for function", phpdbg_get_param_type(param TSRMLS_CC));
-                       return FAILURE;
+               case NUMERIC_PARAM: {
+                   if (PHPDBG_G(exec)) {
+                       phpdbg_set_breakpoint_file(phpdbg_current_file(TSRMLS_C), param->num TSRMLS_CC);
+                   } else {
+                       phpdbg_error(
+                           "Execution context not set !");
+                   }
+               } break;
+
+               phpdbg_default_switch_case();
        }
 
        return SUCCESS;
@@ -111,10 +102,8 @@ PHPDBG_BREAK(func) /* {{{ */
                case STR_PARAM:
                        phpdbg_set_breakpoint_symbol(param->str TSRMLS_CC);
                        break;
-                       
-               default:
-                       phpdbg_error("Unsupported parameter type (%s) for function", phpdbg_get_param_type(param TSRMLS_CC));
-                       break;
+
+               phpdbg_default_switch_case();
        }
 
     return SUCCESS;
index 78ec2fc75a467f6c275f6cf6a7e4fd61f9204074..04f137dcd5d7016f5ec14f1d338ee495feb42a3d 100644 (file)
@@ -203,12 +203,13 @@ void phpdbg_list_file(const char *filename, long count, long offset TSRMLS_DC) /
        int all_content = (count == 0);
        unsigned int line = 0, displayed = 0;
 
-    if (VCWD_STAT(filename, &st) == -1) {
+    if (VCWD_STAT(filename, &st) == FAILURE) {
                phpdbg_error("Failed to stat file %s", filename);
                return;
        }
+       
 #ifndef _WIN32
-       if ((fd = VCWD_OPEN(filename, O_RDONLY)) == -1) {
+       if ((fd = VCWD_OPEN(filename, O_RDONLY)) == FAILURE) {
                phpdbg_error("Failed to open file %s to list", filename);
                return;
        }
index 92609089998636b83d79ca6902868eec7b17d791..adfc06cb8564423c6f97159caad0ac2cdd8e7243 100644 (file)
@@ -117,12 +117,10 @@ PHPDBG_PRINT(class) /* {{{ */
            } else {
                phpdbg_error(
                    "Cannot find class %s", param->str);
-               return FAILURE;
            }
        } else {
                phpdbg_error(
                    "Unsupported parameter type (%s) for command", phpdbg_get_param_type(param TSRMLS_CC));
-               return FAILURE;
        }
 
        return SUCCESS;
index 1361cc70f4ea6d9d96bf3f0c3d21270baf73bad9..91caf4525ee659a3f84b29adcc416c02262fdb8b 100644 (file)
@@ -101,6 +101,8 @@ void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TS
             zend_bool in_code = 0;
 
             while (fgets(cmd, PHPDBG_MAX_CMD, fp) != NULL) {
+                phpdbg_command_t *selected = NULL;
+                
                 cmd_len = strlen(cmd)-1;
 
                        while (*cmd && isspace(cmd[cmd_len-1]))
@@ -141,7 +143,7 @@ void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TS
                                goto next_line;
                            }
 
-                           switch (phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC)) {
+                           switch (phpdbg_do_cmd(phpdbg_prompt_commands, &selected, cmd, cmd_len TSRMLS_CC)) {
                            case FAILURE:
                                phpdbg_error(
                                    "Unrecognized command in %s:%d: %s!", init_file, line, cmd);
@@ -691,7 +693,9 @@ static PHPDBG_COMMAND(list) /* {{{ */
        return SUCCESS;
 } /* }}} */
 
-int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_len TSRMLS_DC) /* {{{ */
+int phpdbg_do_cmd(  const phpdbg_command_t *command, 
+                    phpdbg_command_t **selected, 
+                    char *cmd_line, size_t cmd_len TSRMLS_DC) /* {{{ */
 {
     int rc = FAILURE;
     
@@ -703,12 +707,14 @@ int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_le
 #endif
        size_t expr_len = (cmd != NULL) ? strlen(cmd) : 0;
 
+    phpdbg_param_t *param = NULL;
+    
        while (command && command->name && command->handler) {
                if ((command->name_len == expr_len
                            && memcmp(cmd, command->name, expr_len) == 0)
                   || ((expr_len == 1) && (command->alias && command->alias == cmd_line[0]))) {
 
-                   phpdbg_param_t *param = emalloc(sizeof(phpdbg_param_t));
+                   param = emalloc(sizeof(phpdbg_param_t));
 
                        PHPDBG_G(last) = (phpdbg_command_t*) command;
 
@@ -727,20 +733,30 @@ int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_le
             PHPDBG_G(lparam) = param;
 
             if (command->subs && (param->type == STR_PARAM)) {
-                if (phpdbg_do_cmd(command->subs, param->str, param->len TSRMLS_CC) == SUCCESS) {
+                if (phpdbg_do_cmd(command->subs, selected, param->str, param->len TSRMLS_CC) == SUCCESS) {
                     rc = SUCCESS;
-                    break;
+                    /* because we can */
+                    phpdbg_clear_param(param TSRMLS_CC);
+                    efree(param);
+                    goto done;
                 }
             }
 
-            phpdbg_debug("phpdbg_do_cmd(%s, \"%s\")",
-                        command->name, phpdbg_get_param_type(param TSRMLS_CC));
-
+            *selected = command;
+            
                        rc = command->handler(param TSRMLS_CC);
-                       break;  
-               }
-               ++command;
-       }
+                       
+            break;     
+        }
+        ++command;
+    }
+    
+done:
+    if (selected && param) {
+        phpdbg_debug(
+            "phpdbg_do_cmd(%s, \"%s\"): %d",
+            command->name, phpdbg_get_param_type(param TSRMLS_CC), (rc==SUCCESS));
+    }
 
        return rc;
 } /* }}} */
@@ -773,11 +789,14 @@ int phpdbg_interactive(TSRMLS_D) /* {{{ */
                cmd[cmd_len] = '\0';
 
                if (*cmd && cmd_len > 0L) {
+                   /* keep a cursor to selected command */
+                   phpdbg_command_t *selected = NULL;
+                   
 #ifdef HAVE_LIBREADLINE
             add_history(cmd);
 #endif
-
-                   switch (ret = phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC)) {
+            
+                   switch (ret = phpdbg_do_cmd(phpdbg_prompt_commands, &selected, cmd, cmd_len TSRMLS_CC)) {
                        case FAILURE:
                            if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
                                                phpdbg_error("Failed to execute %s!", cmd);
index 3781d833175e9a077d584a4408de12dd908f485e..8e4a0c144c97fd755ac4f9591014b2f4349d9558 100644 (file)
@@ -33,7 +33,7 @@
 /**
  * Command Executor
  */
-int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_len TSRMLS_DC);
+int phpdbg_do_cmd(const phpdbg_command_t *command, phpdbg_command_t **selected, char *cmd_line, size_t cmd_len TSRMLS_DC);
 
 /**
  * Command Declarators
@@ -59,4 +59,11 @@ void phpdbg_execute_ex(zend_execute_data *execute_data TSRMLS_DC);
 void phpdbg_execute_ex(zend_op_array *op_array TSRMLS_DC);
 #endif
 
+#define phpdbg_default_switch_case() \
+    default:\
+        phpdbg_error(\
+            "Unsupported parameter type (%s) for command", \
+                phpdbg_get_param_type(param TSRMLS_CC)); \
+    break
+
 #endif /* PHPDBG_PROMPT_H */