a dz@sdZddlZddlZddlZddlmZmZmZgdZeeBeBZ Gddde Z GdddZ d d Z Gd d d Zd dZddZGddde ZddZddZddZdS)zDebugger basicsN) CO_GENERATOR CO_COROUTINECO_ASYNC_GENERATOR)BdbQuitBdb Breakpointc@seZdZdZdS)rz Exception to give up completely.N)__name__ __module__ __qualname____doc__r r /usr/lib64/python3.9/bdb.pyr src@speZdZdZdZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd[d&d'Zd\d(d)Zd*d+Zd,d-Zd.d/Zd]d0d1Zd2d3Zd4d5Zd^d7d8Zd9d:Zd;d<Zd=d>Z d?d@Z!dAdBZ"dCdDZ#dEdFZ$dGdHZ%dIdJZ&dKdLZ'dMdNZ(d_dPdQZ)d`dRdSZ*dadTdUZ+dVdWZ,dXdYZ-dS)braGeneric Python debugger base class. This class takes care of details of the trace facility; a derived class should implement user interaction. The standard debugger class (pdb.Pdb) is an example. The optional skip argument must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. Whether a frame is considered to originate in a certain module is determined by the __name__ in the frame globals. NcCs(|r t|nd|_i|_i|_d|_dS)N)setskipbreaksfncacheframe_returning)selfrr r r __init__sz Bdb.__init__cCsR|d|dddkr|S|j|}|sNtj|}tj|}||j|<|S)a%Return canonical form of filename. For real filenames, the canonical form is a case-normalized (on case insensitive filesystems) absolute path. 'Filenames' with angle brackets, such as "", generated in interactive mode, are returned unchanged. <>)rgetospathabspathnormcase)rfilenamecanonicr r r r%s    z Bdb.canoniccCs&ddl}|d|_|dddS)z5Set values of attributes as ready to start debugging.rN) linecache checkcachebotframe _set_stopinfo)rr r r r reset6sz Bdb.resetcCs|jr dS|dkr||S|dkr0|||S|dkrD|||S|dkrX|||S|dkrf|jS|dkrt|jS|dkr|jStd t||jS) aODispatch a trace function for debugged frames based on the event. This function is installed as the trace function for debugged frames. Its return value is the new trace function, which is usually itself. The default implementation decides how to dispatch a frame, depending on the type of event (passed in as a string) that is about to be executed. The event can be one of the following: line: A new line of code is going to be executed. call: A function is about to be called or another code block is entered. return: A function or other code block is about to return. exception: An exception has occurred. c_call: A C function is about to be called. c_return: A C function has returned. c_exception: A C function has raised an exception. For the Python events, specialized functions (see the dispatch_*() methods) are called. For the C events, no action is taken. The arg parameter depends on the previous event. Nlinecallreturn exceptionc_call c_exceptionc_returnz*bdb.Bdb.dispatch: unknown debugging event:)quitting dispatch_line dispatch_calldispatch_returndispatch_exceptiontrace_dispatchprintrepr)rframeeventargr r r r1=s$    zBdb.trace_dispatchcCs.||s||r(|||jr(t|jS)a Invoke user function and return trace function for line event. If the debugger stops on the current line, invoke self.user_line(). Raise BdbQuit if self.quitting is set. Return self.trace_dispatch to continue tracing in this scope. ) stop_here break_here user_liner,rr1rr4r r r r-hs  zBdb.dispatch_linecCsd|jdur|j|_|jS||s0||s0dS|jrH|jjt@rH|jS| |||j r^t |jS)a Invoke user function and return trace function for call event. If the debugger stops on this function call, invoke self.user_call(). Raise BdbQuit if self.quitting is set. Return self.trace_dispatch to continue tracing in this scope. N) r"f_backr1r7break_anywhere stopframef_codeco_flagsGENERATOR_AND_COROUTINE_FLAGS user_callr,rrr4r6r r r r.ts   zBdb.dispatch_callcCs||s||jkr||jr,|jjt@r,|jSz||_|||Wd|_nd|_0|j r\t |j|ur||j dkr|| dd|jS)aInvoke user function and return trace function for return event. If the debugger stops on this function return, invoke self.user_return(). Raise BdbQuit if self.quitting is set. Return self.trace_dispatch to continue tracing in this scope. Nr) r7 returnframer=r>r?r@r1r user_returnr,r stoplinenor#rBr r r r/s  zBdb.dispatch_returncCs||rF|jjt@r.|dtur.|ddus||||jrtnD|jr||jur|jjjt@r|dtt fvr||||jrt|j S)aInvoke user function and return trace function for exception event. If the debugger stops on this exception, invoke self.user_exception(). Raise BdbQuit if self.quitting is set. Return self.trace_dispatch to continue tracing in this scope. rN) r7r>r?r@ StopIterationuser_exceptionr,rr= GeneratorExitr1rBr r r r0s          zBdb.dispatch_exceptioncCs.|dur dS|jD]}t||rdSqdS)z4Return True if module_name matches any skip pattern.NFT)rfnmatch)r module_namepatternr r r is_skipped_modules   zBdb.is_skipped_modulecCsN|jr||jdrdS||jur@|jdkr4dS|j|jkS|jsJdSdS)z>Return True if frame is below the starting frame in the stack.rFrT)rrM f_globalsrr=rEf_linenor:r r r r7s   z Bdb.stop_herecCs||jj}||jvrdS|j}||j|vrJ|jj}||j|vrJdSt|||\}}|r|j|_|r|j r| t |jdSdSdS)zReturn True if there is an effective breakpoint for this line. Check for line or function breakpoint and if in effect. Delete temporary breakpoints if effective() says to. FTN) rr> co_filenamerrOco_firstlineno effectivenumber currentbp temporarydo_clearstr)rr4rlinenobpflagr r r r8s  zBdb.break_herecCs tddS)zlRemove temporary breakpoint. Must implement in derived classes or get NotImplementedError. z)subclass of bdb must implement do_clear()N)NotImplementedError)rr6r r r rVsz Bdb.do_clearcCs||jj|jvS)zEReturn True if there is any breakpoint for frame's filename. )rr>rPrr:r r r r<szBdb.break_anywherecCsdS)z&Called if we might stop in a function.Nr )rr4 argument_listr r r rAsz Bdb.user_callcCsdS)z'Called when we stop or break at a line.Nr r:r r r r9sz Bdb.user_linecCsdS)z&Called when a return trap is set here.Nr )rr4 return_valuer r r rD szBdb.user_returncCsdS)z$Called when we stop on an exception.Nr )rr4exc_infor r r rH szBdb.user_exceptionrcCs||_||_d|_||_dS)zSet the attributes for stopping. If stoplineno is greater than or equal to 0, then stop at line greater than or equal to the stopline. If stoplineno is -1, then don't stop at all. FN)r=rCr,rE)rr=rCrEr r r r#szBdb._set_stopinfocCs$|dur|jd}||||dS)zxStop when the line with the lineno greater than the current one is reached or when returning from current frame.Nr)rOr#)rr4rXr r r set_until"s z Bdb.set_untilcCs0|jr |jj}|r |js |j|_|dddS)zStop after one line of code.N)rr;f_tracer1r#)r caller_framer r r set_step*s  z Bdb.set_stepcCs||ddS)z2Stop on the next line in or below the given frame.N)r#r:r r r set_next6sz Bdb.set_nextcCs.|jjt@r||ddn||j|dS)z)Stop when returning from the given frame.Nr)r>r?r@r#r;r:r r r set_return:s zBdb.set_returncCsL|durtj}||r4|j|_||_|j}q|t|jdS)znStart debugging from frame. If frame is not specified, debugging starts from caller's frame. N) sys _getframer;r$r1r`r"rbsettracer:r r r set_traceAs z Bdb.set_tracecCsH||jdd|jsDtdtj}|rD||jurD|`|j}q*dS)zStop only at breakpoints or when finished. If there are no breakpoints, set the system trace function to None. Nr)r#r"rrergrfr;r`r:r r r set_continuePs  zBdb.set_continuecCs"|j|_d|_d|_tddS)zuSet quitting attribute to True. Raises BdbQuit exception in the next call to a dispatch_*() method. NT)r"r=rCr,rergrr r r set_quit_sz Bdb.set_quitFc Csb||}ddl}|||}|s.d||fS|j|g}||vrN||t|||||} dS)zSet a new breakpoint for filename:lineno. If lineno doesn't exist for the filename, return an error message. The filename should be in canonical form. rNzLine %s:%d does not exist)rr getliner setdefaultappendr) rrrXrUcondfuncnamer r%listrYr r r set_breakps    z Bdb.set_breakcCs4||ftjvr|j|||j|s0|j|=dS)aPrune breakpoints for filename:lineno. A list of breakpoints is maintained in the Bdb instance and in the Breakpoint class. If a breakpoint in the Bdb instance no longer exists in the Breakpoint class, then it's removed from the Bdb instance. N)rbplistrremoverrrXr r r _prune_breakss zBdb._prune_breakscCsj||}||jvrd|S||j|vr6d||fStj||fddD] }|qL|||dS)znDelete breakpoints for filename:lineno. If no breakpoints were set, return an error message. There are no breakpoints in %szThere is no breakpoint at %s:%dN)rrrrsdeleteMerv)rrrXrYr r r clear_breaks     zBdb.clear_breakc CsXz||}Wn,ty:}zt|WYd}~Sd}~00|||j|jdS)zxDelete a breakpoint by its index in Breakpoint.bpbynumber. If arg is invalid, return an error message. N)get_bpbynumber ValueErrorrWrxrvfiler%)rr6rYerrr r r clear_bpbynumberszBdb.clear_bpbynumbercCsX||}||jvrd|S|j|D]$}tj||f}|D] }|q', the input arguments, the return value, and the line of code (if it exists). rNz%s(%r)zz() __return__z->) r reprlibrr>rPco_namef_localsr3rlrNstrip) r frame_linenolprefixr rr4rXrsrvr%r r r format_stack_entrys    zBdb.format_stack_entryc Cs|durddl}|j}|dur"|}|t|tr@t|dd}t|jz8zt |||Wnt ypYn0Wd|_ tdnd|_ td0dS)zDebug a statement executed via the exec() function. globals defaults to __main__.dict; locals defaults to globals. NrzexecT) __main____dict__r$ isinstancerWcompilerergr1rrr,)rcmdglobalslocalsrr r r run5s"     zBdb.runc Cs|durddl}|j}|dur"|}|t|jzHz t|||WWd|_tdStyjYn0Wd|_tdnd|_td0dS)zDebug an expression executed via the eval() function. globals defaults to __main__.dict; locals defaults to globals. NrT) rrr$rergr1evalr,r)rexprrrrr r r runevalKs$    z Bdb.runevalcCs||||dS)z.For backwards-compatibility. Defers to run().N)r)rrrrr r r runctx_sz Bdb.runctxc Osj|t|jd}z:z||i|}Wnty>Yn0Wd|_tdnd|_td0|S)zWDebug a single function call. Return the result of the function call. NT)r$rergr1rr,)rfuncargskwdsresr r r runcallfs    z Bdb.runcall)N)r)N)N)FNN)r)NN)NN).rr r r rrr$r1r-r.r/r0rMr7r8rVr<rAr9rDrHr#r_rbrcrdrhrirkrrrvryr~rrrzrrrrrrrrrrr r r r rsZ +               rcCstdS)z|j|jf}d|j|j<|j|||j|s:|j|=dS)zDelete the breakpoint from the list associated to a file:line. If it is the last breakpoint in that position, it also deletes the entry for the file:line. N)r|r%rrSrsrt)rindexr r r rxs    zBreakpoint.deleteMecCs d|_dS)zMark the breakpoint as enabled.TNrrjr r r enableszBreakpoint.enablecCs d|_dS)z Mark the breakpoint as disabled.FNrrjr r r disableszBreakpoint.disablecCs"|durtj}t||ddS)zPrint the output of bpformat(). The optional out argument directs where the output is sent and defaults to standard output. N)r|)restdoutr2bpformat)routr r r bpprintszBreakpoint.bpprintcCs|jr d}nd}|jr |d}n|d}d|j||j|jf}|jrT|d|jf7}|jrj|d|jf7}|jr|jdkrd }nd }|d |j|f7}|S) zReturn a string with information about the breakpoint. The information includes the breakpoint number, temporary status, file:line position, break condition, number of times to ignore, and number of times hit. zdel zkeep zyes zno z%-4dbreakpoint %s at %s:%dz stop only if %sz ignore next %d hitsrrz" breakpoint already hit %d time%s)rUrrSr|r%rorr)rdispretssr r r rs&  zBreakpoint.bpformatcCsd|j|j|jfS)z1Return a condensed description of the breakpoint.zbreakpoint %s at %s:%s)rSr|r%rjr r r __str__szBreakpoint.__str__)FNN)N)rr r r rrsrrrxrrrrrr r r r r}s  rcCsN|js|j|jkrdSdS|jj|jkr,dS|js:|j|_|j|jkrJdSdS)aVReturn True if break should happen here. Whether a break should happen depends on the way that b (the breakpoint) was set. If it was set via line number, check if b.line is the same as the one in the frame. If it was set via function name, check if this is the right function and if it is on the first executable line. FT)rpr%rOr>rr)br4r r r checkfuncnames  rcCstj||f}|D]}|jsqt||s*q|jd7_|jsh|jdkrZ|jd8_qq|dfSqzBt|j|j|j }|r|jdkr|jd8_n|dfWSWq|dfYS0qdS)aEDetermine which breakpoint for this file:line is to be acted upon. Called only if we know there is a breakpoint at this location. Return the breakpoint that was triggered and a boolean that indicates if it is ok to delete a temporary breakpoint. Return (None, None) if there is no matching breakpoint. rrTF)NN) rrsrrrrorrrNr)r|r%r4 possiblesrvalr r r rRs*   rRc@s,eZdZddZddZddZddZd S) TdbcCs |jj}|sd}td||dS)N???z+++ call)r>rr2)rr4rnamer r r rADsz Tdb.user_callcCsTddl}|jj}|sd}||jj}|||j|j}td||j|d| dS)Nrrz+++:) r r>rrrPrlrOrNr2r)rr4r rfnr%r r r r9Hs z Tdb.user_linecCstd|dS)Nz +++ returnr2)rr4retvalr r r rDOszTdb.user_returncCstd||dS)Nz +++ exception)r2ri)rr4 exc_stuffr r r rHQs zTdb.user_exceptionN)rr r rAr9rDrHr r r r rCsrcCs&td|dt|d}td|dS)Nzfoo() z bar returned)r2bar)nxr r r fooUs  rcCstd|d|dS)Nzbar(rrFr)ar r r rZs rcCst}|ddS)Nzimport bdb; bdb.foo(10))rr)rr r r test^sr)r rJrerinspectrrr__all__r@ ExceptionrrrhrrrRrrrrr r r r s( kt"0