Skip to content

Commit e996437

Browse files
committed
Add more optional debugging code to pcre2_match().
1 parent dfc2cd1 commit e996437

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

src/pcre2_match.c

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ POSSIBILITY OF SUCH DAMAGE.
4343
#include "config.h"
4444
#endif
4545

46+
#include "pcre2_internal.h"
47+
4648
/* These defines enable debugging code */
4749

4850
/* #define DEBUG_FRAMES_DISPLAY */
@@ -53,15 +55,17 @@ POSSIBILITY OF SUCH DAMAGE.
5355
#include <stdarg.h>
5456
#endif
5557

58+
#ifdef DEBUG_SHOW_OPS
59+
static const char *OP_names[] = { OP_NAME_LIST };
60+
#endif
61+
5662
/* These defines identify the name of the block containing "static"
5763
information, and fields within it. */
5864

5965
#define NLBLOCK mb /* Block containing newline information */
6066
#define PSSTART start_subject /* Field containing processed string start */
6167
#define PSEND end_subject /* Field containing processed string end */
6268

63-
#include "pcre2_internal.h"
64-
6569
#define RECURSE_UNSET 0xffffffffu /* Bigger than max group number */
6670

6771
/* Masks for identifying the public options that are permitted at match time. */
@@ -707,7 +711,7 @@ if ((heapframe *)((char *)N + frame_size) >= frames_top)
707711
}
708712

709713
#ifdef DEBUG_SHOW_RMATCH
710-
fprintf(stderr, "++ RMATCH %2d frame=%d", Freturn_id, Frdepth + 1);
714+
fprintf(stderr, "++ RMATCH %d frame=%d", Freturn_id, Frdepth + 1);
711715
if (group_frame_type != 0)
712716
{
713717
fprintf(stderr, " type=%x ", group_frame_type);
@@ -777,10 +781,16 @@ opcodes. */
777781
if (mb->match_call_count++ >= mb->match_limit) return PCRE2_ERROR_MATCHLIMIT;
778782
if (Frdepth >= mb->match_limit_depth) return PCRE2_ERROR_DEPTHLIMIT;
779783

784+
#ifdef DEBUG_SHOW_OPS
785+
fprintf(stderr, "\n++ New frame: type=0x%x subject offset %ld\n",
786+
GF_IDMASK(Fgroup_frame_type), Feptr - mb->start_subject);
787+
#endif
788+
780789
for (;;)
781790
{
782791
#ifdef DEBUG_SHOW_OPS
783-
fprintf(stderr, "++ op=%d\n", *Fecode);
792+
fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
793+
OP_names[*Fecode]);
784794
#endif
785795

786796
Fop = (uint8_t)(*Fecode); /* Cast needed for 16-bit and 32-bit modes */
@@ -837,13 +847,17 @@ fprintf(stderr, "++ op=%d\n", *Fecode);
837847

838848
if (Fcurrent_recurse != RECURSE_UNSET)
839849
{
850+
#ifdef DEBUG_SHOW_OPS
851+
fprintf(stderr, "++ End within recursion\n");
852+
#endif
840853
offset = Flast_group_offset;
841854
for(;;)
842855
{
843856
if (offset == PCRE2_UNSET) return PCRE2_ERROR_INTERNAL;
844857
N = (heapframe *)((char *)match_data->heapframes + offset);
845858
P = (heapframe *)((char *)N - frame_size);
846859
if (GF_IDMASK(N->group_frame_type) == GF_RECURSE) break;
860+
847861
offset = P->last_group_offset;
848862
}
849863

@@ -869,7 +883,12 @@ fprintf(stderr, "++ op=%d\n", *Fecode);
869883
((mb->moptions & PCRE2_NOTEMPTY) != 0 ||
870884
((mb->moptions & PCRE2_NOTEMPTY_ATSTART) != 0 &&
871885
Fstart_match == mb->start_subject + mb->start_offset)))
886+
{
887+
#ifdef DEBUG_SHOW_OPS
888+
fprintf(stderr, "++ Backtrack because empty string\n");
889+
#endif
872890
RRETURN(MATCH_NOMATCH);
891+
}
873892

874893
/* Fail if PCRE2_ENDANCHORED is set and the end of the match is not
875894
the end of the subject. After (*ACCEPT) we fail the entire match (at this
@@ -879,7 +898,17 @@ fprintf(stderr, "++ op=%d\n", *Fecode);
879898
if (Feptr < mb->end_subject &&
880899
((mb->moptions | mb->poptions) & PCRE2_ENDANCHORED) != 0)
881900
{
882-
if (Fop == OP_END) RRETURN(MATCH_NOMATCH);
901+
if (Fop == OP_END)
902+
{
903+
#ifdef DEBUG_SHOW_OPS
904+
fprintf(stderr, "++ Backtrack because not at end (endanchored set)\n");
905+
#endif
906+
RRETURN(MATCH_NOMATCH);
907+
}
908+
909+
#ifdef DEBUG_SHOW_OPS
910+
fprintf(stderr, "++ Failed ACCEPT not at end (endanchnored set)\n");
911+
#endif
883912
return MATCH_NOMATCH; /* (*ACCEPT) */
884913
}
885914

@@ -5842,7 +5871,6 @@ fprintf(stderr, "++ op=%d\n", *Fecode);
58425871
branch_start += GET(branch_start, 1);
58435872
branch_end = NULL;
58445873

5845-
58465874
/* Point N to the frame at the start of the most recent group, and P to its
58475875
predecessor. Remember the subject pointer at the start of the group. */
58485876

@@ -6372,7 +6400,7 @@ F = (heapframe *)((char *)F - Fback_frame); /* Backtrack */
63726400
mb->cb->callout_flags |= PCRE2_CALLOUT_BACKTRACK; /* Note for callouts */
63736401

63746402
#ifdef DEBUG_SHOW_RMATCH
6375-
fprintf(stderr, "++ RETURN %d to %d\n", rrc, Freturn_id);
6403+
fprintf(stderr, "++ RETURN %d to RM%d\n", rrc, Freturn_id);
63766404
#endif
63776405

63786406
switch (Freturn_id)
@@ -7404,9 +7432,18 @@ for(;;)
74047432
mb->match_call_count = 0;
74057433
mb->end_offset_top = 0;
74067434
mb->skip_arg_count = 0;
7435+
7436+
#ifdef DEBUG_SHOW_OPS
7437+
fprintf(stderr, "++ Calling match()\n");
7438+
#endif
7439+
74077440
rc = match(start_match, mb->start_code, re->top_bracket, frame_size,
74087441
match_data, mb);
74097442

7443+
#ifdef DEBUG_SHOW_OPS
7444+
fprintf(stderr, "++ match() returned %d\n\n", rc);
7445+
#endif
7446+
74107447
if (mb->hitend && start_partial == NULL)
74117448
{
74127449
start_partial = mb->start_used_ptr;

0 commit comments

Comments
 (0)