xref: /openbsd-src/gnu/llvm/lldb/tools/debugserver/source/DNBError.cpp (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1*061da546Spatrick //===-- DNBError.cpp --------------------------------------------*- C++ -*-===//
2*061da546Spatrick //
3*061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*061da546Spatrick //
7*061da546Spatrick //===----------------------------------------------------------------------===//
8*061da546Spatrick //
9*061da546Spatrick //  Created by Greg Clayton on 6/26/07.
10*061da546Spatrick //
11*061da546Spatrick //===----------------------------------------------------------------------===//
12*061da546Spatrick 
13*061da546Spatrick #include "DNBError.h"
14*061da546Spatrick #include "CFString.h"
15*061da546Spatrick #include "DNBLog.h"
16*061da546Spatrick #include "PThreadMutex.h"
17*061da546Spatrick 
18*061da546Spatrick #ifdef WITH_SPRINGBOARD
19*061da546Spatrick #include <SpringBoardServices/SpringBoardServer.h>
20*061da546Spatrick #endif
21*061da546Spatrick 
AsString() const22*061da546Spatrick const char *DNBError::AsString() const {
23*061da546Spatrick   if (Success())
24*061da546Spatrick     return NULL;
25*061da546Spatrick 
26*061da546Spatrick   if (m_str.empty()) {
27*061da546Spatrick     const char *s = NULL;
28*061da546Spatrick     switch (m_flavor) {
29*061da546Spatrick     case MachKernel:
30*061da546Spatrick       s = ::mach_error_string(m_err);
31*061da546Spatrick       break;
32*061da546Spatrick 
33*061da546Spatrick     case POSIX:
34*061da546Spatrick       s = ::strerror(m_err);
35*061da546Spatrick       break;
36*061da546Spatrick 
37*061da546Spatrick #ifdef WITH_SPRINGBOARD
38*061da546Spatrick     case SpringBoard: {
39*061da546Spatrick       CFStringRef statusStr = SBSApplicationLaunchingErrorString(m_err);
40*061da546Spatrick       if (CFString::UTF8(statusStr, m_str) == NULL)
41*061da546Spatrick         m_str.clear();
42*061da546Spatrick     } break;
43*061da546Spatrick #endif
44*061da546Spatrick #ifdef WITH_BKS
45*061da546Spatrick     case BackBoard: {
46*061da546Spatrick       // You have to call ObjC routines to get the error string from
47*061da546Spatrick       // BackBoardServices.
48*061da546Spatrick       // Not sure I want to make DNBError.cpp an .mm file.  For now just make
49*061da546Spatrick       // sure you
50*061da546Spatrick       // pre-populate the error string when you make the DNBError of type
51*061da546Spatrick       // BackBoard.
52*061da546Spatrick       m_str.assign(
53*061da546Spatrick           "Should have set BackBoard error when making the error string.");
54*061da546Spatrick     } break;
55*061da546Spatrick #endif
56*061da546Spatrick #ifdef WITH_FBS
57*061da546Spatrick     case FrontBoard: {
58*061da546Spatrick       // You have to call ObjC routines to get the error string from
59*061da546Spatrick       // FrontBoardServices.
60*061da546Spatrick       // Not sure I want to make DNBError.cpp an .mm file.  For now just make
61*061da546Spatrick       // sure you
62*061da546Spatrick       // pre-populate the error string when you make the DNBError of type
63*061da546Spatrick       // FrontBoard.
64*061da546Spatrick       m_str.assign(
65*061da546Spatrick           "Should have set FrontBoard error when making the error string.");
66*061da546Spatrick     } break;
67*061da546Spatrick #endif
68*061da546Spatrick     default:
69*061da546Spatrick       break;
70*061da546Spatrick     }
71*061da546Spatrick     if (s)
72*061da546Spatrick       m_str.assign(s);
73*061da546Spatrick   }
74*061da546Spatrick   if (m_str.empty())
75*061da546Spatrick     return NULL;
76*061da546Spatrick   return m_str.c_str();
77*061da546Spatrick }
78*061da546Spatrick 
LogThreadedIfError(const char * format,...) const79*061da546Spatrick void DNBError::LogThreadedIfError(const char *format, ...) const {
80*061da546Spatrick   if (Fail()) {
81*061da546Spatrick     char *arg_msg = NULL;
82*061da546Spatrick     va_list args;
83*061da546Spatrick     va_start(args, format);
84*061da546Spatrick     ::vasprintf(&arg_msg, format, args);
85*061da546Spatrick     va_end(args);
86*061da546Spatrick 
87*061da546Spatrick     if (arg_msg != NULL) {
88*061da546Spatrick       const char *err_str = AsString();
89*061da546Spatrick       if (err_str == NULL)
90*061da546Spatrick         err_str = "???";
91*061da546Spatrick       DNBLogThreaded("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_err);
92*061da546Spatrick       free(arg_msg);
93*061da546Spatrick     }
94*061da546Spatrick   }
95*061da546Spatrick }
96*061da546Spatrick 
LogThreaded(const char * format,...) const97*061da546Spatrick void DNBError::LogThreaded(const char *format, ...) const {
98*061da546Spatrick   char *arg_msg = NULL;
99*061da546Spatrick   va_list args;
100*061da546Spatrick   va_start(args, format);
101*061da546Spatrick   ::vasprintf(&arg_msg, format, args);
102*061da546Spatrick   va_end(args);
103*061da546Spatrick 
104*061da546Spatrick   if (arg_msg != NULL) {
105*061da546Spatrick     if (Fail()) {
106*061da546Spatrick       const char *err_str = AsString();
107*061da546Spatrick       if (err_str == NULL)
108*061da546Spatrick         err_str = "???";
109*061da546Spatrick       DNBLogThreaded("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_err);
110*061da546Spatrick     } else {
111*061da546Spatrick       DNBLogThreaded("%s err = 0x%8.8x", arg_msg, m_err);
112*061da546Spatrick     }
113*061da546Spatrick     free(arg_msg);
114*061da546Spatrick   }
115*061da546Spatrick }
116