1 /*
2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3  *                         University Research and Technology
4  *                         Corporation.  All rights reserved.
5  * Copyright (c) 2004-2005 The University of Tennessee and The University
6  *                         of Tennessee Research Foundation.  All rights
7  *                         reserved.
8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9  *                         University of Stuttgart.  All rights reserved.
10  * Copyright (c) 2004-2006 The Regents of the University of California.
11  *                         All rights reserved.
12  * Copyright (c) 2011 Cisco Systems, Inc.  All rights reserved.
13  * Copyright (c) 2017      IBM Corporation.  All rights reserved.
14  * $COPYRIGHT$
15  *
16  * Additional copyrights may follow
17  *
18  * $HEADER$
19  */
20 
21 #include "opal_config.h"
22 
23 #include <stdio.h>
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_EXECINFO_H
29 #include <execinfo.h>
30 #endif
31 
32 #include "opal/constants.h"
33 #include "opal/mca/backtrace/backtrace.h"
34 
35 int
opal_backtrace_print(FILE * file,char * prefix,int strip)36 opal_backtrace_print(FILE *file, char *prefix, int strip)
37 {
38     int i, len;
39     int trace_size;
40     void * trace[32];
41     char buf[6];
42     int fd = opal_stacktrace_output_fileno;
43 
44     if( NULL != file ) {
45         fd = fileno(file);
46     }
47 
48     if (-1 == fd) {
49         return OPAL_ERR_BAD_PARAM;
50     }
51 
52     trace_size = backtrace (trace, 32);
53 
54     for (i = strip; i < trace_size; i++) {
55         if (NULL != prefix) {
56             write (fd, prefix, strlen (prefix));
57         }
58         len = snprintf (buf, sizeof(buf), "[%2d] ", i - strip);
59         write (fd, buf, len);
60         backtrace_symbols_fd (&trace[i], 1, fd);
61     }
62 
63     return OPAL_SUCCESS;
64 }
65 
66 
67 int
opal_backtrace_buffer(char *** message_out,int * len_out)68 opal_backtrace_buffer(char ***message_out, int *len_out)
69 {
70     int trace_size;
71     void * trace[32];
72     char ** funcs = (char **)NULL;
73 
74     trace_size = backtrace (trace, 32);
75     funcs = backtrace_symbols (trace, trace_size);
76 
77     *message_out = funcs;
78     *len_out = trace_size;
79 
80     return OPAL_SUCCESS;
81 }
82