
    ^hx*                        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Jr  S S	KJr  S
rSrS\R4                  S\SS4S jr\R:                  " S5      S\R4                  S\S\R>                  S\SS4
S j5       r \R:                  " S5      S\R4                  S\S\R>                  S\SS4
S j5       r!\R:                  " S5      \R:                  " S5      S\R4                  S\S\R>                  S\4S j5       5       r"\R:                  " S5      S\R4                  S\S\R>                  S\4S j5       r# " S S\5      r$g)    N)ListOptionalUnion)clangd_lsp_integration)code)	DBGDialog)_ArgumentEntry_FrameSummaryEntry_SkippedFramesEntry)chatdbg_config)chatdbg_was_calledprint_exit_message)command_is_safezpanic_log.txtz(ChatDBG lldb) debuggerinternal_dictreturnc                 v    U R                  S[         S35        U R                  [        5        S[        l        g )Nzsettings set prompt ''md)HandleCommandPROMPTSetDestroyCallbackr   r   format)r   r   s     N/home/james-whalen/.local/lib/python3.13/site-packages/chatdbg/chatdbg_lldb.py__lldb_init_moduler      s1    26(!<= 23 N    r   commandresultc                 8    UR                  [        U5      5        g N)AppendMessager   r   r   r   r   s       r   _function_coder#      s     g'r   
definitionc                 N    UR                  [        R                  " U5      5        g r    )r!   r   native_definitionr"   s       r   _function_definitionr'   )   s     /AA'JKr   chatwhyc                      [        [        U 5      nUR                  U5        g ! [         a$  nUR	                  [        U5      5         S nAg S nAff = fr    )
LLDBDialogr   dialog	ExceptionSetErrorstr)r   r   r   r   r,   es         r   r(   r(   3   sB     FH-g  A s   !$ 
AAAconfigc                 r    UR                  5       n[        R                  " U5      nUR                  U5        g r    )splitr   parse_only_user_flagsr!   )r   r   r   r   argsmessages         r   r1   r1   B   s-     ==?D2248G
!r   c            	          ^  \ rS rSrSU 4S jjrS rS rS\4S jrS\	\
R                     4S jrS r SS	\S\	\\\\4         4S
 jjrS\	\
R(                     4S jrS rS rS rS rS rS\4S jrSrU =r$ )r+   N   r   c                 D   > [         TU ]  U5        [        5         X l        g r    )super__init__r   	_debugger)selfpromptr   	__class__s      r   r;   LLDBDialog.__init__P   s     !r   c                 @    UR                  5       R                  S5      $ )Nzis not a valid command.)stripendswith)r=   r6   s     r   _message_is_a_bad_command_error*LLDBDialog._message_is_a_bad_command_errorU   s    }}''(ABBr   c                     U R                   R                  5       n[        R                  " 5       nUR	                  X5        UR                  5       (       a  UR                  5       $ UR                  5       $ r    )r<   GetCommandInterpreterlldbSBCommandReturnObjectr   	Succeeded	GetOutputGetError)r=   r   interpreterr   s       r   _run_one_commandLLDBDialog._run_one_commandX   s[    nn::<++-!!'2##%%??$$r   c                     U R                   R                  5       nU(       d  gUR                  5        H;  nUR                  5        H$  nU H  nUR	                  5       S:  d  M        g   M&     M=     g)z5Returns False if not compiled with debug information.Fr   T)r<   GetSelectedTargetmodule_itercompile_unit_iterGetLine)r=   targetmodulecu
line_entrys        r   _is_debug_buildLLDBDialog._is_debug_buildb   sc    113((*F..0"$J!))+a/# #% 1 +
 r   c                     U R                  5       nU(       d  gU H;  nUR                  5       nU[        R                  [        R                  4;  d  M9  Us  $    W$ )z
Returns a currently stopped thread in the debugged process.
:return: A currently stopped thread or None if no thread is stopped.
N)_get_processGetStopReasonrH   eStopReasonNoneeStopReasonInvalid)r=   processthreadreasons       r   
get_threadLLDBDialog.get_threadn   sU    
 ##%F))+Fd22D4K4KLL  r   c                 Z   U R                   R                  5       (       d  U R                  S5        O&U R                  5       (       d  U R                  S5        U R	                  5       nU(       d  U R                  S5        [
        R                  " 5       (       d  U R                  S5        g g )Nz5Must be attached to a program to use `why` or `chat`.zSYour program must be compiled with debug information (`-g`) to use `why` or `chat`.z&must run the code first to use `chat`.zR`clangd` was not found. The `find_definition` function will not be made available.)r<   rQ   failrY   rc   r   is_availablewarn)r=   ra   s     r   check_debugger_stateLLDBDialog.check_debugger_state|   s    ~~//11IIMN%%''IIe "II>?%2244IId 5r   max_entriesc           
         U R                  5       nU(       d  g Sn/ nSnU GH1  nUS-  nUR                  5       (       d  US-  nM%  UR                  5       R                  S5      S   n/ n[        UR	                  5       R                  5       R                  5       R                  5       5       H  n	UR                  UR	                  5       R                  U	5      5      n
U
(       d  UR                  [        SSS5      5        MV  UR                  [        U
R                  5       U
R                  5       U
R                  5       5      5        M     UR                  5       nUR!                  5       R"                  nUS :X  a  SnUR%                  5       nUR'                  [(        R*                  " 5       5      (       a  [(        R,                  R/                  U5      n[(        R,                  R1                  U5      (       d  US-  nGM  US:  a  UR                  [3        U5      5        SnUR                  [5        XWXU5      5        [7        U5      U:  d  GM2    O   US:  a:  UR                  [3        U5      5        [7        U5      U:  a  UR9                  S5        [;        U Vs/ s H*  n[=        U[2        5      (       a  UR?                  5       OSPM,     sn5      nU[7        U5      :  a  [=        US   [2        5      (       a/  [3        [7        U5      U-
  US   R?                  5       -   5      US'   OIUR                  [3        [7        U5      U-
  S-   5      5        [7        U5      U:  a  UR9                  S5        [;        U Vs/ s H*  n[=        U[2        5      (       a  UR?                  5       OSPM,     sn5      [7        U5      :X  d   eU$ s  snf s  snf )Nr      (z	[unknown]) rc   GetDisplayFunctionNamer3   rangeGetFunctionGetTypeGetFunctionArgumentTypesGetSizeFindVariableGetArgumentNameappendr	   GetTypeNameGetNameGetValueGetLineEntryGetFileSpecfullpathrT   
startswithosgetcwdpathrelpathexistsr   r
   lenpopsum
isinstancecount)r=   rk   ra   skipped	summariesindexframename	argumentsjargrX   	file_pathlinenostotal_summary_counts                   r   _get_frame_summariesLLDBDialog._get_frame_summaries   sQ    "JL	EQJE//111//177<Q?D.0I!!#++-FFHPPR (():):)<)L)LQ)OP$$&{KM   "3??#4ckkmS\\^T ++-J"..099ID '	'')F ##BIIK00GGOOI6	 77>>),,1 {  !4W!=> "5	fM 9~,a d Q;09:9~+b!!MVWY*Q(;<<QWWY!CYW
 V,)B-)<== 3K"55	"8K8K8MM!	"   'F6I(IA(MN y>K/MM"%MVWY*Q(;<<QWWY!CYW
[ 	  ' X  Xs    1O	 1Oc                 `    U R                   R                  5       nU(       a  UR                  $ S$ )z
Get the process that the current target owns.
:return: An lldb object representing the process (lldb.SBProcess) that this target owns.
N)r<   rQ   r`   )r=   rU   s     r   r\   LLDBDialog._get_process   s'    
 113!'v~~1T1r   c                     U R                  5       nU(       a  UR                  S5      OS nU(       a  U$ U R                  S5        g )Ni   z$could not generate an error message.)rc   GetStopDescriptionrh   )r=   ra   error_messages      r   _initial_prompt_error_message(LLDBDialog._initial_prompt_error_message   s;    ";A11$7t  II<=r   c                    U R                   R                  5       R                  5       n[        R                  R                  UR                  5       UR                  5       5      nUR                  [        R                  " 5       5      (       a=  [        R                  R                  S[        R                  R                  U5      5      n[        U R                   R                  5       R                  5       R                  5       5       Vs/ s H:  nU R                   R                  5       R                  5       R                  U5      PM<     nnSR                  U/UQ5      nU(       a  U$ U R                  S5        g s  snf )N. z/could not retrieve the command line invocation.)r<   rQ   GetExecutabler   r   joinGetDirectoryGetFilenamer   r   r   rr   GetLaunchInfoGetNumArgumentsGetArgumentAtIndexrh   )r=   
executableexecutable_pathicommand_line_argumentscommand_line_invocations         r   _initial_prompt_command_line'LLDBDialog._initial_prompt_command_line   s'   ^^557EEG
'',,##%z'='='?
 %%biik22 ggll30PQO 002@@BRRT"
 NN,,.<<>QQRST 	 "
 #&((O+U>T+U"V"**IIGH"
s   AE>c                    [         R                  " 5       nU R                  R                  S5      R	                  U5        [
        R                  " UR                  5       5      nU(       a  UOS nU(       a(   [        USSS9 nUR                  5       nUsS S S 5        $ g ! , (       d  f       g = f! [         a    U R                  S5         g f = f)Nztarget.input-pathrignore)errorsz"could not retrieve the input data.)rH   SBStreamr<   
GetSetting	GetAsJSONjsonloadsGetDataopenreadFileNotFoundErrorrh   )r=   streamentry
input_pathfileinput_contentss         r   _initial_prompt_input LLDBDialog._initial_prompt_input  s    !!"56@@H

6>>+,#U
*c(;t%)YY[N) <; ;; % 		>?s0   7B0 B	B0 
B-)B0 -B0 0CCc                     g)z:Anything more beyond the initial error message to include.N r=   s    r   _initial_prompt_error_details(LLDBDialog._initial_prompt_error_details  s    r   c                     g)zZ
Return a simple backtrace to show the LLM where we are on the stack
in followup prompts.
Nr   r   s    r   _prompt_stackLLDBDialog._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 an LLDB command on the stopped program and gets the response.",
    "parameters": {
        "type": "object",
        "properties": {
            "command": {
                "type": "string",
                "description": "The LLDB command to run, possibly with arguments."
            }
        },
        "required": [ "command" ]
    }
}
Tz	Command `z` is not allowed.)r   unsafer   _unsafe_cmdrN   )r=   r   s     r   	llm_debugLLDBDialog.llm_debug*  sH    " $$_W-E-E#Diy0ABBB--g666r   )r<   r   )r   N)   )__name__
__module____qualname____firstlineno__r;   rD   rN   boolrY   r   rH   SBThreadrc   ri   intr   r   r
   r   r   	SBProcessr\   r   r   r   r   r   r/   r   __static_attributes____classcell__)r?   s   @r   r+   r+   N   s    "
C%
 
HT]]3 & "$WW	$u/1DDEF	GWr2ht~~6 2.7 7 7r   r+   )%r   r   typingr   r   r   rH   chatdbg.native_utilr   chatdbg.native_util.coder   chatdbg.native_util.dbg_dialogr   chatdbg.native_util.stacksr	   r
   r   chatdbg.util.configr   chatdbg.util.exit_messager   r   chatdbg.native_util.safetyr   RUST_PANIC_LOG_FILENAMEr   
SBDebuggerdictr   r   r/   rI   r#   r'   r(   r1   r+   r   r   r   <module>r      s    	 ( (  6 ) 4 
 / L 6 * 	! ! !$ ! f(oo(( &&( 	(
 
( ( lLooLL &&L 	L
 
L L fe
 oo
 
  &&
  	
   
  h"oo"" &&" 	" "p7 p7r   