#!/usr/bin/env python

description = """
    Attach the debugger to a Python process (given by its pid) and
    extract as much information about its internal state as possible
    without any user interaction. The target process is frozen while
    this script runs and resumes when it is finished."""

# A backtrace is saved in the directory $SAGE_CRASH_LOGS, which is
# $DOT_SAGE/crash_logs by default.  Any backtraces older than
# $SAGE_CRASH_DAYS (default: 7 if SAGE_CRASH_LOGS unset, -1 if
# set) are automatically deleted, but with a negative value they are
# never deleted.


import sys
import os
import subprocess
import signal
import tempfile
import sysconfig

from argparse import ArgumentParser
from datetime import datetime


def pid_exists(pid):
    """
    Return True if and only if there is a process with id pid running.
    """
    try:
        os.kill(pid, 0)
        return True
    except (OSError, ValueError):
        return False

def gdb_commands(pid, color):
    cmds = ''
    cmds += 'set prompt (sage-gdb-prompt)\n'
    cmds += 'set verbose off\n'
    cmds += 'attach {0}\n'.format(pid)
    cmds += 'python\n' 
    cmds += 'print("\\n")\n' 
    cmds += 'print("Stack backtrace")\n' 
    cmds += 'print("---------------")\n' 
    cmds += 'import sys; sys.stdout.flush()\n' 
    cmds += 'end\n' 
    cmds += 'bt full\n'
    script = os.path.join(os.environ['SAGE_LOCAL'], 'bin', 'sage-CSI-helper.py')
    with open(script, 'r') as f:
        cmds += 'python\n'
        cmds += 'color = {0}\n'.format(color)
        cmds += f.read()
        cmds += 'end\n'
    cmds += 'detach inferior 1\n'
    cmds += 'python print("Stack backtrace (newest frame = first)\\n")\n'
    cmds += 'python print("--------------------------------------\\n")\n'
    cmds += 'python import sys; sys.stdout.flush()\n'
    cmds += 'quit\n'
    return cmds

def run_gdb(pid, color):
    """
    Execute gdb.
    """
    PIPE = subprocess.PIPE
    env = dict(os.environ)
    libpython = os.path.join(env['SAGE_LOCAL'], 'lib', 
                             sysconfig.get_config_var('INSTSONAME'))
    if sys.platform == 'macosx':
        env['DYLD_INSERT_LIBRARIES'] = libpython
    else:
        env['LD_PRELOAD'] = libpython   
    try:
        cmd = subprocess.Popen('gdb', stdin=PIPE, stdout=PIPE, 
                               stderr=PIPE, env=env)
    except OSError:
        return "Unable to start gdb (not installed?)"
    stdout, stderr = cmd.communicate(gdb_commands(pid, color))
    result = []
    for line in stdout.splitlines():
        if line.find('(sage-gdb-prompt)') >= 0:
            continue
        if line.startswith('Reading symbols from '):
            continue
        if line.startswith('Loaded symbols for '):
            continue
        result.append(line)
    if stderr != "":
        result.append(stderr)
    return '\n'.join(result)

def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as e:
        if os.path.isdir(path):
            pass
        else: 
            raise

def prune_old_logs(directory, days):
    """
    Delete all files in ``directory`` that are older than a given
    number of days.
    """
    for filename in os.listdir(directory):
        filename = os.path.join(directory, filename)
        mtime = datetime.utcfromtimestamp(os.path.getmtime(filename))
        age = datetime.utcnow() - mtime
        if age.days >= days:
            try:
                os.unlink(filename)
            except OSError:
                pass

def save_backtrace(output):
    try:
        bt_dir = os.environ['SAGE_CRASH_LOGS']
        # Don't delete all files in this directory, in case the user
        # set SAGE_CRASH_LOGS to a stupic value.
        bt_days = -1
    except KeyError:
        bt_dir = os.path.join(os.environ['DOT_SAGE'], 'crash_logs')
        bt_days = 7

    try:
        bt_days = int(os.environ['SAGE_CRASH_DAYS'])
    except KeyError:
        pass

    mkdir_p(bt_dir)
    if bt_days >= 0:
        prune_old_logs(bt_dir, bt_days)
    f, filename = tempfile.mkstemp(dir=bt_dir, prefix='sage_crash_', suffix='.log')
    os.write(f, output)
    os.close(f)
    return filename
    

if __name__ == '__main__':
    parser = ArgumentParser(description=description)
    parser.add_argument('-p', '--pid', dest='pid', action='store',
                        default=None, type=int,
                        help='the pid to attach to.')
    parser.add_argument('-nc', '--no-color', dest='nocolor', action='store_true',
                        default=False,
                        help='turn off syntax-highlighting.')
    parser.add_argument('-k', '--kill', dest='kill', action='store_true',
                        default=False,
                        help='kill after inspection is finished.')
    args = parser.parse_args()
    
    if args.pid is None:
        parser.print_help()
        sys.exit(0)

    if not pid_exists(args.pid):
        print 'There is no process with pid {0}.'.format(args.pid)
        sys.exit(1)

    print 'Attaching gdb to process id {0}.'.format(args.pid)
    trace = run_gdb(args.pid, not args.nocolor)
    print trace

    fatalities = [
        ( 'Unable to start gdb', 
          'GDB is not installed.' ),
        ( 'Hangup detected on fd 0',
          'Your system GDB is an old version that does not work with pipes'),
        ( 'error detected on stdin',
          'Your system GDB does not have Python support'),
        ( 'ImportError: No module named',
          'Your system GDB uses an incompatible version of Python') ]

    fail = False
    for key, msg in fatalities:
        if key in trace:
            print
            print msg
            fail = True
            break

    if fail:
        print 'Install the gdb spkg (sage -f gdb) for enhanced tracebacks.'
    else:
        filename = save_backtrace(trace)
        print 'Saved trace to {0}'.format(filename)
        
    if args.kill:
        os.kill(args.pid, signal.SIGKILL)
        
