o
    vf                    @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlmZ ejdkrndZddlmZ ddlmZ ddlmZ eef nd	ZddlmZ ddlmZ e Zddl!Z!ddl"Z"ddl#Z"dd
l$m%Z& ddl'm(Z( ej)*ddZ+de+ Z,de+ Z-dZ.dZ/dZ0dZ1g dZ2dd Z3d+ddZ4d+ddZ5dd Z6d+ddZ7dd  Z8d!d" Z9d#d$ Z:d%d& Z;d'd( Z<G d)d* d*e!j=Z>dS ),z;Representation of and data collection for a problem report.    N)
ExpatError3F)URLError)urlopen)unquoteT)impl)kill_pkttyagentAPPORT_DATA_DIRz/usr/share/apportz%s/package-hooks/z%s/general-hooks/z/optz~/.apport-ignore.xmlz/etc/apport/blacklist.dz/etc/apport/whitelist.d)shbashdashcshtcshzpython*zruby*phpzperl*zmono*awkc                 C   sT   zt |  W n
 ty   Y dS w t | D ]}||vr'|| t|| qdS )z7Recursively add dependencies of package to depends_set.N)	packagingget_version
ValueErrorget_dependenciesadd_transitive_dependencies)packagedepends_setd r   //usr/lib/python3/dist-packages/apport/report.pyr   9   s   

r   c                 C   s,   t s|durtj| |dS td|| f S )zrUse readlink() to resolve link.

    Return a string representing the path to which the symbolic link points.
    Ndir_fd/proc/%s/%s)_python2osreadlink)pathpidr   r   r   r   _read_proc_linkF   s   r$   c              
   C   s   z>t s|durtj| tjtjB |d}nd|| f }t|d}|  jdddW  d   W S 1 s7w   Y  W dS  t	t
fyX } zdt| W  Y d}~S d}~ww )	zXRead file content.

    Return its content, or return a textual error if it failed.
    Nr   r   rbUTF-8replaceerrorsError: )r   r    openO_RDONLY	O_CLOEXECioreadstripdecodeOSErrorIOErrorstr)r"   r#   r   	proc_filefder   r   r   _read_proc_fileQ   s   (r8   c              
      s   d}t rdS z%td fddd}|  }W d   W |S 1 s$w   Y  W |S  ttfyE } zdt| W  Y d}~S d}~ww )	zRead /proc/pid/maps.

    Since /proc/$pid/maps may become unreadable unless we are ptracing the
    process, detect this, and attempt to attach/detach.
    z%Error: unable to read /proc maps filezDError: python2 does not provide a secure way to read /proc maps filemapsc                    s   t j| | dS )Nr   )r    r+   )r"   modeproc_pid_fdr   r   <lambda>n   s    z_read_maps.<locals>.<lambda>)openerNr*   )r   r+   r/   r0   r2   r3   r4   )r<   r9   r6   r7   r   r;   r   
_read_mapsb   s   r?   c                 C   s   t j| t jt j|d}z|j|ddd }W n t jy4   |  | \}}tdt| |j	|f w |j	dkr<|S |rF|j
ddd}nd	}td
t| |j	|f )zRun command and capture its output.

    Try to execute given command (argv list) and return its stdout, or return
    a textual error if it failed.
    )stdoutstderrenvi  )inputtimeoutr   z0Error: command %s timedout with exit code %i: %sr&   r'   r(    z.Error: command %s failed with exit code %i: %s)
subprocessPopenPIPESTDOUTcommunicateTimeoutExpiredkillr2   r4   
returncoder1   )commandrC   rB   spouterrsr   r   r   _command_outputu   s(   

rR   c              	   C   sT  t r|jds
dS nd|jvrdS |jD ]}|jtjjjkr|j	dkrz|jd j
}W n	 ty5   Y qw || vr= dS |  | r|jd jtjjjkr|jd j
}t r]|d}| | }t|tjrs| }t sr|d}nt|trt s|d}zt|}W n	 ty   Y qw ||s dS qt r|jd j
dS |jd j
S )zxCheck if given report matches the given bug pattern XML DOM node.

    Return the bug URL on match, otherwise None.
    urlNrekeyr   r&   )r   
attributeshas_key
childNodesnodeTypexmldomNodeELEMENT_NODEnodeName	nodeValueKeyError	normalizehasChildNodes	TEXT_NODEencode
isinstanceproblem_reportCompressedValue	get_valuebytesrT   compile	Exceptionsearch)reportpatterncrU   regexpvre_cr   r   r   _check_bug_pattern   sP   






rs   c              	   C   sd   zt r|d}tjj|}W n ttfy   Y d S w |dD ]}t	| |}|r/|  S q"d S )Nr&   rn   )
r   rd   rZ   r[   minidomparseStringr   UnicodeEncodeErrorgetElementsByTagNamers   )rm   patternsr[   rn   rS   r   r   r   _check_bug_patterns   s   

ry   c                 C   sJ   | j D ]}|jtjjjkr|j dkr|  | 	| qt
| qdS )z6Recursively remove whitespace from given XML DOM node.rE   N)rX   rY   rZ   r[   r\   rc   r_   r0   unlinkremoveChild_dom_remove_space)nodero   r   r   r   r|      s   

r|   c              
   C   s  t j|sdS i }zNt|}tt| |d| W d    n1 s%w   Y  z|d | | W W dS  tyX } zt|	drK|d |  n W Y d }~W dS d }~ww  t
yb   Y dS  ty   t jt j|d dd}t | d	| < td
| t  Y dS w )NFexecadd_infoz
add_info()Tr   -_
HookError_zhook %s crashed:)r    r"   existsr+   r~   rj   r/   	TypeErrorr4   
startswithStopIterationrk   splitextbasenamer'   	traceback
format_excapporterror	print_exc)rm   uihooksymbr6   r7   hooknamer   r   r   	_run_hook   s6   
 
r   c                 C   s   |r$t j }|ddt j}|t j|dd< t j||d< nd}ztjd| g|d	 
 W S  tjy?   Y dS w )z-Return path of command, preferring extra_pathPATHrE   r   NwhichrB   )r    environcopygetsplitpathsepjoinrF   check_outputr1   r0   CalledProcessError)rN   
extra_pathrB   partsr   r   r   _which_extrapath   s   
r   c                   @   s>  e Zd ZdZdFddZdd Zdd	 ZdGd
dZdd Zdd Z	dd Z
dd Zdd Zedd Zddg fddZdg dfddZdGddZdHddZd d! ZdHd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!dGd>d?Z"d@dA Z#dBdC Z$e%dHdDdEZ&dS )IReportzA problem report specific to apport (crash or bug).

    This class wraps a standard ProblemReport and adds methods for collecting
    standard debugging data.CrashNc                 C   s    t j| || d| _d| _dS )a,  Initialize a fresh problem report.

        date is the desired date/time string; if None (default), the current
        local time is used.

        If the report is attached to a process ID, this should be set in
        self.pid, so that e. g. hooks can use it to collect additional data.
        N)rf   ProblemReport__init__r#   _proc_maps_cache)selftypedater   r   r   r     s   	
zReport.__init__c                 C   sx   d}t |}|r|dd| 7 }zt |s/t |}|r(|d| 7 }W |S |d7 }W |S W |S  ty;   Y |S w )zReturn a string suitable for appending to Package/Dependencies.

        If package has only unmodified files, return the empty string. If not,
        return ' [modified: ...]' with a list of modified files.
        rE   z [modified: %s] z [origin: %s]z [origin: unknown])r   get_modified_filesr   is_distro_packageget_package_originr   )r   r   suffixmodoriginr   r   r   _customized_package_suffix  s$   




z!Report._customized_package_suffixc                 C   sF   zt |}W n ty   d}Y nw d||pd| |f | d< |S )a  Add Package: field

        Determine the version of the given package (uses "(not installed") for
        uninstalled packages) and add Package: field to report.
        This also checks for any modified files.

        Return determined package version (None for uninstalled).
        N%s %s%sz(not installed)Package)r   r   r   r   )r   r   versionr   r   r   add_package6  s   	

zReport.add_packagec              
   C   s  |sd| vr| d dkr| d }nt j| d }|sdS | |}|s(d| vr;z	t|| d< W n	 ty:   Y nw |s?dS t|| d< tg }t	|| d| d	< t
|D ]/}zt|}W n	 tyi   Y qWw | d	 rv| d	  d
7  < | d	  d||| |f 7  < qWdS )a  Add packaging information.

        If package is not given, the report must have ExecutablePath.
        This adds:
        - Package: package name and installed version
        - SourcePackage: source package name (if possible to determine)
        - PackageArchitecture: processor architecture this package was built
          for
        - Dependencies: package names and versions of all dependencies and
          pre-dependencies; this also checks if the files are unmodified and
          appends a list of all modified files
        ExecutablePathProblemTypeKernelCrashr   NSourcePackagePackageArchitecturerE   Dependencies
r   )r   	fileutilsfind_file_packager   r   
get_sourcer   get_architecturesetr   sortedr   r   )r   r   r   dependenciesdeprq   r   r   r   add_package_infoI  s>   



zReport.add_package_infoc                 C   s  d| d| d| ddf | d< | d}tddd	|g| d
< tdd|g| d< tddd	|g| d| < ddl}td| }||}W d   n1 sQw   Y  d|v rj|d }tddd	|g| d| < g }d|v r|d D ]}|d |  d}	|	r|	|vr||	 qt|D ]}
tddd	|
g| d|
 < q| drd}t|t| dd}|r|	dr|	d| d< |	dr|	d| d< dS dS dS dS dS )zAdd info about an installed Snap

        This adds a Snap: field, containing name, version and channel.
        It adds a SnapSource: field, if the snap has a Launchpad contact defined.
        z
%s %s (%s)namer   channelunknownSnapsnapchangesz
--abs-timezSnap.ChangesconnectionszSnap.ConnectionsinfozSnap.Info.%sr   Nz/snap/%s/current/meta/snap.yamlbaseplugszdefault-providercontactzX^https?:\/\/.*launchpad\.net\/((?:[^\/]+\/\+source\/)?[^\/]+)(?:.*field\.tags?=([^&]+))?rE      
SnapSource   SnapTags)
r   rR   yamlr+   	safe_loadappendrT   rl   r   group)r   r   snapnamer   f	snap_metar   	providersplugdpproviderpmr   r   r   add_snap_info~  sL   







zReport.add_snap_infoc                 C   sd   d| vrdt j  | d< d| vr$t }d|d |d |d f | d< d| vr0t | d< d	S d	S )
a  Add operating system information.

        This adds:
        - DistroRelease: NAME and VERSION from /etc/os-release, or
          'lsb_release -sir' output
        - Architecture: system architecture in distro specific notation
        - Uname: uname -srm output
        DistroReleasez%s %sUnamez%s %s %sr   r      ArchitectureN)r   r   get_os_versionr    unameget_system_architecture)r   ur   r   r   add_os_info  s   	zReport.add_os_infoc                    sR   t t d   fddt D }|  |r#d|| d< dS d| d< dS )zoAdd information about the user.

        This adds:
        - UserGroups: system groups the user is in
        r   c                    s(   g | ]\}}}} |v r|d k r|qS )i  r   ).0r   r   gidmembuserr   r   
<listcomp>  s    z(Report.add_user_info.<locals>.<listcomp>r   
UserGroupszN/AN)pwdgetpwuidr    geteuidgrpgetgrallsortr   )r   groupsr   r   r   add_user_info  s   zReport.add_user_infoc              	      sx  d| vrdS t j| d  tt fddtsdS d}| d  D ]}z
|dd\}}W n	 ty8   Y q#w |dkrA|} nq#|sFdS | d	 d
}g d}t	|dkr|d 
dr|
dr|d dkrt	|dkr| |d }|r| d | d< || d< dS d|d  | d< dS |d= t	|dkr|d 
ds^t	|dkr|d 
drd| v rt j| d |d |d< t |d t jr| d | d< t j|d | d< d| vr| kr|D ]-}t ||d  t jr||d  }	t jt j|	|kr| d | d< |	| d<  nqd| v r8t j| d dkr:| d | d< |  }
|
r2|
| d< dS d| d< dS dS dS )zCheck if process is a script.

        Use ExecutablePath, ProcStatus and ProcCmdline to determine if
        process is an interpreted script. If so, set InterpreterPath
        accordingly.
        r   Nc                    s   t   | S N)fnmatch)iexebasenamer   r   r=     s    z+Report._check_interpreted.<locals>.<lambda>
ProcStatus	r   zName:ProcCmdline )z	/usr/bin/z
/usr/sbin/z/bin/z/sbin/r   r   pythonz-m   InterpreterPathz)Cannot determine path of python module %sUnreportableReason.ProcCwdr   twistdz&Cannot determine twistd client program)r    r"   r   anyfilterinterpreters
splitlinesr   r   lenr   _python_module_pathr   accessR_OKrealpath_twistd_executable)r   r   linekrq   cmdargsbindirsr"   r   argvexeexer   r   r   _check_interpreted  sj   " 	zReport._check_interpretedc                 C   s   | d  ddd }|ro|d  dd}|d ds*|d d	s*|d d
r8t|dkr4|d S |d S t|d dkrh|d d dkrh|d d dkrh|d dd }d|v sdd|v sdd|v rh|d S |d |sdS )z5Determine the twistd client program from ProcCmdline.r  r  r   Nr   =r   z--filez--pythonz--sourcer   r   ys)r   r   r  pop)r   argsargoptsr   r   r   r    s   *0
zReport._twistd_executablec                 C   s8   zt j| }W n
 ty   Y dS w |du rdS |jS )z%Determine path of given Python moduleN)	importlibutil	find_specImportErrorr   )modulespecr   r   r   r  /  s   zReport._python_module_pathc              
   C   s  |sO|s| j p
t }| j st|| _ t|}tsOztd| tjtjB tj	B }W n$ t
yN } z|jtjtjfv r?td|jtjkrItd d}~ww z
td||| d< W n	 t
yb   Y nw | j|||d td||| d	< td
||d| d< t|| d< d| vrz
td||| d< W n$ t
y } z|jtjtjfv rtd|jtjkrtd d}~ww dD ]}| d d| r| d td| d | d<  nqtj| d std| d  |   ttt| d j| d< | d dddddd| d< zt dkr td||}|dkr || d< W n tt
fy-   Y nw | ||}|r?|d | d< dS dS )a  Add /proc/pid information.

        If neither pid nor self.pid are given, it defaults to the process'
        current pid and sets self.pid.

        This adds the following fields:
        - ExecutablePath: /proc/pid/exe contents; if the crashed process is
          interpreted, this contains the script path instead
        - InterpreterPath: /proc/pid/exe contents if the crashed process is
          interpreted; otherwise this key does not exist
        - ExecutableTimestamp: time stamp of ExecutablePath, for comparing at
          report time
        - ProcEnviron: A subset of the process' environment (only some standard
          variables that do not disclose potentially sensitive information, plus
          the ones mentioned in extraenv)
        - ProcCmdline: /proc/pid/cmdline contents
        - ProcStatus: /proc/pid/status contents
        - ProcMaps: /proc/pid/maps contents
        - ProcAttrCurrent: /proc/pid/attr/current contents, if not "unconfined"
        - CurrentDesktop: Value of $XDG_CURRENT_DESKTOP, if present
        - _LogindSession: logind cgroup path, if present (Used for filtering
          out crashes that happened in a session that is not running any more)
        /proc/%sznot accessiblezinvalid processNcwdr  )r#   r<   extraenvstatusr   cmdliner  r  ProcMapsr   r  )rofsrwfs	squashmnt
persistmntz/%s/z/%sz%s does not existExecutableTimestamp\z\\r   z\ r   zattr/current
unconfinedProcAttrCurrent_LogindSession) r#   r    getpidintr4   r   r+   r,   O_PATHO_DIRECTORYr2   errnoEPERMEACCESr   ENOENTr$   add_proc_environr8   rstripr?   r   r  r"   r   r  statst_mtimer'   getuidr3   get_logind_session)r   r#   r<   r*  r7   r   valretr   r   r   add_proc_info:  sx   
"$
zReport.add_proc_infoc                 C   s  g d| }|s#|st  }t|}ts#t d| t jt jB t jB }d| d< td||	dd}|
dr<|| d< dS |d	D ]}|d
dd |v rb| d rY| d  d7  < | d  |7  < qA|
dr|d
dd }d|v swd|v r| d r| d  d7  < | d  d7  < qA|dkr| d r| d  d7  < | d  d7  < qA|
dr| d r| d  d7  < | d  d7  < qA|
dr| d r| d  d7  < | d  d7  < qA|
dr| d r| d  d7  < | d  d7  < qA|
dr|d
dd | d< qAdS )a  Add environment information.

        If pid is not given, it defaults to the process' current pid.

        This adds the following fields:
        - ProcEnviron: A subset of the process' environment (only some standard
          variables that do not disclose potentially sensitive information, plus
          the ones mentioned in extraenv)
        - CurrentDesktop: Value of $XDG_CURRENT_DESKTOP, if present
        )SHELLTERMLANGUAGELANGLC_CTYPE
LC_COLLATELC_TIME
LC_NUMERICLC_MONETARYLC_MESSAGESLC_PAPERLC_NAME
LC_ADDRESSLC_TELEPHONELC_MEASUREMENTLC_IDENTIFICATIONLOCPATHr(  rE   ProcEnvironr   r   \nzError:r  r  r   r   zPATH=z/homez/tmpzPATH=(custom, user)zG/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gameszPATH=(custom, no user)zXDG_RUNTIME_DIR=zXDG_RUNTIME_DIR=<set>zLD_PRELOAD=zLD_PRELOAD=<set>zLD_LIBRARY_PATH=zLD_LIBRARY_PATH=<set>zXDG_CURRENT_DESKTOP=CurrentDesktopN)r    r7  r4   r   r+   r,   r9  r:  r8   r'   r   r   )r   r#   r*  r<   	safe_varsrB   r  r   r   r   r   r?    sZ   




zReport.add_proc_environc           
   	   C   s*  d| vrdS d}d}zt | d dr't \}}t|| d  t| d}| d  d }dd	| |g}ztj|tj	tj	tj
d
}W n tyX   Y W |rVt| dS dS w |jd |jd |jd |jd |j }	| dk}|r|	| d< W |rt| |S |rt| w w )zWAdd information from kernel crash.

        This needs a VmCore in the Report.
        VmCoreNFfindTr   r   crashz/usr/lib/debug/boot/vmlinux-%s)stdinr@   rA   z	bt -a -f
zps
zrunq
zquit
r   
Stacktrace)hasattrtempfilemkstempr    writecloser   rF   rG   rH   rI   r2   rz   r`  r@   r/   wait)
r   debugdirunlink_corerF  r6   corekverrN   r   rP   r   r   r   add_kernel_crash_info  sT   



zReport.add_kernel_crash_infoc                 C   s  d| vsd| vr
dS dddddd	d
d}|  ||\}}|s)ttjttjd|g d7 }g }| D ]\}}|| |ddd|g7 }q5|d |ddg7 }t||dj	ddd}	d|	v sed|	v rd|	v rvd
dd |	 D }
nd|	v rd
dd |	 D }
n|	 d }
d|
  }|| d< t||	dd d rttjttjd!td"tj}||	}|d |D ]}|d#d$ | |d< qd%| v rd&| d% v r| d% | d'< | d%= d(| v r| d( d)r| d( | d'< | d(= d'| v r'td*| d' }|r$|d+| d'< | d' d,r#| d' dd- | d'< n| d'= d.| v r=|   |  }|r?|| d/< dS dS dS )0a<  Add information from gdb.

        This requires that the report has a CoreDump and an
        ExecutablePath. This adds the following fields:
        - Registers: Output of gdb's 'info registers' command
        - Disassembly: Output of gdb's 'x/16i $pc' command
        - Stacktrace: Output of gdb's 'bt full' command
        - ThreadStacktrace: Output of gdb's 'thread apply all bt full' command
        - StacktraceTop: simplified stacktrace (topmost 5 functions) for inline
          inclusion into bug reports and easier processing
        - AssertionMessage: Value of __abort_msg, __glib_assert_msg, or
          __nih_abort_msg if present

        The optional rootdir can specify a root directory which has the
        executable, libraries, and debug symbols. This does not require
        chroot() or root privileges, it just instructs gdb to search for the
        files there.

        Raises a IOError if the core dump is invalid/truncated, or OSError if
        calling gdb fails, or FileNotFoundError if gdb or the crashing
        executable cannot be found.
        CoreDumpr   Nzinfo registersz	x/16i $pczbt fullzthread apply all bt fullzprint __abort_msg->msgzprint __glib_assert_msgzprint (char*) __nih_abort_msg)	RegistersDisassemblyra  ThreadStacktraceAssertionMessageGLibAssertionMessageNihAssertionMessagezgdb not found in retracing env)z--batch--exzset backtrace limit 2000rt  zp -99	separatorr   r&   r'   r(   z%is truncated: expected core file sizezis not a core dumpwarning:r   c                 S      g | ]}d |v r|qS )rv  r   r   r  r   r   r   r   0      z'Report.add_gdb_info.<locals>.<listcomp>Warning:c                 S   rw  )rz  r   rx  r   r   r   r   3  ry  r   zInvalid core dump: r  zNo such file or directory.z&executable file for coredump not foundz^\$\d+\s*=\s*-99$z

z
.
rr  z"ERROR:rq  rs  $z(^\$\d+\s+=\s+0x[0-9a-fA-F]+\s+"(.*)"\s*$r   rZ  ra  StacktraceAddressSignature)gdb_commandFileNotFoundErrorr;  r>  r    strerroritemsr   rR   r1   r   r  r0   r3   r   endswithrT   rj   	MULTILINEr  r'   r   matchr   _gen_stacktrace_topcrash_signature_addresses)r   rootdirgdb_sandboxgdb_reportsgdb_cmdr   
value_keysr   cmdrP   warningsreasonpart_rer   partr   addr_signaturer   r   r   add_gdb_info  s   





zReport.add_gdb_infoc                 C   sd  t g d}dgd }d}d}d}d}td}td}td}	| d	  D ]~}
||
}|s9||
}|s9q(|r=|r|d
rQ|d
 d dd }nd}|ra|ds^|dv r_q(d}|dsj||v rd}d}dgd }|drzd}|dkrd}q(d}|d
p|d}| d }|t|k r|	|s|||< |d7 }q(d	|
 | d< dS )zBuild field StacktraceTop as the top five functions of Stacktrace.

        Signal handler invocations and related functions are skipped since they
        are generally not useful for triaging and duplicate detection.
        )g_logvg_log	IA__g_log
IA__g_logvg_assert_warningIA__g_assert_warning
__GI_abort_XErrorrE      r   FzD^#(\d+)\s+(?:0x(?:\w+)\s+in\s+\*?(.*)|(<signal handler called>)\s*)$z0^#(\d+)\s+(?:(.*)|(<signal handler called>)\s*)$z,^(__.*_s?sse\d+(?:_\w+)?|__kernel_vsyscall)$ra  r   (N_X)handle_responsehandle_errorXWindowEventr  Tr  r   r   StacktraceTop)r   rT   rj   r  r  r   r   r   r  r   r0   )r   unwind_functionstoptracedepthunwound	unwindingunwinding_xerrorbt_fn_rebt_fn_noaddr_reignore_functions_rer  r   fnframefunctionr   r   r   r  e  sP   








zReport._gen_stacktrace_topc                 C   s   |  |||}t  |S )a  Run hook script for collecting package specific data.

        A hook script needs to be in _hook_dir/<Package>.py or in
        _common_hook_dir/*.py and has to contain a function 'add_info(report,
        ui)' that takes and modifies a Report, and gets an UserInterface
        reference for interactivity.

        return True if the hook requested to stop the report filing process,
        False otherwise.
        )_add_hooks_infor   )r   r   r   
srcpackagerF  r   r   r   add_hooks_info  s   zReport.add_hooks_infoc           
   	   C   s  |s|  d}|r| d }d|v rd| | d< d S |s"|  d}|r6| d }d|v r6d| | d< d S tg}d }tj|  dd	}|trM|}n&|rsztj	
|D ]}|trgtj|rg|} nqVW n	 tyr   Y nw |rt|ttkr|tj|d
dd tj|}t|ttks}ttd D ]}t| ||r dS q|r|D ]}	t| |tj|	|d r dS q|r|D ]}	t| |tj|	d| r dS qdS )Nr   r   /zinvalid Package: %sr  r   zinvalid SourcePackage: %sr   rE   sharer   zpackage-hooksz/*.pyTz.pyzsource_%s.pyF)r   r   	_hook_dirr    r"   r  r   _opt_dirr   r   	get_filesisfiler   r  r   r   dirnameglob_common_hook_dirr   )
r   r   r   r  	hook_dirsopt_path	exec_pathr   r   hook_dirr   r   r   r    sb   


zReport._add_hooks_infoc              	   C   sj   |sdS zt |}| jddd}|  W n ttfy#   Y dS w d|v r*dS t| |}|r3|S dS )a  Check bug patterns loaded from the specified url.

        Return bug URL on match, or None otherwise.

        The url must refer to a valid XML document with the following syntax:
        root element := <patterns>
        patterns := <pattern url="http://bug.url"> *
        pattern := <re key="report_key">regular expression*</re> +

        For example:
        <?xml version="1.0"?>
        <patterns>
            <pattern url="http://bugtracker.net/bugs/1">
                <re key="Foo">ba.*r</re>
            </pattern>
            <pattern url="http://bugtracker.net/bugs/2">
                <re key="Package">^\S* 1-2$</re> <!-- test for a particular version -->
                <re key="Foo">write_(hello|goodbye)</re>
            </pattern>
        </patterns>
        Nr&   r'   r(   z<title>404 Not Found)r   r/   r1   rf  r3   r   ry   )r   rS   r   rx   r   r   r   search_bug_patterns  s   
zReport.search_bug_patternsc           	   
   C   sN  t t d }td|}d}d}d}zBz"t|tjtjB }t	|}t
|jr7t|d}|d}W n ttfyC   Y nw W |durN|  n|durWt| n|dura|  w |durkt| w w |dkr{tj ddd}n z	tjj|}W n ty } z
tdtt|f d}~ww |j  t|j |S )	zRead ignore list XML file and return a DOM tree.

        Return an empty DOM tree if file does not exist.

        Raises ValueError if the file exists but is invalid XML.
        r  ~rE   NriP  r   z%s has invalid format: %s)r   r   r    r   _ignore_filer'   r+   
O_NOFOLLOWr,   fstatrA  S_ISREGst_modefdopenr/   r3   r2   rf  rZ   r[   getDOMImplementationcreateDocumentrt   ru   r   r   r4   documentElementra   r|   )	r   homedirifpathcontentsr6   r   str[   r7   r   r   r   _get_ignore_dom  sJ   






zReport._get_ignore_domc              	   C   s  d| v sJ zGt tD ]?}z4tt jt|!}|D ]}| | d kr1 W d   W  W dS qW d   n1 s<w   Y  W q tyK   Y qw W n	 tyV   Y nw zHt	 }t t
D ]2}z'tt jt
|}|D ]	}||  qoW d   n1 sw   Y  W q` ty   Y q`w |r| d |vrW dS W n	 ty   Y nw z|  }W n ttfy   td t  Y dS w ztt | d j}W n
 ty   Y dS w z!|dD ]}|d| d krt|d|kr W dS qW dS  ttfy
   Y dS w )	a\  Check if current report should not be presented.

        Reports can be suppressed by per-user blacklisting in
        ~/.apport-ignore.xml (in the real UID's home) and
        /etc/apport/blacklist.d/. For environments where you are only
        interested in crashes of some programs, you can also create a whitelist
        in /etc/apport/whitelist.d/, everything which does not match gets
        ignored then.

        This requires the ExecutablePath attribute. Throws a ValueError if the
        file has an invalid format.

        Privileges may need to be dropped before calling this.
        r   NTzCould not get ignore file:Fignoreprogrammtime)r    listdir_blacklist_dirr+   r"   r   r0   r3   r2   r   _whitelist_dirr   r  r   r`   r   r   r   r   r8  rA  rB  rw   getAttributefloat)r   r   r6   r  	whitelistr[   	cur_mtimer  r   r   r   check_ignoredE  s~   
zReport.check_ignoredc              
   C   s.  d| v sJ |   }zttt| d j}W n ty2 } z|jtjkr-W Y d}~dS  d}~ww |	dD ]}|
d| d krK|d|  nq8|d}|d| d  |d| |j| tt d }td|}t|d}|j|d	d
d W d   n1 sw   Y  |  dS )a~  Ignore future crashes of this executable.

        Add a ignore list entry for this report to ~/.apport-ignore.xml, so
        that future reports for this ExecutablePath are not presented to the
        user any more.

        Throws a ValueError if the file already exists and has an invalid
        format.

        Privileges may need to be dropped before calling this.
        r   Nr  r  r  r  r  wz  r   )	addindentnewl)r  r4   r8  r    rA  rB  r2   r;  r>  rw   r  setAttributecreateElementr  appendChildr   r   r   r  r'   r+   writexmlrz   )r   r[   r  r7   r  r  ignore_file_pathr6   r   r   r   mark_ignore  s2   
zReport.mark_ignorec                 C   sT   |  dsdS dd | d  D }t|dk r|ddkS |dt|d kS )	a  Check whether StackTrace can be considered 'useful'.

        The current heuristic is to consider it useless if it either is shorter
        than three lines and has any unknown function, or for longer traces, a
        minority of known functions.
        r  Fc                 S   s   g | ]}| d qS )??)r   )r   r   r   r   r   r     s    z0Report.has_useful_stacktrace.<locals>.<listcomp>r  Tr   g       @)r   r  r  count)r   
unknown_fnr   r   r   has_useful_stacktrace  s   
zReport.has_useful_stacktracec                 C   s<   |  dd D ]}|dd  }|dkr|  S qdS )z(Return topmost function in StacktraceTopr  rE   r  r   r  N)r   r  r   r0   )r   r  fnamer   r   r   stacktrace_top_function  s   zReport.stacktrace_top_functionc              	   C   s\  |  ddkrd| v rd| v rdtj| d | d f S d| v rvd| v rvd| v rvddd	d
dd}|  }|r<d| }nd}d}d| v r\d| v r\| d | d kr\| d dkr\d| d  }dtj| d | |  dd|  d ||f S d| v rd| v r| d  }t|dk rdS t|dk rdtj| d |d f S td}t|d }d}|dkr|	|| }|r|
d}	|
d}n|d8 }|dkstj| d }
|d }|dd }t	d t| |}|r|
d}nd}|d!kr	|	| d krd"}n|	}nd#| }d$|
||f }|r|d%| 7 }|S |  d&d'krFd'| v rFd(| d'  }|  d)rD|d*| d)  d  7 }|S |  d&d+krqd,| v rq| d, }|d-rg|d.dd }|S |d.dd }|S |  d&d+krd/| v rd}d0| v r|d1| d0  d2 7 }|| d/ d3 7 }d4| v r|d5| d4  d6 7 }|d.7 }|S dS )7aZ  Create an appropriate title for a crash database entry.

        This contains the topmost function name from the stack trace and the
        signal (for signal crashes) or the Python exception (for unhandled
        Python exceptions).

        Return None if the report is not a crash or a default title could not
        be generated.
        Signal6r   rq  z%s assert failure: %sr  SIGILLSIGABRTSIGFPESIGSEGVSIGPIPE)4r  81113z in %s()rE   r   r   allz [non-native %s package]z%s crashed with %s%s%szsignal 	Tracebackr   Nr  z%s crashed with %sr   z^\s*File\s*"(\S+)".* in (.+)$r   r   :z
^%s: (.+)$<module>__main__z%s()z%s crashed with %s in %sz: %sr   r   z$package %s failed to install/upgradeErrorMessagez: 
KernelOopsOopsTextz$------------[ cut here ]------------r   FailureMachineType[z] z failureNonfreeKernelModulesz [non-free: ])r   r    r"   r   r  r  r  rT   rj   r  r   r   escaper   )r   signal_namesr  arch_mismatchtracetrace_rer   r  r   module_pathr"   	last_line	exceptionmessagecontexttitleoopsr   r   r   standard_title  s   
,





zReport.standard_titlec                 C   s   g }|  ddd |  dd  D ]-}|sq| dd \}}t|}|dur?|dkr?|dur?t||dk r?|| q|S )	z=Return list of obsolete packages in Package and Dependencies.r   rE   r   r   Nr   Noner   )r   r  r   r   get_available_versioncompare_versionsr   )r   obsoleter  pkgveravailr   r   r   obsolete_packagesW  s   $
(
zReport.obsolete_packagesc                 C   s  d| vr| d dvrdS d| v r8| d dkr8d}t d}| d  D ]}||}|r5|d	|d
 7 }q#|S | ddkrTd| v rT| d d	 | d  }t dd|S d| v rd| v rd| d | d f }t d}| d  }t|dk rydS |D ]}||}|r|d	|d
p|d 7 }q{ dS |S d| v r1| d  }d}t|d
krt d|d }|r| d d	 |d
 S dS t|dk rdS t d}|D ]7}||}|r|ddkr|d	|d 7 }q|d
}	tj	
|	rtj	|	}	|d|	|df 7 }q|d d	d }
z
|
d| d  7 }
W n
 ty&   Y nw | d d	 |
 | S | d dkrmd | v rmd!| d  v sKd"| d  v rm| d  }| d#r]|d$| d#  7 }| d%rk|d$| d%  7 }|S d&| v rd'}g }| d& d(D ]d}|d)rt d*|}|r|d
}|sJ d+| || |d,r| |}|r|| q}|d-rd.}q}|rd}|r|d d/kr| |}|r|| q}d'}q}|rd	|S dS )0a  Get a signature string for a crash.

        This is suitable for identifying duplicates.

        For signal crashes this the concatenation of ExecutablePath, Signal
        number, and StacktraceTop function names, separated by a colon. If
        StacktraceTop has unknown functions or the report lacks any of those
        fields, return None. In this case, you can use
        crash_signature_addresses() to get a less precise duplicate signature
        based on addresses instead of symbol names.

        For assertion failures, it is the concatenation of ExecutablePath
        and assertion message, separated by colons.

        For Python crashes, this concatenates the ExecutablePath, exception
        name, and Traceback function names, again separated by a colon.

        For suspend/resume failures, this concatenates whether it was a suspend
        or resume failure with the hardware identifier and the BIOS version, if
        it exists.
        r   r   )r   r  Nra  r   kernelz^\s*\#\d+\s\[\w+\]\s(\w+)r  r   r  r  rq  z0x[0-9a-f]{6,}ADDRr  z%s:%sz.^(?:([\w:~]+).*|(<signal handler called>)\s*)$r   r  rE   z(\w+): r   r  z)^\s+File "([^"]+).*line (\d+).*\sin (.*)$r  z:%s@%sr  z(%s)_PythonExceptionQualifierr  r  suspendresumer  z:%szdmi.bios.versionr  Fr   zBUG: unable to handlez^BUG: unable to handle (.*) at z.could not parse expected problem type line: %szIP: zCall Trace:Tr   )rT   rj   r  r  r   r   subr  r    r"   islinkr  r   r`   r   rl   r   _extract_function_and_addressr   )r   sigregexr  r   r  linesr  loc_rer   exc_namein_trace_bodyr   parsedr  r   r   r   crash_signatured  s   















zReport.crash_signaturec                 C   s>   t d|}|r|d}|sJ d| |d dkr|S d S )Nz\[.*\] (.*)$r   z,could not parse expected call trace line: %sr   ?)rT   rl   r   )r   r  r%  r  r   r   r   r    s   
z$Report._extract_function_and_addressc                 C   s  d| vsd| vsd| vrdS d| d v rdS g }d}| d   D ]:}|drR| d }|d	s3q t|d
}|dk r=q | |}|rN||dd n|d7 }t|dkrZ nq |dkrit|| dk ridS |dkrst|dk s}|dkrt|dk rdS d| d | d d|f S )a  Compute heuristic duplicate signature for a signal crash.

        This should be used if crash_signature() fails, i. e. Stacktrace does
        not have enough symbols.

        This approach only uses addresses in the stack trace and does not rely
        on symbol resolution. As we can't unwind these stack traces, we cannot
        limit them to the top five frames and thus will end up with several or
        many different signatures for a particular crash. But these can be
        computed and synchronously checked with a crash database at the client
        side, which avoids having to upload and process the full report. So on
        the server-side crash database we will only have to deal with all the
        equivalence classes (i. e. same crash producing a number of possible
        signatures) instead of every single report.

        Return None when signature cannot be determined.
        r-  ra  r  NzErrno 13r   #r   0x   i   r  z..   r   r     z%s:%s:%sr   )	r  r   r   r8  _address_to_offsetr   r'   r  r   )r   stackfailedr  addroffsetr   r   r   r    s<   



(z Report.crash_signature_addressesc           	   	   C   s  g }t  dkr\tt  }t|d dkr(|tdt	|d  df |tdt	|d  df |d 
dD ]}| }t|dkr[|td	t	| d
f q@t  d }t|dkrw|tdt	| df z| d= W n	 ty   Y nw | D ]I}|do|dv}|sd|v s|dv rt| | dsq|D ])\}}t| | tkr||| | jdddd| |< q||| | | |< qqdS )zRemove user identifying strings from the report.

        This particularly removes the user name, host name, and IPs
        from attributes which contain data read from the environment, and
        removes the ProcCwd attribute completely.
        r   r   z\b%s\busernamer  z/home/usernamer   ,z(\b|\s)%s\bz\1User Namer   hostnamer  Proc)ProcCpuinfor-  r   ProcInterruptsProcModulesra  )r  
PythonArgsTitleJournalErrorsisspacer&   r'   r(   N)r    rC  r   r   r   r  r   rT   rj   r  r   r0   r   r`   r   rb  r   ri   r  r1   rd   )	r   replacementsr   r  r4  r  is_proc_fieldrn   replr   r   r   	anonymize7  s<   ""
&zReport.anonymizec                 C   s*  d| v sJ |  d| d }d}d| v r| d t krd}|r(tj|ddnd}td	|}|s3d
S |g}d}|sKtd|}	|	rE|	g}ntj	d |rd}
|dd| dd| dd| dd||
||
f g7 }|rd|||
||
|f }d| }||d||
f d}|
dd||
f  |dd| g7 }|d | }|dd| g7 }d| v rt| d drtjdd\}}ttj| t	|| d  t| n?t| d d rtjdd\}}ttj| t| t|d!}| d 	| W d   n1 sw   Y  n| d d }|dd"| g7 }||fS )#a"  Build gdb command for this report.

        This builds a gdb command for processing the given report, by setting
        the file to the ExecutablePath/InterpreterPath, unpacking the core dump
        and pointing "core-file" to it (if the report has a core dump), and
        setting up the paths when calling gdb in a package sandbox.

        When available, this calls "gdb-multiarch" instead of "gdb", for
        processing crash reports from foreign architectures.

        Return argv list for gdb and any environment variables.
        r   r  Fr   TusrbinNgdb)rE   rE   zgdb-multiarchzWARNING: Please install gdb-multiarch for processing reports from foreign architectures. Results with "gdb" will be very poor.
zx86_64-linux-gnurt  z)set debug-file-directory %s/usr/lib/debugzset solib-absolute-prefix zadd-auto-load-safe-path z-set solib-search-path %s/lib/%s:%s/usr/lib/%sz)%s/lib:%s/lib/%s:%s/usr/lib/%s:%s/usr/libz%s/usrz%s/usr/lib/%s/gconv)LD_LIBRARY_PATH
PYTHONHOME
GCONV_PATHr   z%s/lib/%s/ld-linux-x86-64.so.2z#set data-directory %s/usr/share/gdbr  z	file "%s"rm  r^  apport_core_)prefix	gzipvaluewbz
core-file )r   r   r   r    r"   r   r   sysrA   re  insertrb  rc  rd  atexitregisterrz   rf  r+   )r   sandboxr  
executable	same_archgdb_sandbox_bingdb_pathrN   r   manative_multiarchld_lib_pathpyhomer6   rj  r   r   r   r   r~  a  s   




zReport.gdb_commandc                 C   sB   |    | jD ]\}}}||kr||krd||| f   S qdS )a^  Resolve a memory address to an ELF name and offset.

        This can be used for building duplicate signatures from non-symbolic
        stack traces. These often do not have enough symbols available to
        resolve function names, but taking the raw addresses also is not
        suitable due to ASLR. But the offsets within a library should be
        constant between crashes (assuming the same version of all libraries).

        This needs and uses the "ProcMaps" field to resolve addresses.

        Return 'path+offset' when found, or None if address is not in any
        mapped range.
        z%s+%xN)_build_proc_maps_cacher   )r   r0  startendelfr   r   r   r-    s   zReport._address_to_offsetc                 C   s   | j rdS d| v sJ g | _ td}td}| d  D ]4}| s%q||}|s:||r2q|s:J d| | j t|ddt|dd|d	f qdS )
z_Generate self._proc_maps_cache from ProcMaps field.

        This only gets done once.
        Nr-  z-^([0-9a-fA-F]+)-([0-9a-fA-F]+).*\s{2,}(\S.*$)z ^([0-9a-fA-F]+)-([0-9a-fA-F]+)\szcannot parse ProcMaps line: r   r*  r   r  )	r   rT   rj   r  r0   r  r   r8  r   )r   fmtfmt_unknownr  r   r   r   r   rX    s$   



zReport._build_proc_maps_cachec              	   C   s   t s|durtjdtj|d}nd| }zPt|6}|D ]!}| }d|v r>|dr>d|v r>|ddd dd	 } nq	 W d   W dS W d   n1 sSw   Y  td
| j	}W ||fS  t
tfyp   Y dS w )zGet logind session path and start time.

        Return (session_id, session_start_timestamp) if process is in a logind
        session, or None otherwise.
        Ncgroupr   z/proc/%s/cgroupzname=systemd:z.scopez	/session-r   iz/run/systemd/sessions/)r   r    r+   r,   r.   r0   r  r   rA  rB  r3   r2   )klassr#   r<   cgroup_filer   r  
my_sessionsession_start_timer   r   r   rD    s(   	zReport.get_logind_session)r   Nr   NN)'__name__
__module____qualname____doc__r   r   r   r   r   r   r   r  r  staticmethodr  rG  r?  rl  r  r  r  r  r  r  r  r  r  r  r  r  r&  r  r  r@  r~  r-  rX  classmethodrD  r   r   r   r   r     sN    

5+M

Z
;
)m
>;+-F.
  	>
*Y r   rc  )?rg  rF   rc  os.pathr    rT   r   r   r.   r   r  r   r;  rK  rM  importlib.utilr"  rA  xml.domrZ   xml.dom.minidomxml.parsers.expatr   r   r   urllib.errorr   urllib.requestr   urllib.parser   urllibr3   rf   r   apport.fileutilsapport.packaging_implr   r   apport.hookutilsr   r   r   	_data_dirr  r  r  r  r  r  r  r   r$   r8   r?   rR   rs   ry   r|   r   r   r   r   r   r   r   r   <module>   sN    @@




/