tesseract  4.0.0-1-g2a2b
globaloc.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: errcode.cpp (Formerly error.c)
3  * Description: Generic error handler function
4  * Author: Ray Smith
5  * Created: Tue May 1 16:28:39 BST 1990
6  *
7  * (C) Copyright 1989, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include "globaloc.h"
21 #include <csignal>
22 #ifdef __linux__
23 #include <sys/syscall.h> // For SYS_gettid.
24 #include <unistd.h> // For syscall itself.
25 #endif
26 #include "allheaders.h"
27 #include "errcode.h"
28 #include "tprintf.h"
29 
30 // Size of thread-id array of pixes to keep in case of crash.
31 const int kMaxNumThreadPixes = 32768;
32 
33 static Pix* global_crash_pixes[kMaxNumThreadPixes];
34 
35 void SavePixForCrash(int resolution, Pix* pix) {
36 #ifdef __linux__
37 #ifndef ANDROID
38  int thread_id = syscall(SYS_gettid) % kMaxNumThreadPixes;
39 #else
40  int thread_id = gettid() % kMaxNumThreadPixes;
41 #endif
42  pixDestroy(&global_crash_pixes[thread_id]);
43  if (pix != nullptr) {
44  Pix* clone = pixClone(pix);
45  pixSetXRes(clone, resolution);
46  pixSetYRes(clone, resolution);
47  global_crash_pixes[thread_id] = clone;
48  }
49 #endif
50 }
51 
52 // CALL ONLY from a signal handler! Writes a crash image to stderr.
53 void signal_exit(int signal_code) {
54  tprintf("Received signal %d!\n", signal_code);
55 #ifdef __linux__
56 #ifndef ANDROID
57  int thread_id = syscall(SYS_gettid) % kMaxNumThreadPixes;
58 #else
59  int thread_id = gettid() % kMaxNumThreadPixes;
60 #endif
61  if (global_crash_pixes[thread_id] != nullptr) {
62  fprintf(stderr, "Crash caused by image with resolution %d\n",
63  pixGetYRes(global_crash_pixes[thread_id]));
64  fprintf(stderr, "<Cut here>\n");
65  pixWriteStreamPng(stderr, global_crash_pixes[thread_id], 0.0);
66  fprintf(stderr, "\n<End cut>\n");
67  }
68  // Raise an uncaught signal, so as to get a useful stack trace.
69  raise(SIGILL);
70 #else
71  abort();
72 #endif
73 }
74 
75 void err_exit() {
76  ASSERT_HOST("Fatal error encountered!" == nullptr);
77 }
78 
79 // TODO: remove empty function?
80 void set_global_loc_code(int loc_code) {
81  // global_loc_code = loc_code;
82 }
83 
84 // TODO: remove empty function?
85 void set_global_subloc_code(int loc_code) {
86  // global_subloc_code = loc_code;
87 }
88 
89 // TODO: remove empty function?
90 void set_global_subsubloc_code(int loc_code) {
91  // global_subsubloc_code = loc_code;
92 }
void set_global_loc_code(int loc_code)
Definition: globaloc.cpp:80
void SavePixForCrash(int resolution, Pix *pix)
Definition: globaloc.cpp:35
void signal_exit(int signal_code)
Definition: globaloc.cpp:53
const int kMaxNumThreadPixes
Definition: globaloc.cpp:31
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
void set_global_subsubloc_code(int loc_code)
Definition: globaloc.cpp:90
void err_exit()
Definition: globaloc.cpp:75
void set_global_subloc_code(int loc_code)
Definition: globaloc.cpp:85
#define ASSERT_HOST(x)
Definition: errcode.h:84