>From f95506d390d65094646eb744203a21f2e328c283 Mon Sep 17 00:00:00 2001
From: Jiri Moskovcak <jmoskovc@redhat.com>
Date: Mon, 13 Feb 2012 10:31:24 +0100
Subject: [PATCH 3/3] vmcore: rewrite to python

- for easier localization and better debuginfo handling
---
 configure.ac                              |    1 +
 po/POTFILES.in                            |    1 +
 src/plugins/Makefile.am                   |    8 +-
 src/plugins/abrt-action-analyze-vmcore    |   27 -------
 src/plugins/abrt-action-analyze-vmcore.in |  110 +++++++++++++++++++++++++++++
 5 files changed, 116 insertions(+), 31 deletions(-)
 delete mode 100755 src/plugins/abrt-action-analyze-vmcore
 create mode 100644 src/plugins/abrt-action-analyze-vmcore.in

diff --git a/configure.ac b/configure.ac
index c2d4950..4391239 100644
--- a/configure.ac
+++ b/configure.ac
@@ -130,6 +130,7 @@ AC_CONFIG_FILES([
 	src/gui-gtk/Makefile
 	src/cli/Makefile
 	src/plugins/abrt-action-install-debuginfo
+	src/plugins/abrt-action-analyze-vmcore
 	po/Makefile.in
 	icons/Makefile
 ])
diff --git a/po/POTFILES.in b/po/POTFILES.in
index fdff6bb..9b70e30 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -15,6 +15,7 @@ src/plugins/abrt-action-analyze-backtrace.c
 src/plugins/abrt-action-analyze-c.c
 src/plugins/abrt-action-analyze-oops.c
 src/plugins/abrt-action-analyze-python.c
+src/plugins/abrt-action-analyze-vmcore.in
 src/plugins/abrt-action-generate-backtrace.c
 src/plugins/abrt-action-generate-core-backtrace.c
 src/plugins/abrt-action-install-debuginfo
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 8cbb354..eebada0 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -52,7 +52,8 @@ dist_eventsconf_DATA = \
 PYTHON_FILES = \
     abrt-action-install-debuginfo.in \
     abrt-action-list-dsos \
-    abrt-action-analyze-core
+    abrt-action-analyze-core \
+    abrt-action-analyze-vmcore.in
 
 EXTRA_DIST = \
     $(PYTHON_FILES) \
@@ -63,8 +64,7 @@ EXTRA_DIST = \
     collect_vimrc_system.xml.in \
     analyze_LocalGDB.xml.in \
     analyze_RetraceServer.xml.in \
-    analyze_VMcore.xml.in \
-    abrt-action-analyze-vmcore
+    analyze_VMcore.xml.in
 
 abrt_dump_oops_SOURCES = \
     abrt-dump-oops.c
@@ -210,4 +210,4 @@ abrt_retrace_client_SOURCES = \
      $(BTPARSER_LIBS) \
      $(NSS_LIBS)
 
-DEFS = -DLOCALEDIR=\"$(localedir)\" @DEFS@
+DEFS = -DLOCALEDIR=\"$(localedir)\" @DEFS@
\ No newline at end of file
diff --git a/src/plugins/abrt-action-analyze-vmcore b/src/plugins/abrt-action-analyze-vmcore
deleted file mode 100755
index 358535b..0000000
--- a/src/plugins/abrt-action-analyze-vmcore
+++ /dev/null
@@ -1,27 +0,0 @@
-#!/bin/sh
-#
-# Generate backtrace from vmcore
-#
-
-VER=`crash --osrelease vmcore`
-# If crash tool is not installed, or doesn't support --osrelease
-# (support was added in crash-5.1.7),
-# prev line failed (exitcode !0). In this case, abort:
-test $? = 0 || exit 1
-# Paranoia: ensure $VER is not empty
-test "$VER" || exit 1
-
-if ! test -f "/usr/lib/debug/lib/modules/$VER/vmlinux"; then
-	echo "Installing kernel debuginfo(s)"
-	yum install --enablerepo=*debuginfo* "/usr/lib/debug/lib/modules/$VER/vmlinux"
-fi
-
-echo "Generating backtrace"
-export TERM=dumb  # defeat gdb ESC sequence emission bug
-echo "log" | crash -s vmcore "/usr/lib/debug/lib/modules/$VER/vmlinux" >kernel_log
-abrt-dump-oops -o kernel_log >backtrace
-tail -n +3 backtrace >backtrace1
-mv backtrace1 backtrace
-
-head -n 1 backtrace >reason
-printf 'kernel-%s' "$VER" >package
diff --git a/src/plugins/abrt-action-analyze-vmcore.in b/src/plugins/abrt-action-analyze-vmcore.in
new file mode 100644
index 0000000..d81a2ef
--- /dev/null
+++ b/src/plugins/abrt-action-analyze-vmcore.in
@@ -0,0 +1,110 @@
+#! /usr/bin/python -u
+# -*- coding: utf-8 -*-
+# Generate backtrace from vmcore
+#
+
+from subprocess import Popen, PIPE
+import os
+import sys
+from reportclient import _, verbose, log, log1, log2, set_verbosity, error_msg_and_die
+from reportclient.debuginfo import DebugInfoDownload
+
+PROGNAME = "abrt-action-analyze-vmcore"
+
+# everything was ok
+RETURN_OK = 0
+# serious problem, should be logged somewhere
+RETURN_FAILURE = 2
+
+import getopt
+
+ver=""
+if __name__ == "__main__":
+    cachedirs = []
+    vmlinux_di_cachedir = ""
+    vmlinux_di_path = ""
+    vmcore = os.getcwd()+"/vmcore"
+    global verbose
+    help_text = _("Usage: {0} [-v[v]] [--core=VMCORE] [--tmpdir=TMPDIR] [--cache=CACHEDIR]").format(PROGNAME)
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "tmpdir=", "cache=", "core="])
+    except getopt.GetoptError, err:
+        error_msg(err) # prints something like "option -a not recognized"
+        error_msg_and_die(help_text)
+
+    for opt, arg in opts:
+        if opt in ("-h", "--help"):
+            print help_text
+            exit(0)
+        if opt == "--cache":
+            cachedirs = arg.split(':')
+        elif opt == "-v":
+            verbose += 1
+        elif opt == "--core":
+            vmcore = arg
+        elif opt == "--tmpdir":
+            tmpdir = arg
+
+    set_verbosity(verbose)
+    if not os.path.exists(vmcore):
+        print _("File {0} doesn't exist").format(vmcore)
+        sys.exit(RETURN_FAILURE)
+
+    if not cachedirs:
+        try:
+            fp = open("@sysconfdir@/@PACKAGE_NAME@/plugins/CCpp.conf", "r")
+            for line in fp:
+                stripped = line.lstrip()
+                if len(stripped) == 0:
+                    continue
+                if stripped[0] == "#":
+                    continue
+                if stripped[:len("DebuginfoLocation")] != "DebuginfoLocation":
+                    continue
+
+                cachedirs =  stripped[len("DebuginfoLocation"):].strip(" =\n").split(":")
+            fp.close()
+        except IOError as (errno, strerror):
+            print "I/O error({0}): {1}".format(errno, strerror)
+
+    if not cachedirs:
+        cachedirs = ["/var/cache/abrt-di"]
+
+    if not tmpdir:
+        # security people prefer temp subdirs in app's private dir, like /var/run/abrt
+        # for now, we use /tmp...
+        tmpdir = "/tmp/abrt-tmp-debuginfo-%s.%u" % (time.strftime("%Y-%m-%d-%H:%M:%S"), os.getpid())
+
+    crash = Popen(["crash", "--osrelease", vmcore], stdout=PIPE, bufsize = -1)
+    ver, err = crash.communicate()
+    ver = ver.strip() #remove '\n'
+    if not ver or crash.returncode != 0:
+        error_msg_and_die(_("{0} is not a valid vmcore").format(vmcore))
+
+    for cachedir in cachedirs:
+        path = "{0}/usr/lib/debug/lib/modules/{1}/vmlinux".format(cachedir, ver)
+        if os.path.exists(path):
+            vmlinux_di_cachedir = cachedir
+            print _("Required vmlinux debuginfo is installed in {0}".format(vmlinux_di_cachedir))
+            vmlinux_di_path = path
+            break
+
+    if not vmlinux_di_path: #didn't find the vmlinux in any cachedir
+        vmlinux_di_cachedir = cachedirs[0] # let's hope it has always at least 1 item
+        print _("Installing kernel debuginfo(s)")
+        # replace with abrt-action-install-debuginfo
+        vmlinux_di_path = "/usr/lib/debug/lib/modules/%s/vmlinux" % ver
+        downloader = DebugInfoDownload(cache=vmlinux_di_cachedir, tmp=tmpdir, keep_rpms=True)
+        res = downloader.download([vmlinux_di_path], exact_files=True)
+        if res != RETURN_OK:
+            error_msg_and_die(_("Can't download required debuginfo"))
+        vmlinux_di_path = "{0}/usr/lib/debug/lib/modules/{1}/vmlinux".format(cachedir, ver)
+
+    print _("Generating backtrace")
+    log_file = open("kernel_log","w")
+    crash = Popen(["crash", "-s", "vmcore", vmlinux_di_path], stdin=PIPE, stderr=PIPE, stdout=log_file, bufsize = -1)
+    out, err = crash.communicate(input="log")
+    log_file.close()
+    backtrace_file = open("backtrace", "w")
+    dump_oops = Popen(["abrt-dump-oops", "-o", "kernel_log"], stdout=backtrace_file, bufsize=-1)
+    backtrace_file.close()
-- 
1.7.7.6

