[njs] Tests: introduced OPCODE debug.

Dmitry Volyntsev xeioex at nginx.com
Tue Apr 5 03:02:29 UTC 2022


details:   https://hg.nginx.org/njs/rev/6a5ec4a275a6
branches:  
changeset: 1832:6a5ec4a275a6
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Feb 22 19:38:59 2022 +0000
description:
Tests: introduced OPCODE debug.

diffstat:

 src/njs_disassembler.c |  18 ++++++------------
 src/njs_error.c        |  21 ++++++++-------------
 src/njs_generator.c    |  26 ++++++++++++++++++++++----
 src/njs_generator.h    |   3 ++-
 src/njs_vm.h           |   3 +++
 src/njs_vmcode.c       |  50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 91 insertions(+), 30 deletions(-)

diffs (281 lines):

diff -r 805c1c96a2d2 -r 6a5ec4a275a6 src/njs_disassembler.c
--- a/src/njs_disassembler.c	Mon Feb 21 16:53:16 2022 +0000
+++ b/src/njs_disassembler.c	Tue Feb 22 19:38:59 2022 +0000
@@ -15,9 +15,6 @@ typedef struct {
 } njs_code_name_t;
 
 
-static void njs_disassemble(njs_vm_code_t *code);
-
-
 static njs_code_name_t  code_names[] = {
 
     { NJS_VMCODE_OBJECT, sizeof(njs_vmcode_object_t),
@@ -173,7 +170,7 @@ njs_disassembler(njs_vm_t *vm)
 
     while (n != 0) {
         njs_printf("%V:%V\n", &code->file, &code->name);
-        njs_disassemble(code);
+        njs_disassemble(code->start, code->end, -1, code->lines);
         code++;
         n--;
     }
@@ -182,10 +179,10 @@ njs_disassembler(njs_vm_t *vm)
 }
 
 
-static void
-njs_disassemble(njs_vm_code_t *code)
+void
+njs_disassemble(u_char *start, u_char *end, njs_int_t count, njs_arr_t *lines)
 {
-    u_char                       *p, *start, *end;
+    u_char                       *p;
     uint32_t                     line;
     njs_str_t                    *name;
     njs_uint_t                   n;
@@ -215,9 +212,6 @@ njs_disassemble(njs_vm_code_t *code)
     njs_vmcode_try_trampoline_t  *try_tramp;
     njs_vmcode_function_frame_t  *function;
 
-    start = code->start;
-    end = code->end;
-
     /*
      * On some 32-bit platform uintptr_t is int and compilers warn
      * about %l format modifier.  size_t has the size as pointer so
@@ -226,9 +220,9 @@ njs_disassemble(njs_vm_code_t *code)
 
     p = start;
 
-    while (p < end) {
+    while (((p < end) && (count == -1)) || (count-- > 0)) {
         operation = *(njs_vmcode_operation_t *) p;
-        line = njs_lookup_line(code, p - start);
+        line = njs_lookup_line(lines, p - start);
 
         if (operation == NJS_VMCODE_ARRAY) {
             array = (njs_vmcode_array_t *) p;
diff -r 805c1c96a2d2 -r 6a5ec4a275a6 src/njs_error.c
--- a/src/njs_error.c	Mon Feb 21 16:53:16 2022 +0000
+++ b/src/njs_error.c	Tue Feb 22 19:38:59 2022 +0000
@@ -1286,7 +1286,6 @@ njs_add_backtrace_entry(njs_vm_t *vm, nj
     njs_native_frame_t *native_frame)
 {
     njs_int_t              ret;
-    njs_uint_t             i;
     njs_vm_code_t          *code;
     njs_function_t         *function;
     njs_backtrace_entry_t  *be;
@@ -1316,20 +1315,16 @@ njs_add_backtrace_entry(njs_vm_t *vm, nj
         return NJS_OK;
     }
 
-    code = vm->codes->start;
+    code = njs_lookup_code(vm, native_frame->pc);
 
-    for (i = 0; i < vm->codes->items; i++, code++) {
-        if (code->start <= native_frame->pc
-            && native_frame->pc < code->end)
-        {
-            be->name = code->name;
-            be->line = njs_lookup_line(code, native_frame->pc - code->start);
-            if (!vm->options.quiet) {
-                be->file = code->file;
-            }
+    if (code != NULL) {
+        be->name = code->name;
+        be->line = njs_lookup_line(code->lines, native_frame->pc - code->start);
+        if (!vm->options.quiet) {
+            be->file = code->file;
+        }
 
-            return NJS_OK;
-        }
+        return NJS_OK;
     }
 
     be->name = njs_entry_unknown;
diff -r 805c1c96a2d2 -r 6a5ec4a275a6 src/njs_generator.c
--- a/src/njs_generator.c	Mon Feb 21 16:53:16 2022 +0000
+++ b/src/njs_generator.c	Tue Feb 22 19:38:59 2022 +0000
@@ -808,8 +808,26 @@ njs_generate_code_map(njs_vm_t *vm, njs_
 }
 
 
+njs_vm_code_t *
+njs_lookup_code(njs_vm_t *vm, u_char *pc)
+{
+    njs_uint_t     i;
+    njs_vm_code_t  *code;
+
+    code = vm->codes->start;
+
+    for (i = 0; i < vm->codes->items; i++, code++) {
+        if (code->start <= pc && pc < code->end) {
+            return code;
+        }
+    }
+
+    return NULL;
+}
+
+
 uint32_t
-njs_lookup_line(njs_vm_code_t *code, uint32_t offset)
+njs_lookup_line(njs_arr_t *lines, uint32_t offset)
 {
     njs_uint_t         n;
     njs_vm_line_num_t  *map;
@@ -817,9 +835,9 @@ njs_lookup_line(njs_vm_code_t *code, uin
     n = 0;
     map = NULL;
 
-    if (code->lines != NULL) {
-        n = code->lines->items;
-        map = (njs_vm_line_num_t *) code->lines->start;
+    if (lines != NULL) {
+        n = lines->items;
+        map = (njs_vm_line_num_t *) lines->start;
     }
 
     while (n != 0) {
diff -r 805c1c96a2d2 -r 6a5ec4a275a6 src/njs_generator.h
--- a/src/njs_generator.h	Mon Feb 21 16:53:16 2022 +0000
+++ b/src/njs_generator.h	Tue Feb 22 19:38:59 2022 +0000
@@ -45,7 +45,8 @@ njs_int_t njs_generator_init(njs_generat
     njs_int_t depth, njs_bool_t runtime);
 njs_vm_code_t *njs_generate_scope(njs_vm_t *vm, njs_generator_t *generator,
     njs_parser_scope_t *scope, const njs_str_t *name);
-uint32_t njs_lookup_line(njs_vm_code_t *code, uint32_t offset);
+njs_vm_code_t *njs_lookup_code(njs_vm_t *vm, u_char *pc);
+uint32_t njs_lookup_line(njs_arr_t *lines, uint32_t offset);
 
 
 #endif /* _NJS_GENERATOR_H_INCLUDED_ */
diff -r 805c1c96a2d2 -r 6a5ec4a275a6 src/njs_vm.h
--- a/src/njs_vm.h	Mon Feb 21 16:53:16 2022 +0000
+++ b/src/njs_vm.h	Tue Feb 22 19:38:59 2022 +0000
@@ -258,6 +258,9 @@ njs_int_t njs_builtin_objects_clone(njs_
 njs_int_t njs_builtin_match_native_function(njs_vm_t *vm,
     njs_function_t *function, njs_str_t *name);
 
+void njs_disassemble(u_char *start, u_char *end, njs_int_t count,
+    njs_arr_t *lines);
+
 njs_arr_t *njs_vm_completions(njs_vm_t *vm, njs_str_t *expression);
 
 void *njs_lvlhsh_alloc(void *data, size_t size);
diff -r 805c1c96a2d2 -r 6a5ec4a275a6 src/njs_vmcode.c
--- a/src/njs_vmcode.c	Mon Feb 21 16:53:16 2022 +0000
+++ b/src/njs_vmcode.c	Tue Feb 22 19:38:59 2022 +0000
@@ -79,6 +79,11 @@ static njs_jump_off_t njs_function_frame
     } while (0)
 
 
+#ifdef NJS_OPCODE_DEBUG
+void njs_vmcode_debug(njs_vm_t *vm, u_char *pc, const char *prefix);
+#endif
+
+
 njs_int_t
 njs_vmcode_interpreter(njs_vm_t *vm, u_char *pc, void *promise_cap,
     void *async_ctx)
@@ -117,6 +122,10 @@ njs_vmcode_interpreter(njs_vm_t *vm, u_c
     njs_vmcode_try_trampoline_t  *try_trampoline;
     njs_vmcode_function_frame_t  *function_frame;
 
+#ifdef NJS_OPCODE_DEBUG
+    njs_vmcode_debug(vm, pc, "ENTER");
+#endif
+
 next:
 
     for ( ;; ) {
@@ -163,6 +172,10 @@ next:
          * as a single unsigned comparision.
          */
 
+#ifdef NJS_OPCODE_DEBUG
+        njs_disassemble(pc, NULL, 1, NULL);
+#endif
+
         if (op > NJS_VMCODE_NORET) {
 
             if (op == NJS_VMCODE_MOVE) {
@@ -650,6 +663,10 @@ next:
                 njs_vmcode_operand(vm, (njs_index_t) value2, value2);
                 vm->retval = *value2;
 
+#ifdef NJS_OPCODE_DEBUG
+                njs_vmcode_debug(vm, pc, "EXIT STOP");
+#endif
+
                 return NJS_OK;
 
             case NJS_VMCODE_JUMP:
@@ -723,6 +740,11 @@ next:
 
             case NJS_VMCODE_RETURN:
                 njs_vmcode_operand(vm, (njs_index_t) value2, value2);
+
+#ifdef NJS_OPCODE_DEBUG
+                njs_vmcode_debug(vm, pc, "EXIT RETURN");
+#endif
+
                 return njs_vmcode_return(vm, NULL, value2);
 
             case NJS_VMCODE_FUNCTION_COPY:
@@ -841,6 +863,11 @@ next:
 
             case NJS_VMCODE_AWAIT:
                 await = (njs_vmcode_await_t *) pc;
+
+#ifdef NJS_OPCODE_DEBUG
+                njs_vmcode_debug(vm, pc, "EXIT AWAIT");
+#endif
+
                 return njs_vmcode_await(vm, await, promise_cap, async_ctx);
 
             case NJS_VMCODE_TRY_START:
@@ -904,6 +931,11 @@ next:
 
                 switch (ret) {
                 case NJS_OK:
+
+#ifdef NJS_OPCODE_DEBUG
+                    njs_vmcode_debug(vm, pc, "EXIT FINALLY");
+#endif
+
                     return NJS_OK;
                 case NJS_ERROR:
                     goto error;
@@ -1018,6 +1050,10 @@ error:
         }
     }
 
+#ifdef NJS_OPCODE_DEBUG
+    njs_vmcode_debug(vm, pc, "EXIT ERROR");
+#endif
+
     return NJS_ERROR;
 }
 
@@ -2141,3 +2177,17 @@ njs_vmcode_error(njs_vm_t *vm, u_char *p
         njs_error_fmt_new(vm, &vm->retval, err->type, "%V", &err->u.message);
     }
 }
+
+
+#ifdef NJS_OPCODE_DEBUG
+void
+njs_vmcode_debug(njs_vm_t *vm, u_char *pc, const char *prefix)
+{
+    njs_vm_code_t  *code;
+
+    code = njs_lookup_code(vm, pc);
+
+    njs_printf("%s %V\n", prefix,
+               (code != NULL) ? &code->name : &njs_entry_unknown);
+}
+#endif



More information about the nginx-devel mailing list