All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
globaloc.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: errcode.c (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 <signal.h>
21 #ifdef __linux__
22 #include <sys/syscall.h> // For SYS_gettid.
23 #include <unistd.h> // For syscall itself.
24 #endif
25 #include "allheaders.h"
26 #include "errcode.h"
27 #include "tprintf.h"
28 
29 // Size of thread-id array of pixes to keep in case of crash.
30 const int kMaxNumThreadPixes = 32768;
31 
33 
34 void SavePixForCrash(int resolution, Pix* pix) {
35 #ifdef __linux__
36 #ifndef ANDROID
37  int thread_id = syscall(SYS_gettid) % kMaxNumThreadPixes;
38 #else
39  int thread_id = gettid() % kMaxNumThreadPixes;
40 #endif
41  pixDestroy(&global_crash_pixes[thread_id]);
42  if (pix != NULL) {
43  Pix* clone = pixClone(pix);
44  pixSetXRes(clone, resolution);
45  pixSetYRes(clone, resolution);
46  global_crash_pixes[thread_id] = clone;
47  }
48 #endif
49 }
50 
51 // CALL ONLY from a signal handler! Writes a crash image to stderr.
52 void signal_exit(int signal_code) {
53  tprintf("Received signal %d!\n", signal_code);
54 #ifdef __linux__
55 #ifndef ANDROID
56  int thread_id = syscall(SYS_gettid) % kMaxNumThreadPixes;
57 #else
58  int thread_id = gettid() % kMaxNumThreadPixes;
59 #endif
60  if (global_crash_pixes[thread_id] != NULL) {
61  fprintf(stderr, "Crash caused by image with resolution %d\n",
62  pixGetYRes(global_crash_pixes[thread_id]));
63  fprintf(stderr, "<Cut here>\n");
64  pixWriteStreamPng(stderr, global_crash_pixes[thread_id], 0.0);
65  fprintf(stderr, "\n<End cut>\n");
66  }
67  // Raise an uncaught signal, so as to get a useful stack trace.
68  raise(SIGILL);
69 #else
70  abort();
71 #endif
72 }
73 
74 void err_exit() {
75  ASSERT_HOST("Fatal error encountered!" == NULL);
76 }
77 
78 
79 void set_global_loc_code(int loc_code) {
80  // global_loc_code = loc_code;
81 
82 }
83 
84 
85 void set_global_subloc_code(int loc_code) {
86  // global_subloc_code = loc_code;
87 
88 }
89 
90 
91 void set_global_subsubloc_code(int loc_code) {
92  // global_subsubloc_code = loc_code;
93 
94 }
void SavePixForCrash(int resolution, Pix *pix)
Definition: globaloc.cpp:34
void err_exit()
Definition: globaloc.cpp:74
#define tprintf(...)
Definition: tprintf.h:31
void set_global_subsubloc_code(int loc_code)
Definition: globaloc.cpp:91
#define ASSERT_HOST(x)
Definition: errcode.h:84
void set_global_loc_code(int loc_code)
Definition: globaloc.cpp:79
void set_global_subloc_code(int loc_code)
Definition: globaloc.cpp:85
const int kMaxNumThreadPixes
Definition: globaloc.cpp:30
Pix * global_crash_pixes[kMaxNumThreadPixes]
Definition: globaloc.cpp:32
#define NULL
Definition: host.h:144
void signal_exit(int signal_code)
Definition: globaloc.cpp:52