
    ^ho"                     :   S SK r S SKrS SKJrJrJr  S SKrS SKJr  S SK	J
r
  S SKJr  S SKJrJrJr  S SKJr  S SKJr  S S	KJrJr  S
rSrS \l        Sq\R8                  " \5        S r\R<                  R>                  RA                  \5         " S S\RB                  5      r"\"" 5          " S S\RB                  5      r#\#" 5          " S S\RB                  5      r$\$" 5          " S S\RB                  5      r%\%" 5         \RL                  " S5         " S S\5      r'g)    N)ListOptionalUnion)clangd_lsp_integration)code)	DBGDialog)_ArgumentEntry_FrameSummaryEntry_SkippedFramesEntry)chatdbg_config)command_is_safe)chatdbg_was_calledprint_exit_messagezpanic_log.txtz(ChatDBG gdb) c                     [         $ N)PROMPT)current_prompts    M/home/james-whalen/.local/lib/python3.13/site-packages/chatdbg/chatdbg_gdb.py<lambda>r      s         c                 `    [        U S5      (       d  SqgU R                  b  U R                  qgg)z/Sets last error type so we can report it later.stop_signalr   N)hasattrlast_error_typer   )events    r   stop_handlerr       s5     5-(($++ %r   c                        \ rS rSrS rS rSrg)Code.   c                 b    [         R                  R                  U S[         R                  5        g )Nr   gdbCommand__init__COMMAND_USERselfs    r   r%   Code.__init__0   s    T63+;+;<r   c                 ,    [        [        U5      5        g r   )printr   r(   commandfrom_ttys      r   invokeCode.invoke3   s    d7mr    N__name__
__module____qualname____firstlineno__r%   r/   __static_attributes__r1   r   r   r   r   .   s    =r   r   c                        \ rS rSrS rS rSrg)
Definition;   c                 b    [         R                  R                  U S[         R                  5        g )N
definitionr"   r'   s    r   r%   Definition.__init__=   s    T<1A1ABr   c                 B    [        [        R                  " U5      5        g r   )r+   r   native_definitionr,   s      r   r/   Definition.invoke@   s    $66w?@r   r1   Nr2   r1   r   r   r9   r9   ;   s    Cr   r9   c                        \ rS rSrS rS rSrg)ConfigH   c                 b    [         R                  R                  U S[         R                  5        g )Nconfigr"   r'   s    r   r%   Config.__init__J   s    T8S-=-=>r   c                 f    UR                  5       n[        R                  " U5      n[        U5        g r   )splitr   parse_only_user_flagsr+   )r(   r-   r.   argsmessages        r   r/   Config.invokeM   s'    }} 66t<gr   r1   Nr2   r1   r   r   rB   rB   H   s    ?r   rB   c                   $    \ rS rSrSrS rS rSrg)WhyX   z+Provides root cause analysis for a failure.c                 b    [         R                  R                  U S[         R                  5        g )Nwhyr"   r'   s    r   r%   Why.__init__[   s    T5#*:*:;r   c                      [        [        5      nUR                  U5        g ! [         a  n[	        [        U5      5         S nAg S nAff = fr   )	GDBDialogr   dialog	Exceptionr+   str)r(   r-   r.   rU   es        r   r/   
Why.invoke^   s;    	v&FMM'" 	#a&M	s    # 
AAAr1   N)r3   r4   r5   r6   __doc__r%   r/   r7   r1   r   r   rN   rN   X   s    5<r   rN   zalias chat = whyc            	          ^  \ rS rSrSU 4S jjrS rS rS r SS\S\	\
\\\4         4S jjrS	 rS
 rS rS rS rS\4S jrSrU =r$ )rT   l   returnc                 8   > [        5         [        TU ]	  U5        g r   )r   superr%   )r(   prompt	__class__s     r   r%   GDBDialog.__init__n   s     r   c                 @    UR                  5       R                  S5      $ )NzUndefined command:)strip
startswith)r(   rK   s     r   _message_is_a_bad_command_error)GDBDialog._message_is_a_bad_command_errorr   s    }}))*>??r   c                 r     [         R                  " USS9$ ! [         a  n[        U5      s S nA$ S nAff = f)NT	to_string)r#   executerV   rW   )r(   r-   rX   s      r   _run_one_commandGDBDialog._run_one_commandu   s1    	;;w$77 	q6M	s    
6
166c                     [         (       d  Sq  [        R                  " 5       nUR                  5       ng ! [        R                   a    U R                  S5         g [         a    U R                  S5         g f = f)NSIGSEGVz@Must be attached to a program that fails to use `why` or `chat`.zSYour program must be compiled with debug information (`-g`) to use `why` or `chat`.)r   r#   selected_frameblockerrorfailRuntimeError)r(   framerq   s      r   check_debugger_stateGDBDialog.check_debugger_state{   sg     (O
	&&(EKKMEyy 	IIR  	IIe	s   %5 %A9A98A9max_entriesc           	         [         R                  " 5       nU(       d  g Sn/ n[         R                  " 5       nSnUGb  US-  nUR                  5       nU(       d  US-  nUR	                  5       nM7  UR                  5       nUR                  b  UR                  R                  5       n	U	S :X  a  Sn	OU	R                  [        R                  " 5       5      (       a  [        R                  R                  U	5      n	[        R                  R                  U	5      (       d  US-  nUR	                  5       nM  OS n	UR                  b  UR                  n
OS n
/ n[         R                  n UR!                  5       nU Hc  nUR$                  (       d  M  UR&                  nUR                  n[)        UR+                  U5      5      nUR-                  [/        XU5      5        Me     US:  a  UR-                  [1        U5      5        SnUR-                  [3        XgXU
5      5        [5        U5      U:  a  OUR	                  5       nUb  GM  US:  a  UR-                  [1        U5      5        U$ ! ["         a    US-  nUR	                  5       n GMN  f = f)Nr      z	[unknown])r#   selected_threadrp   nameolderfind_salsymtabfullnamere   osgetcwdpathrelpathexistslineBlockrq   rV   is_argumenttyperW   read_varappendr	   r   r
   len)r(   rx   threadskipped	summariesru   indexr}   symtab_and_line	file_pathlineno	argumentsrq   symboltypenamevalues                   r   _get_frame_summariesGDBDialog._get_frame_summaries   sB    $$&JL	""$QJE::<D1#nn.O %%1+22;;=	$ +I !++BIIK88$&GGOOI$>	77>>)441 %  5
 !	 ##/(-- /1IIIE
  %%%%{{H!;;Dt 45E$$^HE%JK   {  !4W!=>"5	fM 9~,KKMEu x Q;09:3  1s   %I$ $JJc                      [        [        S5       nUR                  5       nS S S 5        WS-   [        -   q[        $ ! , (       d  f       N = f!    [        $ = f)Nr
)openRUST_PANIC_LOG_FILENAMEreadr   )r(   log	panic_logs      r   _initial_prompt_error_message'GDBDialog._initial_prompt_error_message   sS    	-s3sHHJ	 4'$.@O  43	s   A =A 
AA Ac                     g)z:Anything more beyond the initial error message to include.Nr1   r'   s    r   _initial_prompt_error_details'GDBDialog._initial_prompt_error_details   s    r   c                    [         R                  " 5       R                  R                  nUR	                  [
        R                  " 5       5      (       a=  [
        R                  R                  S[
        R                  R                  U5      5      nSn[         R                  " SSS9R                  5       nUR	                  U5      (       a  U[        U5      S  R                  S5      nUS-   U-   $ )N.CArgument list to give program being debugged when it is started is 	show argsTri   ." )r#   selected_inferior	progspacefilenamere   r   r   r   joinr   rk   rd   r   )r(   executable_pathprefixrJ   s       r   _initial_prompt_command_line&GDBDialog._initial_prompt_command_line   s    //1;;DD%%biik22 ggll30PQOV{{;$7==???6""F&,,T2D$t++r   c                    Sn[         R                  " SSS9R                  5       nUR                  U5      (       a  U[	        U5      S  R                  S5      nUR                  S5      nUS:w  a2  X#S-   S  R                  5       n [        US	5      R                  5       nU$ g ! [         a    U R                  S
U S35         g f = f)Nr   r   Tri   r   <rz   r{   r   zThe detected input file z could not be read.)
r#   rk   rd   re   r   findr   r   rV   rs   )r(   r   rJ   
input_pipe
input_filecontents         r   _initial_prompt_inputGDBDialog._initial_prompt_input   s    V{{;$7==???6""F&,,T2DYYs^
1n./557JVz3/446	 
  V		4ZL@STUVs   B# #CCc                     g)zZ
Return a simple backtrace to show the LLM where we are on the stack
in followup prompts.
Nr1   r'   s    r   _prompt_stackGDBDialog._prompt_stack  s    
 r   r-   c                     [         R                  (       d  [        U5      (       d  SU l        USU S34$ XR	                  U5      4$ )a  
{
    "name": "debug",
    "description": "The `debug` function runs a GDB command on the stopped program and gets the response.",
    "parameters": {
        "type": "object",
        "properties": {
            "command": {
                "type": "string",
                "description": "The GDB command to run, possibly with arguments."
            }
        },
        "required": [ "command" ]
    }
}
Tz	Command `z` is not allowed.)r   unsafer   _unsafe_cmdrl   )r(   r-   s     r   	llm_debugGDBDialog.llm_debug  sH    " $$_W-E-E#Diy0ABBB--g666r   )r   )r]   N)   )r3   r4   r5   r6   r%   rf   rl   rv   intr   r   r   r
   r   r   r   r   r   r   r   rW   r   r7   __classcell__)ra   s   @r   rT   rT   l   sw    !@& "$MM	$u/1DDEF	GM^	,V7 7 7r   rT   )(r   atexittypingr   r   r   r#   chatdbg.native_utilr   chatdbg.native_util.coder   chatdbg.native_util.dbg_dialogr   chatdbg.native_util.stacksr	   r
   r   chatdbg.util.configr   chatdbg.native_util.safetyr   chatdbg.util.exit_messager   r   r   r   prompt_hookr   registerr   eventsstopconnectr$   r   r9   rB   rN   rk   rT   r1   r   r   <module>r      s    	  ( ( 
 6 ) 4 
 / 6 L * 	 0  " #, 

   %3;;    	S[[ 	 #++    v7	 v7r   