-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
280 lines (244 loc) · 8.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*******************************************************************************
* Copyright 2017 IBM Corp.
*
* 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.
******************************************************************************/
// Main module entry point for gencore
'use strict';
const gencore = require('./gencore');
const fs = require('fs');
const path = require('path');
const fstream = require('fstream');
const exec = require('child_process').exec;
let seq = 0;
exports.collectCore = collectCore;
exports.createCore = createCore;
/* Fork and create the core dump then callback is called
* with the path of the core file created so it can be
* opened on the current machine with a standard debugger
* such as gdb or lldb.
*/
function createCore(callback) {
if (process.platform == 'win32') {
callback(new Error('Function not supported on Windows.'));
return;
}
// Create a directory for the child process to crash in.
// Use the timestamp to create a (hopefully) unique namespace
// that allows the core to be related back to which process
// crashed and when.
const timestamp = generateTimestamp();
// Run synchronously until fork() returns.
const work_dir = `core_${timestamp}`;
fs.mkdirSync(work_dir);
let result = null;
try {
result = gencore.forkCore(work_dir);
} catch (err) {
setImmediate(callback, err);
return;
}
result.work_dir = work_dir;
setImmediate(waitForCore, result, callback);
}
function waitForCore(result, callback) {
try {
if(!gencore.checkChild(result.child_pid)) {
// Check again once the file has had a chance to be written.
setTimeout(waitForCore, 10, result, callback);
return;
}
} catch (err) {
setImmediate(callback, err);
return;
}
const work_dir = result.work_dir;
const pid = result.child_pid;
let core_name = findCore(work_dir, pid);
if( core_name !== undefined ) {
callback(null, core_name);
} else {
callback(new Error('No core file created.'));
}
}
/* Fork and create the core dump then collect the libraries
* that were in use at the same time and package the core
* and the libraries in a tar.gz file for analysis on another
* box.
* The uncompressed core is deleted.
* callback is called with the name of the tar.gz file created.
*/
function collectCore(callback) {
if (process.platform == 'win32') {
callback(new Error('Function not supported on Windows.'));
return;
}
// Up until we fork the child process this funciton needs to
// work synchronously to minimise how much the state of the
// process changes between requesting a core and the core
// being created.
// Create a directory for the child process to crash in.
// Use the timestamp to create a (hopefully) unique namespace
// that allows the core to be related back to which process
// crashed and when.
const timestamp = generateTimestamp();
// Run synchronously until fork() returns.
const work_dir = `core_${timestamp}`;
fs.mkdirSync(work_dir);
// Gather the library list before we allow async work
// that might change the list to run.
const libraries = gencore.findLibraries();
let result = null;
try {
result = gencore.forkCore(work_dir);
} catch (err) {
setImmediate(callback, err);
return;
}
// Now we can let other things run asyncrhonously!
result.libraries = libraries;
result.work_dir = work_dir;
setImmediate(waitForCoreAndCollect, result, callback);
}
function waitForCoreAndCollect(result, callback) {
try {
if(!gencore.checkChild(result.child_pid)) {
// Check again once the file has had a chance to be written.
setTimeout(waitForCoreAndCollect, 10, result, callback);
// Return so we don't need to indent the rest of
// this function in an else.
return;
}
} catch (err) {
setImmediate(callback, err);
return;
}
// Zip up the core file and libraries.
// The core file should have been created or
// copied to the work_dir.
const work_dir = result.work_dir;
const pid = result.child_pid;
let file_count = result.libraries.length;
// We will only return the last error if multiple files failed to copy.
let copy_err = null;
// Declare triggerZip here to give shared access
// to lib_count.
function triggerZip(error) {
// No need for locking to update libCount,
// only happens on the main node loop.
file_count--;
if( error ) {
copy_err = error;
}
if( file_count == 0 ) {
if( copy_err == null ) {
tarGzDir(work_dir, result, callback);
} else {
callback(copy_err);
}
}
}
let core_name = findCore(work_dir, pid);
if( core_name === undefined ) {
callback(new Error('Unable to locate core file'));
return;
} else if (!path.dirname(core_name).endsWith(work_dir)) {
// Mac OS X puts cores in /cores/core.<pid> by default so
// we have an extra file to copy into our tar.gz.
file_count++;
copyFile(core_name, `${work_dir}/core.${result.child_pid}`,
triggerZip);
}
for( let library of result.libraries ) {
let dest;
// Make all the paths relative to make it less likely to overwrite
// system libraries when the tar is extracted.
if( library.startsWith('/') ) {
dest = work_dir + '/' + library.substr(1);
} else {
dest = work_dir + '/';
}
// console.log(library + " -> " + dest);
let library_writer = fstream.Writer(dest);
let library_reader = fstream.Reader({ path: library, follow: true });
// Now copy the data, don't let failed copies
// stop us creating the zip.
library_reader.pipe(library_writer)
.on('close', (e) => triggerZip(e))
.on('error', (e) => triggerZip(e));
}
}
function copyFile(source, dest, closeCb) {
const read = fs.createReadStream(source);
const write = fs.createWriteStream(dest);
function error_func(e) {
read.close();
write.close();
closeCb(e);
}
read.on('error', error_func);
write.on('error', error_func);
read.pipe(write).on('close', closeCb);
}
function tarGzDir(work_dir, result, callback) {
let tar_file = `${work_dir}.tar.gz`;
// Use ls to obtain a list of files in work dir so the
// resulting paths don't start with "./"
exec(`tar -czf ${tar_file} ${work_dir}`,
(error, stdout, stderr) => {
exec(`rm -r ${work_dir}`);
callback(error, tar_file);
}
);
}
function findCore(work_dir, pid) {
let possible_names = [];
if( process.platform == 'darwin') {
// Mac OS X puts cores in /cores/core.<pid> by default so
// we have an extra file to copy into our zip.
// TODO - To be totally correct we should use:
// sysctlbyname("kern.corefile", in C to find the exact
// location.
possible_names.push(path.resolve(`${work_dir}/core.${pid}`));
possible_names.push(path.resolve(`/cores/core.${pid}`));
} else if (process.platform == 'linux') {
// Check the likely locations for a linux core dump.
possible_names.push(path.resolve(`${work_dir}/core.${pid}`));
possible_names.push(path.resolve(`${work_dir}/core`));
}
for(let name of possible_names) {
if( fs.existsSync(name) ) {
return name;
}
}
return undefined;
}
function generateTimestamp() {
const now = new Date();
function pad(n, len) {
if( len === undefined ) {
len = 2;
}
let str = `${n}`;
while(str.length < len) {
str = '0' + str;
}
return str;
}
// Create a time stamp that include the process id and a sequence number
// to make the core identifiable and unique.
const timestamp = `${pad(now.getFullYear())}${pad(now.getMonth()+1)}` +
`${pad(now.getDate())}.${pad(now.getHours())}${pad(now.getMinutes())}` +
`${pad(now.getSeconds())}.${process.pid}.${pad(++seq,3)}`;
return timestamp;
}