#!/usr/bin/env python
# Copyright 2012 Cloudera Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script process a minidump generated by breakpad.
# TODO: this script can be made arbitrarily sophisticated. It can marry the information
# in the minidump from multiple tools, file jiras, initial triage, etc.

from optparse import OptionParser
import sys
import os
import shutil
import subprocess

BREAKPAD_DEFAULT_HOME = os.environ['IMPALA_TOOLCHAIN'] + "/breakpad-" +\
   os.environ['IMPALA_BREAKPAD_VERSION']

parser = OptionParser()
parser.add_option("--stackwalk", dest="stackwalk_binary",
    default=BREAKPAD_DEFAULT_HOME + "/bin/minidump_stackwalk",
    help="Path to minidump_stackwalk tool")
options, args = parser.parse_args()

def run_shell_cmd(cmd):
  ret = subprocess.call(cmd, shell = True)
  if ret != 0:
    raise Exception("Failed to run cmd: %s ret=%s" % (cmd, ret))
  return 0

if __name__ == "__main__":
  if len(args) != 2:
    sys.exit("usage: [mini dump] [root symbols directory]")

  cmd = options.stackwalk_binary + " " + args[0] + " " + args[1]
  run_shell_cmd(cmd)
