File: reverse_debugging.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (851 lines) | stat: -rw-r--r-- 31,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
import sys
from rpython.rlib import revdb
from rpython.rlib.debug import make_sure_not_resized
from rpython.rlib.objectmodel import specialize, we_are_translated
from rpython.rtyper.annlowlevel import cast_gcref_to_instance
from pypy.interpreter.error import OperationError, oefmt
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter import gateway, typedef, pycode, pytraceback, pyframe
from pypy.module.marshal import interp_marshal
from pypy.interpreter.executioncontext import AbstractActionFlag, ActionFlag


class DBState:
    standard_code = True
    breakpoint_stack_id = 0
    breakpoint_funcnames = None
    breakpoint_filelines = None
    breakpoint_by_file = None
    breakpoint_version = 0
    printed_objects = {}
    metavars = []
    watch_progs = []
    watch_futures = {}

dbstate = DBState()


pycode.PyCode.co_revdb_linestarts = None   # or a string: see below
pycode.PyCode.co_revdb_bkpt_version = 0    # see check_and_trigger_bkpt()
pycode.PyCode.co_revdb_bkpt_cache = None   # see check_and_trigger_bkpt()

# invariant: "f_revdb_nextline_instr" is the bytecode offset of
# the start of the line that follows "last_instr".
pyframe.PyFrame.f_revdb_nextline_instr = -1


# ____________________________________________________________


def setup_revdb(space):
    """Called at run-time, before the space is set up.

    The various register_debug_command() lines attach functions
    to some commands that 'revdb.py' can call, if we are running
    in replay mode.
    """
    assert space.config.translation.reverse_debugger
    dbstate.space = space

    make_sure_not_resized(dbstate.watch_progs)
    make_sure_not_resized(dbstate.metavars)

    revdb.register_debug_command(revdb.CMD_PRINT, lambda_print)
    revdb.register_debug_command(revdb.CMD_BACKTRACE, lambda_backtrace)
    revdb.register_debug_command(revdb.CMD_LOCALS, lambda_locals)
    revdb.register_debug_command(revdb.CMD_BREAKPOINTS, lambda_breakpoints)
    revdb.register_debug_command(revdb.CMD_STACKID, lambda_stackid)
    revdb.register_debug_command("ALLOCATING", lambda_allocating)
    revdb.register_debug_command(revdb.CMD_ATTACHID, lambda_attachid)
    revdb.register_debug_command(revdb.CMD_COMPILEWATCH, lambda_compilewatch)
    revdb.register_debug_command(revdb.CMD_CHECKWATCH, lambda_checkwatch)
    revdb.register_debug_command(revdb.CMD_WATCHVALUES, lambda_watchvalues)


# ____________________________________________________________


def enter_call(caller_frame, callee_frame):
    if dbstate.breakpoint_funcnames is not None:
        name = callee_frame.getcode().co_name
        if name in dbstate.breakpoint_funcnames:
            revdb.breakpoint(dbstate.breakpoint_funcnames[name])
    if dbstate.breakpoint_stack_id != 0 and caller_frame is not None:
        if dbstate.breakpoint_stack_id == revdb.get_unique_id(caller_frame):
            revdb.breakpoint(-1)
    #
    code = callee_frame.pycode
    if code.co_revdb_linestarts is None:
        build_co_revdb_linestarts(code)

def leave_call(caller_frame, got_exception):
    if dbstate.breakpoint_stack_id != 0 and caller_frame is not None:
        if dbstate.breakpoint_stack_id == revdb.get_unique_id(caller_frame):
            revdb.breakpoint(-2)
    if we_are_translated():
        stop_point_activate(-2 + got_exception)

def stop_point():
    if we_are_translated():
        revdb.breakpoint(-3)


def jump_backward(frame, jumpto):
    # When we see a jump backward, we set 'f_revdb_nextline_instr' in
    # such a way that the next instruction, at 'jumpto', will trigger
    # stop_point_activate().  We have to trigger it even if
    # 'jumpto' is not actually a start of line.  For example, after a
    # 'while foo:', the body ends with a JUMP_ABSOLUTE which
    # jumps back to the *second* opcode of the while.
    frame.f_revdb_nextline_instr = jumpto


def potential_stop_point(frame):
    if not we_are_translated():
        return
    #
    # We only record a stop_point at every line, not every bytecode.
    # Uses roughly the same algo as ExecutionContext.run_trace_func()
    # to know where the line starts are, but tweaked for speed,
    # avoiding the quadratic complexity when run N times with a large
    # code object.
    #
    cur = frame.last_instr
    if cur < frame.f_revdb_nextline_instr:
        return    # fast path: we're still inside the same line as before
    #
    call_stop_point_at_line = True
    co_revdb_linestarts = frame.pycode.co_revdb_linestarts
    if cur > frame.f_revdb_nextline_instr:
        #
        # We jumped forward over the start of the next line.  We're
        # inside a different line, but we will only trigger a stop
        # point if we're at the starting bytecode of that line.  Fetch
        # from co_revdb_linestarts the start of the line that is at or
        # follows 'cur'.
        ch = ord(co_revdb_linestarts[cur])
        if ch == 0:
            pass   # we are at the start of a line now
        else:
            # We are not, so don't call stop_point_activate().
            # We still have to fill f_revdb_nextline_instr.
            call_stop_point_at_line = False
    #
    if call_stop_point_at_line:
        stop_point_activate(pycode=frame.pycode, opindex=cur)
        cur += 1
        ch = ord(co_revdb_linestarts[cur])
    #
    # Update f_revdb_nextline_instr.  Check if 'ch' was greater than
    # 255, in which case it was rounded down to 255 and we have to
    # continue looking
    nextline_instr = cur + ch
    while ch == 255:
        ch = ord(co_revdb_linestarts[nextline_instr])
        nextline_instr += ch
    frame.f_revdb_nextline_instr = nextline_instr


def build_co_revdb_linestarts(code):
    # Inspired by findlinestarts() in the 'dis' standard module.
    # Set up 'bits' so that it contains \x00 at line starts and \xff
    # in-between.
    bits = ['\xff'] * (len(code.co_code) + 1)
    if not code.hidden_applevel:
        lnotab = code.co_lnotab
        addr = 0
        p = 0
        newline = 1
        while p + 1 < len(lnotab):
            byte_incr = ord(lnotab[p])
            line_incr = ord(lnotab[p+1])
            if byte_incr:
                if newline != 0:
                    bits[addr] = '\x00'
                    newline = 0
                addr += byte_incr
            newline |= line_incr
            p += 2
        if newline:
            bits[addr] = '\x00'
    bits[len(code.co_code)] = '\x00'
    #
    # Change 'bits' so that the character at 'i', if not \x00, measures
    # how far the next \x00 is
    next_null = len(code.co_code)
    p = next_null - 1
    while p >= 0:
        if bits[p] == '\x00':
            next_null = p
        else:
            ch = next_null - p
            if ch > 255: ch = 255
            bits[p] = chr(ch)
        p -= 1
    lstart = ''.join(bits)
    code.co_revdb_linestarts = lstart
    return lstart

def get_final_lineno(code):
    lineno = code.co_firstlineno
    lnotab = code.co_lnotab
    p = 1
    while p < len(lnotab):
        line_incr = ord(lnotab[p])
        lineno += line_incr
        p += 2
    return lineno

def find_line_starts(code):
    # RPython version of dis.findlinestarts()
    lnotab = code.co_lnotab
    lastlineno = -1
    lineno = code.co_firstlineno
    addr = 0
    p = 0
    result = []
    while p < len(lnotab) - 1:
        byte_incr = ord(lnotab[p + 0])
        line_incr = ord(lnotab[p + 1])
        if byte_incr:
            if lineno != lastlineno:
                result.append((addr, lineno))
                lastlineno = lineno
            addr += byte_incr
        lineno += line_incr
        p += 2
    if lineno != lastlineno:
        result.append((addr, lineno))
    return result

class NonStandardCode(object):
    def __enter__(self):
        dbstate.standard_code = False
        self.t = dbstate.space.actionflag._ticker
        self.c = dbstate.space.actionflag._ticker_revdb_count
    def __exit__(self, *args):
        dbstate.space.actionflag._ticker = self.t
        dbstate.space.actionflag._ticker_revdb_count = self.c
        dbstate.standard_code = True
non_standard_code = NonStandardCode()


def stop_point_activate(place=0, pycode=None, opindex=-1):
    if revdb.watch_save_state():
        any_watch_point = False
        # ^^ this flag is set to True if we must continue to enter this
        # block of code.  If it is still False for watch_restore_state()
        # below, then future watch_save_state() will return False too---
        # until the next time revdb.c:set_revdb_breakpoints() is called.
        space = dbstate.space
        with non_standard_code:
            watch_id = -1
            if dbstate.breakpoint_by_file is not None:
                any_watch_point = True
                if pycode is not None:
                    watch_id = check_and_trigger_bkpt(pycode, opindex)
            if watch_id == -1:
                for prog, watch_id, expected in dbstate.watch_progs:
                    any_watch_point = True
                    try:
                        got = _run_watch(space, prog)
                    except OperationError as e:
                        got = e.errorstr(space)
                    except Exception:
                        break
                    if got != expected:
                        break
                else:
                    watch_id = -1
        revdb.watch_restore_state(any_watch_point)
        if watch_id != -1:
            revdb.breakpoint(watch_id)
    revdb.stop_point(place)


def future_object(space):
    return space.w_Ellipsis    # a random prebuilt object

def load_metavar(index):
    assert index >= 0
    space = dbstate.space
    metavars = dbstate.metavars
    w_var = metavars[index] if index < len(metavars) else None
    if w_var is None:
        raise oefmt(space.w_NameError, "no constant object '$%d'",
                    index)
    if w_var is future_object(space):
        raise oefmt(space.w_RuntimeError,
                    "'$%d' refers to an object created later in time",
                    index)
    return w_var

def set_metavar(index, w_obj):
    assert index >= 0
    if index >= len(dbstate.metavars):
        missing = index + 1 - len(dbstate.metavars)
        dbstate.metavars = dbstate.metavars + [None] * missing
    dbstate.metavars[index] = w_obj


# ____________________________________________________________


def fetch_cur_frame(silent=False):
    ec = dbstate.space.threadlocals.get_ec()
    if ec is None:
        frame = None
    else:
        frame = ec.topframeref()
    if frame is None and not silent:
        revdb.send_print("No stack.")
    return frame

def compile(source, mode):
    assert not dbstate.standard_code
    space = dbstate.space
    compiler = space.createcompiler()
    code = compiler.compile(source, '<revdb>', mode, 0,
                            hidden_applevel=True)
    return code


class W_RevDBOutput(W_Root):
    softspace = 0

    def __init__(self, space):
        self.space = space

    def descr_write(self, w_buffer):
        assert not dbstate.standard_code
        space = self.space
        if space.isinstance_w(w_buffer, space.w_unicode):
            w_buffer = space.call_method(w_buffer, 'encode',
                                         space.newtext('utf-8'))   # safe?
        revdb.send_output(space.bytes_w(w_buffer))

def descr_get_softspace(space, revdb):
    return space.newint(revdb.softspace)
def descr_set_softspace(space, revdb, w_newvalue):
    revdb.softspace = space.int_w(w_newvalue)

W_RevDBOutput.typedef = typedef.TypeDef(
    "revdb_output",
    write = gateway.interp2app(W_RevDBOutput.descr_write),
    # XXX is 'softspace' still necessary in Python 3?
    softspace = typedef.GetSetProperty(descr_get_softspace,
                                       descr_set_softspace,
                                       cls=W_RevDBOutput),
    )

def revdb_displayhook(space, w_obj):
    """Modified sys.displayhook() that also outputs '$NUM = ',
    for non-prebuilt objects.  Such objects are then recorded in
    'printed_objects'.
    """
    assert not dbstate.standard_code
    if space.is_w(w_obj, space.w_None):
        return
    uid = revdb.get_unique_id(w_obj)
    if uid > 0:
        dbstate.printed_objects[uid] = w_obj
        revdb.send_nextnid(uid)   # outputs '$NUM = '
    space.setitem(space.builtin.w_dict, space.newtext('_'), w_obj)
    # do repr() after setitem: if w_obj was produced successfully,
    # but its repr crashes because it tries to do I/O, then we already
    # have it recorded in '_' and in '$NUM ='.
    w_repr = space.repr(w_obj)
    if space.isinstance_w(w_repr, space.w_unicode):
        w_repr = space.call_method(w_repr, 'encode',
                                   space.newtext('utf-8'))   # safe?
    revdb.send_print(space.bytes_w(w_repr))

@gateway.unwrap_spec(name='text0', level=int)
def revdb_importhook(space, name, w_globals=None,
                     w_locals=None, w_fromlist=None, level=-1):
    # Incredibly simplified version of __import__, which only returns
    # already-imported modules and doesn't call any custom import
    # hooks.  Recognizes only absolute imports.  With a 'fromlist'
    # argument that is a non-empty list, returns the module 'name3' if
    # the 'name' argument is 'name1.name2.name3'.  With an empty or
    # None 'fromlist' argument, returns the module 'name1' instead.
    return space.appexec([space.newtext(name), w_fromlist or space.w_None,
                          space.newint(level), space.sys],
    """(name, fromlist, level, sys):
        if level > 0:
            raise ImportError("only absolute imports are "
                               "supported in the debugger")
        basename = name.split('.')[0]
        try:
            basemod = sys.modules[basename]
            mod = sys.modules[name]
        except KeyError:
            raise ImportError("'%s' not found or not imported yet "
                    "(the debugger can't import new modules, "
                    "and only supports absolute imports)" % (name,))
        if fromlist:
            return mod
        return basemod
    """)

@specialize.memo()
def get_revdb_displayhook(space):
    return gateway.interp2app(revdb_displayhook).spacebind(space)

@specialize.memo()
def get_revdb_importhook(space):
    return gateway.interp2app(revdb_importhook).spacebind(space)


def prepare_print_environment(space):
    assert not dbstate.standard_code
    w_revdb_output = W_RevDBOutput(space)
    w_displayhook = get_revdb_displayhook(space)
    w_import = get_revdb_importhook(space)
    space.sys.setdictvalue(space, 'stdout', w_revdb_output)
    space.sys.setdictvalue(space, 'stderr', w_revdb_output)
    space.sys.setdictvalue(space, 'displayhook', w_displayhook)
    space.builtin.setdictvalue(space, '__import__', w_import)

def command_print(cmd, expression):
    frame = fetch_cur_frame()
    if frame is None:
        return
    space = dbstate.space
    with non_standard_code:
        try:
            prepare_print_environment(space)
            code = compile(expression, 'single')
            try:
                code.exec_code(space,
                               frame.get_w_globals(),
                               frame.getdictscope())

            except OperationError as operationerr:
                # can't use sys.excepthook: it will likely try to do 'import
                # traceback', which might not be doable without using I/O
                tb = operationerr.get_traceback()
                if tb is not None:
                    revdb.send_print("Traceback (most recent call last):")
                    while tb is not None:
                        if not isinstance(tb, pytraceback.PyTraceback):
                            revdb.send_print("  ??? %s" % tb)
                            break
                        show_frame(tb.frame, tb.get_lineno(), indent='  ')
                        tb = tb.next
                revdb.send_print(operationerr.errorstr(space))

                # set the sys.last_xxx attributes
                w_type = operationerr.w_type
                w_value = operationerr.get_w_value(space)
                w_tb = operationerr.get_traceback()
                w_dict = space.sys.w_dict
                space.setitem(w_dict, space.newtext('last_type'), w_type)
                space.setitem(w_dict, space.newtext('last_value'), w_value)
                space.setitem(w_dict, space.newtext('last_traceback'), w_tb)

        except OperationError as e:
            revdb.send_print(e.errorstr(space, use_repr=True))
lambda_print = lambda: command_print


def file_and_lineno(frame, lineno):
    code = frame.getcode()
    return 'File "%s", line %d in %s' % (
        code.co_filename, lineno, code.co_name)

def show_frame(frame, lineno=0, indent=''):
    if lineno == 0:
        lineno = frame.get_last_lineno()
    revdb.send_output("%s%s\n%s  " % (
        indent,
        file_and_lineno(frame, lineno),
        indent))
    revdb.send_linecache(frame.getcode().co_filename, lineno)

def display_function_part(frame, max_lines_before, max_lines_after):
    if frame is None:
        return
    code = frame.getcode()
    if code.co_filename.startswith('<builtin>'):
        return
    first_lineno = code.co_firstlineno
    current_lineno = frame.get_last_lineno()
    final_lineno = get_final_lineno(code)
    #
    ellipsis_after = False
    if first_lineno < current_lineno - max_lines_before - 1:
        first_lineno = current_lineno - max_lines_before
        revdb.send_print("...")
    if final_lineno > current_lineno + max_lines_after + 1:
        final_lineno = current_lineno + max_lines_after
        ellipsis_after = True
    #
    for i in range(first_lineno, final_lineno + 1):
        if i == current_lineno:
            if revdb.current_place() == -2: # <= this is the arg to stop_point()
                prompt = "<< "     # return
            elif revdb.current_place() == -1:
                prompt = "!! "     # exceptional return
            else:
                prompt = " > "     # plain line
            revdb.send_output(prompt)
        else:
            revdb.send_output("   ")
        revdb.send_linecache(code.co_filename, i, strip=False)
    #
    if ellipsis_after:
        revdb.send_print("...")

def command_backtrace(cmd, extra):
    frame = fetch_cur_frame(silent=True)
    if cmd.c_arg1 == 0:
        if frame is not None:
            revdb.send_print("%s:" % (
                file_and_lineno(frame, frame.get_last_lineno()),))
        display_function_part(frame, max_lines_before=8, max_lines_after=5)
    elif cmd.c_arg1 == 2:
        display_function_part(frame, max_lines_before=1000,max_lines_after=1000)
    else:
        revdb.send_print("Current call stack (most recent call last):")
        if frame is None:
            revdb.send_print("  (empty)")
        frames = []
        while frame is not None:
            frames.append(frame)
            if len(frames) == 200:
                revdb.send_print("  ...")
                break
            frame = frame.get_f_back()
        while len(frames) > 0:
            show_frame(frames.pop(), indent='  ')
lambda_backtrace = lambda: command_backtrace


def command_locals(cmd, extra):
    frame = fetch_cur_frame()
    if frame is None:
        return
    space = dbstate.space
    with non_standard_code:
        try:
            prepare_print_environment(space)
            space.appexec([space.sys,
                           frame.getdictscope()], """(sys, locals):
                lst = sorted(locals.keys())
                print('Locals:')
                for key in lst:
                    try:
                        print('    %s =' % key, end=' ', flush=True)
                        s = '%r' % locals[key]
                        if len(s) > 140:
                            s = s[:100] + '...' + s[-30:]
                        print(s)
                    except:
                        exc, val, tb = sys.exc_info()
                        print('!<%s: %r>' % (exc, val))
            """)
        except OperationError as e:
            revdb.send_print(e.errorstr(space, use_repr=True))
lambda_locals = lambda: command_locals

# ____________________________________________________________


def check_and_trigger_bkpt(pycode, opindex):
    # We cache on 'pycode.co_revdb_bkpt_cache' either None or a dict
    # mapping {opindex: bkpt_num}.  This cache is updated when the
    # version in 'pycode.co_revdb_bkpt_version' does not match
    # 'dbstate.breakpoint_version' any more.
    if pycode.co_revdb_bkpt_version != dbstate.breakpoint_version:
        update_bkpt_cache(pycode)
    cache = pycode.co_revdb_bkpt_cache
    if cache is not None and opindex in cache:
        return cache[opindex]
    else:
        return -1

def update_bkpt_cache(pycode):
    # initialized by command_breakpoints():
    #     dbstate.breakpoint_filelines == [('FILENAME', lineno, bkpt_num)]
    # computed lazily (here, first half of the logic):
    #     dbstate.breakpoint_by_file == {'co_filename': {lineno: bkpt_num}}
    # the goal is to set:
    #     pycode.co_revdb_bkpt_cache == {opindex: bkpt_num}

    co_filename = pycode.co_filename
    try:
        linenos = dbstate.breakpoint_by_file[co_filename]
    except KeyError:
        linenos = None
        match = co_filename.upper()    # ignore cAsE in filename matching
        for filename, lineno, bkpt_num in dbstate.breakpoint_filelines:
            if match.endswith(filename) and (
                    len(match) == len(filename) or
                    match[-len(filename)-1] in '/\\'):    # a valid prefix
                if linenos is None:
                    linenos = {}
                linenos[lineno] = bkpt_num
        dbstate.breakpoint_by_file[co_filename] = linenos

    newcache = None
    if linenos is not None:
        # parse co_lnotab to figure out the opindexes that correspond
        # to the marked line numbers.  here, linenos == {lineno: bkpt_num}
        for addr, lineno in find_line_starts(pycode):
            if lineno in linenos:
                if newcache is None:
                    newcache = {}
                newcache[addr] = linenos[lineno]

    pycode.co_revdb_bkpt_cache = newcache
    pycode.co_revdb_bkpt_version = dbstate.breakpoint_version


def valid_identifier(s):
    if not s:
        return False
    if s[0].isdigit():
        return False
    for c in s:
        if not (c.isalnum() or c == '_'):
            return False
    return True

def add_breakpoint_funcname(name, i):
    if dbstate.breakpoint_funcnames is None:
        dbstate.breakpoint_funcnames = {}
    dbstate.breakpoint_funcnames[name] = i

def add_breakpoint_fileline(filename, lineno, i):
    # dbstate.breakpoint_filelines is just a list of (FILENAME, lineno, i).
    # dbstate.breakpoint_by_file is {co_filename: {lineno: i}}, but
    # computed lazily when we encounter a code object with the given
    # co_filename.  Any suffix 'filename' matches 'co_filename'.
    if dbstate.breakpoint_filelines is None:
        dbstate.breakpoint_filelines = []
        dbstate.breakpoint_by_file = {}
    dbstate.breakpoint_filelines.append((filename.upper(), lineno, i))

def add_breakpoint(name, i):
    # if it is empty, complain
    if not name:
        revdb.send_print("Empty breakpoint name")
        revdb.send_change_breakpoint(i)
        return
    # if it is surrounded by < >, it is the name of a code object
    if name.startswith('<') and name.endswith('>'):
        add_breakpoint_funcname(name, i)
        return
    # if it has no ':', it can be a valid identifier (which we
    # register as a function name), or a lineno
    original_name = name
    j = name.rfind(':')
    if j < 0:
        try:
            lineno = int(name)
        except ValueError:
            if name.endswith('()'):
                n = len(name) - 2
                assert n >= 0
                name = name[:n]
            if not valid_identifier(name):
                revdb.send_print(
                    'Note: "%s()" doesn''t look like a function name. '
                    'Setting breakpoint anyway' % name)
            add_breakpoint_funcname(name, i)
            name += '()'
            if name != original_name:
                revdb.send_change_breakpoint(i, name)
            return
        # "number" does the same as ":number"
        filename = ''
    else:
        # if it has a ':', it must end in ':lineno'
        try:
            lineno = int(name[j+1:])
        except ValueError:
            revdb.send_print('expected a line number after colon')
            revdb.send_change_breakpoint(i)
            return
        filename = name[:j]

    # the text before must be a pathname, possibly a relative one,
    # or be escaped by < >.  if it isn't, make it absolute and normalized
    # and warn if it doesn't end in '.py'.
    if filename == '':
        frame = fetch_cur_frame()
        if frame is None:
            revdb.send_change_breakpoint(i)
            return
        filename = frame.getcode().co_filename
    elif filename.startswith('<') and filename.endswith('>'):
        pass    # use unmodified
    elif not filename.lower().endswith('.py'):
        # use unmodified, but warn
        revdb.send_print(
            'Note: "%s" doesn''t look like a Python filename. '
            'Setting breakpoint anyway' % (filename,))

    add_breakpoint_fileline(filename, lineno, i)
    name = '%s:%d' % (filename, lineno)
    if name != original_name:
        revdb.send_change_breakpoint(i, name)

def command_breakpoints(cmd, extra):
    space = dbstate.space
    dbstate.breakpoint_stack_id = cmd.c_arg1
    revdb.set_thread_breakpoint(cmd.c_arg2)
    dbstate.breakpoint_funcnames = None
    dbstate.breakpoint_filelines = None
    dbstate.breakpoint_by_file = None
    dbstate.breakpoint_version += 1
    watch_progs = []
    with non_standard_code:
        for i, kind, name in revdb.split_breakpoints_arg(extra):
            if kind == 'B':
                add_breakpoint(name, i)
            elif kind == 'W':
                code = interp_marshal.loads(space, space.newbytes(name))
                watch_progs.append((code, i, ''))
    dbstate.watch_progs = watch_progs[:]
lambda_breakpoints = lambda: command_breakpoints


def command_watchvalues(cmd, extra):
    expected = extra.split('\x00')
    for j in range(len(dbstate.watch_progs)):
        prog, i, _ = dbstate.watch_progs[j]
        if i >= len(expected):
            raise IndexError
        dbstate.watch_progs[j] = prog, i, expected[i]
lambda_watchvalues = lambda: command_watchvalues


def command_stackid(cmd, extra):
    frame = fetch_cur_frame(silent=True)
    if frame is not None and cmd.c_arg1 != 0:     # parent_flag
        frame = dbstate.space.getexecutioncontext().getnextframe_nohidden(frame)
    if frame is None:
        uid = 0
    else:
        uid = revdb.get_unique_id(frame)
    if revdb.current_place() == -2:
        hidden_level = 1      # hide the "<<" events from next/bnext commands
    else:
        hidden_level = 0
    revdb.send_answer(revdb.ANSWER_STACKID, uid, hidden_level)
lambda_stackid = lambda: command_stackid


def command_allocating(uid, gcref):
    w_obj = cast_gcref_to_instance(W_Root, gcref)
    dbstate.printed_objects[uid] = w_obj
    try:
        index_metavar = dbstate.watch_futures.pop(uid)
    except KeyError:
        pass
    else:
        set_metavar(index_metavar, w_obj)
lambda_allocating = lambda: command_allocating


def command_attachid(cmd, extra):
    space = dbstate.space
    index_metavar = cmd.c_arg1
    uid = cmd.c_arg2
    try:
        w_obj = dbstate.printed_objects[uid]
    except KeyError:
        # uid not found, probably a future object
        dbstate.watch_futures[uid] = index_metavar
        w_obj = future_object(space)
    set_metavar(index_metavar, w_obj)
lambda_attachid = lambda: command_attachid


def command_compilewatch(cmd, expression):
    space = dbstate.space
    with non_standard_code:
        try:
            code = compile(expression, 'eval')
            marshalled_code = space.bytes_w(interp_marshal.dumps(
                space, code,
                space.newint(interp_marshal.Py_MARSHAL_VERSION)))
        except OperationError as e:
            revdb.send_watch(e.errorstr(space), ok_flag=0)
        else:
            revdb.send_watch(marshalled_code, ok_flag=1)
lambda_compilewatch = lambda: command_compilewatch

def command_checkwatch(cmd, marshalled_code):
    space = dbstate.space
    with non_standard_code:
        try:
            code = interp_marshal.loads(space, space.newbytes(marshalled_code))
            text = _run_watch(space, code)
        except OperationError as e:
            revdb.send_watch(e.errorstr(space), ok_flag=0)
        else:
            revdb.send_watch(text, ok_flag=1)
lambda_checkwatch = lambda: command_checkwatch


def _run_watch(space, prog):
    # must be called from non_standard_code!
    w_dict = space.builtin.w_dict
    w_res = prog.exec_code(space, w_dict, w_dict)
    return space.text_w(space.repr(w_res))


# ____________________________________________________________


ActionFlag._ticker_revdb_count = -1

class RDBSignalActionFlag(AbstractActionFlag):
    # Used instead of pypy.module.signal.interp_signal.SignalActionFlag
    # when we have reverse-debugging.  That other class would work too,
    # but inefficiently: it would generate two words of data per bytecode.
    # This class is tweaked to generate one byte per _SIG_TICKER_COUNT
    # bytecodes, at the expense of not reacting to signals instantly.

    # Threads: after 10'000 calls to decrement_ticker(), it should
    # return -1.  It should also return -1 if there was a signal.
    # This is done by calling _update_ticker_from_signals() every 100
    # calls, and invoking rsignal.pypysig_check_and_reset(); this in
    # turn returns -1 if there was a signal or if it was called 100
    # times.

    _SIG_TICKER_COUNT = 100
    _ticker = 0
    _ticker_revdb_count = _SIG_TICKER_COUNT * 10

    def get_ticker(self):
        return self._ticker

    def reset_ticker(self, value):
        self._ticker = value

    def rearm_ticker(self):
        self._ticker = -1

    def decrement_ticker(self, by):
        if we_are_translated():
            c = self._ticker_revdb_count - 1
            if c < 0:
                c = self._update_ticker_from_signals()
            self._ticker_revdb_count = c
        #if self.has_bytecode_counter:    # this 'if' is constant-folded
        #    print ("RDBSignalActionFlag: has_bytecode_counter: "
        #           "not supported for now")
        #    raise NotImplementedError
        return self._ticker

    def _update_ticker_from_signals(self):
        from rpython.rlib import rsignal
        if dbstate.standard_code:
            if rsignal.pypysig_check_and_reset():
                self.rearm_ticker()
        return self._SIG_TICKER_COUNT
    _update_ticker_from_signals._dont_inline_ = True