~ubuntu-branches/ubuntu/oneiric/bug-buddy/oneiric

« back to all changes in this revision

Viewing changes to google-breakpad/src/client/solaris/handler/minidump_generator.cc

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-03-17 11:52:38 UTC
  • mfrom: (1.1.27 upstream)
  • Revision ID: james.westby@ubuntu.com-20090317115238-pelki75p8yvl9bw1
Tags: 2.26.0+dfsg-0ubuntu1
* New upstream release: (LP: #344132)
  - Don't hardcode a scrollkeeper check in the configure script.
  - Build correctly with --disable-eds.
  - Fix a segfault (Josseline Mouette).
  - Don't free uninitialized memory.
  - Drop libgnome and libgnomeui dependencies.
  - Make google-breakpad support optional (but enabled by default).
    Thanks to Sjoerd Simons.
  - Obtain the real path of the crashed process by looking in /proc.
    Thanks to Sam Morris and Matt Keenan.
  - Add an option to delete the included file after bug-buddy has
    processed it.
  - Implement a logger for pasting critical and fatal warnings in the
    stacktraces.
  - Include the loaded GTK+ modules in the stacktraces sent to bugzilla.
  - Update google-breakpad to SVN r290.
  - Compile with all the GLib/GTK+ deprecation flags.
* debian/control.in:
  - remove scroolkeeper, libgnome2-dev and libgnomeui-dev b-d
  - bump libgtk2.0-dev to 2.14
  - add libgconf2-dev
  - add Vcs-Bzr tag
  - re-generate debian/control
* debian/patches:
  - remove 01_double-free.patch as taken upstream
  - remove 02_disable_breakpad.patch as upstream handle --disable-*
    (and so, --disable-google-breakpad)
  - refresh 70_automake-autoconf.patch
* debian/rules: remove --disable-scrollkeeper as removed in configure
  file
* Adapt debian/watch to get unstable version

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
// Author: Alfred Peng
31
31
 
32
32
#include <fcntl.h>
33
 
#include <pthread.h>
 
33
#include <sys/frame.h>
34
34
#include <sys/stat.h>
35
35
#include <sys/types.h>
36
36
#include <sys/utsname.h>
37
37
#include <sys/wait.h>
38
 
#include <ucontext.h>
39
38
#include <unistd.h>
40
39
 
41
40
#include <cstdlib>
45
44
#include "client/minidump_file_writer-inl.h"
46
45
#include "common/solaris/file_id.h"
47
46
 
48
 
namespace google_breakpad {
49
 
 
50
 
MinidumpGenerator::MinidumpGenerator()
51
 
    : requester_pid_(0),
52
 
      signo_(0),
53
 
      lwp_lister_(NULL) {
54
 
}
55
 
 
56
 
MinidumpGenerator::~MinidumpGenerator() {
57
 
}
58
 
 
59
 
bool MinidumpGenerator::WriteLwpStack(uintptr_t last_esp,
60
 
                                      UntypedMDRVA *memory,
61
 
                                      MDMemoryDescriptor *loc) {
62
 
  uintptr_t stack_bottom = lwp_lister_->GetLwpStackBottom(last_esp);
 
47
namespace {
 
48
 
 
49
using namespace google_breakpad;
 
50
 
 
51
// Argument for the writer function.
 
52
struct WriterArgument {
 
53
  MinidumpFileWriter *minidump_writer;
 
54
 
 
55
  // Pid of the lwp who called WriteMinidumpToFile
 
56
  int requester_pid;
 
57
 
 
58
  // The stack bottom of the lwp which caused the dump.
 
59
  // Mainly used to find the lwp id of the crashed lwp since signal
 
60
  // handler may not be called in the lwp who caused it.
 
61
  uintptr_t crashed_stack_bottom;
 
62
 
 
63
  // Id of the crashing lwp.
 
64
  int crashed_lwpid;
 
65
 
 
66
  // Signal number when crash happened. Can be 0 if this is a requested dump.
 
67
  int signo;
 
68
 
 
69
  // The ebp of the signal handler frame on x86.  Can be 0 if this is a
 
70
  // requested dump.
 
71
  uintptr_t sighandler_ebp;
 
72
 
 
73
  // User context when crash happens. Can be NULL if this is a requested dump.
 
74
  // This is actually an out parameter, but it will be filled in at the start
 
75
  // of the writer LWP.
 
76
  ucontext_t *sig_ctx;
 
77
 
 
78
  // Used to get information about the lwps.
 
79
  SolarisLwp *lwp_lister;
 
80
};
 
81
 
 
82
// Holding context information for the callback of finding the crashing lwp.
 
83
struct FindCrashLwpContext {
 
84
  const SolarisLwp *lwp_lister;
 
85
  uintptr_t crashing_stack_bottom;
 
86
  int crashing_lwpid;
 
87
 
 
88
  FindCrashLwpContext() :
 
89
    lwp_lister(NULL),
 
90
    crashing_stack_bottom(0UL),
 
91
    crashing_lwpid(-1) {
 
92
  }
 
93
};
 
94
 
 
95
// Callback for list lwps.
 
96
// It will compare the stack bottom of the provided lwp with the stack
 
97
// bottom of the crashed lwp, it they are eqaul, this lwp is the one
 
98
// who crashed.
 
99
bool IsLwpCrashedCallback(lwpstatus_t *lsp, void *context) {
 
100
  FindCrashLwpContext *crashing_context =
 
101
    static_cast<FindCrashLwpContext *>(context);
 
102
  const SolarisLwp *lwp_lister = crashing_context->lwp_lister;
 
103
  const prgregset_t *gregs = &(lsp->pr_reg);
 
104
#if TARGET_CPU_SPARC
 
105
  uintptr_t last_ebp = (*gregs)[R_FP];
 
106
#elif TARGET_CPU_X86
 
107
  uintptr_t last_ebp = (*gregs)[EBP];
 
108
#endif
 
109
  uintptr_t stack_bottom = lwp_lister->GetLwpStackBottom(last_ebp);
 
110
  if (stack_bottom > last_ebp &&
 
111
      stack_bottom == crashing_context->crashing_stack_bottom) {
 
112
    // Got it. Stop iteration.
 
113
    crashing_context->crashing_lwpid = lsp->pr_lwpid;
 
114
    return false;
 
115
  }
 
116
 
 
117
  return true;
 
118
}
 
119
 
 
120
// Find the crashing lwpid.
 
121
// This is done based on stack bottom comparing.
 
122
int FindCrashingLwp(uintptr_t crashing_stack_bottom,
 
123
                    int requester_pid,
 
124
                    const SolarisLwp *lwp_lister) {
 
125
  FindCrashLwpContext context;
 
126
  context.lwp_lister = lwp_lister;
 
127
  context.crashing_stack_bottom = crashing_stack_bottom;
 
128
  CallbackParam<LwpCallback> callback_param(IsLwpCrashedCallback,
 
129
                                            &context);
 
130
  lwp_lister->Lwp_iter_all(lwp_lister->getpid(), &callback_param);
 
131
  return context.crashing_lwpid;
 
132
}
 
133
 
 
134
bool WriteLwpStack(const SolarisLwp *lwp_lister,
 
135
                   uintptr_t last_esp,
 
136
                   UntypedMDRVA *memory,
 
137
                   MDMemoryDescriptor *loc) {
 
138
  uintptr_t stack_bottom = lwp_lister->GetLwpStackBottom(last_esp);
63
139
  if (stack_bottom >= last_esp) {
64
140
    int size = stack_bottom - last_esp;
65
141
    if (size > 0) {
75
151
}
76
152
 
77
153
#if TARGET_CPU_SPARC
78
 
bool MinidumpGenerator::WriteContext(MDRawContextSPARC *context, prgregset_t regs,
79
 
                                     prfpregset_t *fp_regs) {
 
154
bool WriteContext(MDRawContextSPARC *context, ucontext_t *sig_ctx) {
 
155
  assert(sig_ctx != NULL);
 
156
  int* regs = sig_ctx->uc_mcontext.gregs;
 
157
  context->context_flags = MD_CONTEXT_SPARC_FULL;
 
158
 
 
159
  context->ccr = (unsigned int)(regs[0]);
 
160
  context->pc = (unsigned int)(regs[REG_PC]);
 
161
  context->npc = (unsigned int)(regs[REG_nPC]);
 
162
  context->y = (unsigned int)(regs[REG_Y]);
 
163
  context->asi = (unsigned int)(regs[19]);
 
164
  context->fprs = (unsigned int)(regs[20]);
 
165
 
 
166
  for ( int i = 0 ; i < 32; ++i ) {
 
167
    context->g_r[i] = 0;
 
168
  }
 
169
 
 
170
  for ( int i = 1 ; i < 16; ++i ) {
 
171
    context->g_r[i] = (uintptr_t)(sig_ctx->uc_mcontext.gregs[i + 3]);
 
172
  }
 
173
  context->g_r[30] = (uintptr_t)(((struct frame *)context->g_r[14])->fr_savfp);
 
174
 
 
175
  return true;
 
176
}
 
177
 
 
178
bool WriteContext(MDRawContextSPARC *context, prgregset_t regs,
 
179
                  prfpregset_t *fp_regs) {
80
180
  if (!context || !regs)
81
181
    return false;
82
182
 
83
183
  context->context_flags = MD_CONTEXT_SPARC_FULL;
84
184
 
85
 
  context->ccr = (unsigned int)(regs[32]);
86
 
  context->pc = (unsigned int)(regs[R_PC]);
87
 
  context->npc = (unsigned int)(regs[R_nPC]);
88
 
  context->y = (unsigned int)(regs[R_Y]);
89
 
  context->asi = (unsigned int)(regs[36]);
90
 
  context->fprs = (unsigned int)(regs[37]);
91
 
 
 
185
  context->ccr = (uintptr_t)(regs[32]);
 
186
  context->pc = (uintptr_t)(regs[R_PC]);
 
187
  context->npc = (uintptr_t)(regs[R_nPC]);
 
188
  context->y = (uintptr_t)(regs[R_Y]);
 
189
  context->asi = (uintptr_t)(regs[36]);
 
190
  context->fprs = (uintptr_t)(regs[37]);
92
191
  for ( int i = 0 ; i < 32 ; ++i ){
93
 
    context->g_r[i] = (unsigned int)(regs[i]);
 
192
    context->g_r[i] = (uintptr_t)(regs[i]);
94
193
  }
95
194
 
96
195
  return true;
97
196
}
98
197
#elif TARGET_CPU_X86
99
 
bool MinidumpGenerator::WriteContext(MDRawContextX86 *context, prgregset_t regs,
100
 
                                     prfpregset_t *fp_regs) {
 
198
bool WriteContext(MDRawContextX86 *context, prgregset_t regs,
 
199
                  prfpregset_t *fp_regs) {
101
200
  if (!context || !regs)
102
201
    return false;
103
202
 
124
223
}
125
224
#endif /* TARGET_CPU_XXX */
126
225
 
127
 
bool MinidumpGenerator::WriteLwpStream(lwpstatus_t *lsp, MDRawThread *lwp) {
 
226
// Write information about a crashed Lwp.
 
227
// When a lwp crash, kernel will write something on the stack for processing
 
228
// signal. This makes the current stack not reliable, and our stack walker
 
229
// won't figure out the whole call stack for this. So we write the stack at the
 
230
// time of the crash into the minidump file, not the current stack.
 
231
bool WriteCrashedLwpStream(MinidumpFileWriter *minidump_writer,
 
232
                           const WriterArgument *writer_args,
 
233
                           const lwpstatus_t *lsp,
 
234
                           MDRawThread *lwp) {
 
235
  assert(writer_args->sig_ctx != NULL);
 
236
 
 
237
  lwp->thread_id = lsp->pr_lwpid;
 
238
 
 
239
#if TARGET_CPU_SPARC
 
240
  UntypedMDRVA memory(minidump_writer);
 
241
  if (!WriteLwpStack(writer_args->lwp_lister,
 
242
                     writer_args->sig_ctx->uc_mcontext.gregs[REG_O6],
 
243
                     &memory,
 
244
                     &lwp->stack))
 
245
    return false;
 
246
 
 
247
  TypedMDRVA<MDRawContextSPARC> context(minidump_writer);
 
248
  if (!context.Allocate())
 
249
    return false;
 
250
  lwp->thread_context = context.location();
 
251
  memset(context.get(), 0, sizeof(MDRawContextSPARC));
 
252
  return WriteContext(context.get(), writer_args->sig_ctx);
 
253
#elif TARGET_CPU_X86
 
254
  UntypedMDRVA memory(minidump_writer);
 
255
  if (!WriteLwpStack(writer_args->lwp_lister,
 
256
                     writer_args->sig_ctx->uc_mcontext.gregs[UESP],
 
257
                     &memory,
 
258
                     &lwp->stack))
 
259
    return false;
 
260
 
 
261
  TypedMDRVA<MDRawContextX86> context(minidump_writer);
 
262
  if (!context.Allocate())
 
263
    return false;
 
264
  lwp->thread_context = context.location();
 
265
  memset(context.get(), 0, sizeof(MDRawContextX86));
 
266
  return WriteContext(context.get(),
 
267
                      (int *)&writer_args->sig_ctx->uc_mcontext.gregs,
 
268
                      &writer_args->sig_ctx->uc_mcontext.fpregs);
 
269
#endif
 
270
}
 
271
 
 
272
bool WriteLwpStream(MinidumpFileWriter *minidump_writer,
 
273
                    const SolarisLwp *lwp_lister,
 
274
                    const lwpstatus_t *lsp, MDRawThread *lwp) {
128
275
  prfpregset_t fp_regs = lsp->pr_fpreg;
129
 
  prgregset_t *gregs = &(lsp->pr_reg);
130
 
  UntypedMDRVA memory(&writer_);
 
276
  const prgregset_t *gregs = &(lsp->pr_reg);
 
277
  UntypedMDRVA memory(minidump_writer);
131
278
#if TARGET_CPU_SPARC
132
 
  if (!WriteLwpStack((*gregs)[R_SP],
 
279
  if (!WriteLwpStack(lwp_lister,
 
280
                     (*gregs)[R_SP],
133
281
                     &memory,
134
282
                     &lwp->stack))
135
283
    return false;
136
284
 
137
285
  // Write context
138
 
  TypedMDRVA<MDRawContextSPARC> context(&writer_);
 
286
  TypedMDRVA<MDRawContextSPARC> context(minidump_writer);
139
287
  if (!context.Allocate())
140
288
    return false;
141
289
  // should be the thread_id
143
291
  lwp->thread_context = context.location();
144
292
  memset(context.get(), 0, sizeof(MDRawContextSPARC));
145
293
#elif TARGET_CPU_X86
146
 
  if (!WriteLwpStack((*gregs)[UESP],
 
294
  if (!WriteLwpStack(lwp_lister,
 
295
                     (*gregs)[UESP],
147
296
                     &memory,
148
297
                     &lwp->stack))
149
298
  return false;
150
299
 
151
300
  // Write context
152
 
  TypedMDRVA<MDRawContextX86> context(&writer_);
 
301
  TypedMDRVA<MDRawContextX86> context(minidump_writer);
153
302
  if (!context.Allocate())
154
303
    return false;
155
304
  // should be the thread_id
160
309
  return WriteContext(context.get(), (int *)gregs, &fp_regs);
161
310
}
162
311
 
163
 
bool MinidumpGenerator::WriteCPUInformation(MDRawSystemInfo *sys_info) {
 
312
bool WriteCPUInformation(MDRawSystemInfo *sys_info) {
164
313
  struct utsname uts;
165
314
  char *major, *minor, *build;
166
315
 
188
337
  return true;
189
338
}
190
339
 
191
 
bool MinidumpGenerator::WriteOSInformation(MDRawSystemInfo *sys_info) {
 
340
bool WriteOSInformation(MinidumpFileWriter *minidump_writer,
 
341
                        MDRawSystemInfo *sys_info) {
192
342
  sys_info->platform_id = MD_OS_SOLARIS;
193
343
 
194
344
  struct utsname uts;
220
370
    }
221
371
 
222
372
    MDLocationDescriptor location;
223
 
    if (!writer_.WriteString(os_version, 0, &location))
 
373
    if (!minidump_writer->WriteString(os_version, 0, &location))
224
374
      return false;
225
375
    sys_info->csd_version_rva = location.rva;
226
376
  }
229
379
 
230
380
// Callback context for get writting lwp information.
231
381
struct LwpInfoCallbackCtx {
232
 
  MinidumpGenerator *generator;
 
382
  MinidumpFileWriter *minidump_writer;
 
383
  const WriterArgument *writer_args;
233
384
  TypedMDRVA<MDRawThreadList> *list;
234
385
  int lwp_index;
235
386
};
236
387
 
237
388
bool LwpInformationCallback(lwpstatus_t *lsp, void *context) {
238
389
  bool success = true;
239
 
  // The current thread is the one to handle the crash. Ignore it.
 
390
  LwpInfoCallbackCtx *callback_context =
 
391
    static_cast<LwpInfoCallbackCtx *>(context);
 
392
 
 
393
  // The current lwp is the one to handle the crash. Ignore it.
240
394
  if (lsp->pr_lwpid != pthread_self()) {
241
395
    LwpInfoCallbackCtx *callback_context =
242
396
      static_cast<LwpInfoCallbackCtx *>(context);
243
397
    MDRawThread lwp;
244
398
    memset(&lwp, 0, sizeof(MDRawThread));
245
399
 
246
 
    success = callback_context->generator->WriteLwpStream(lsp, &lwp);
 
400
    if (lsp->pr_lwpid != callback_context->writer_args->crashed_lwpid ||
 
401
        callback_context->writer_args->sig_ctx == NULL) {
 
402
      success = WriteLwpStream(callback_context->minidump_writer,
 
403
                               callback_context->writer_args->lwp_lister,
 
404
                               lsp, &lwp);
 
405
    } else {
 
406
      success = WriteCrashedLwpStream(callback_context->minidump_writer,
 
407
                                      callback_context->writer_args,
 
408
                                      lsp, &lwp);
 
409
    }
247
410
    if (success) {
248
411
      callback_context->list->CopyIndexAfterObject(
249
412
          callback_context->lwp_index++,
254
417
  return success;
255
418
}
256
419
 
257
 
bool MinidumpGenerator::WriteLwpListStream(MDRawDirectory *dir) {
 
420
bool WriteLwpListStream(MinidumpFileWriter *minidump_writer,
 
421
                        const WriterArgument *writer_args,
 
422
                        MDRawDirectory *dir) {
258
423
  // Get the lwp information.
259
 
  int lwp_count = lwp_lister_->GetLwpCount();
 
424
  const SolarisLwp *lwp_lister = writer_args->lwp_lister;
 
425
  int lwp_count = lwp_lister->GetLwpCount();
260
426
  if (lwp_count < 0)
261
427
    return false;
262
 
  TypedMDRVA<MDRawThreadList> list(&writer_);
 
428
  TypedMDRVA<MDRawThreadList> list(minidump_writer);
263
429
  if (!list.AllocateObjectAndArray(lwp_count - 1, sizeof(MDRawThread)))
264
430
    return false;
265
431
  dir->stream_type = MD_THREAD_LIST_STREAM;
267
433
  list.get()->number_of_threads = lwp_count - 1;
268
434
 
269
435
  LwpInfoCallbackCtx context;
270
 
  context.generator = this;
 
436
  context.minidump_writer = minidump_writer;
 
437
  context.writer_args = writer_args;
271
438
  context.list = &list;
272
439
  context.lwp_index = 0;
273
440
  CallbackParam<LwpCallback> callback_param(LwpInformationCallback,
274
441
                                            &context);
275
442
  int written =
276
 
    lwp_lister_->Lwp_iter_all(lwp_lister_->getpid(), &callback_param);
 
443
    lwp_lister->Lwp_iter_all(lwp_lister->getpid(), &callback_param);
277
444
  return written == lwp_count;
278
445
}
279
446
 
280
 
bool MinidumpGenerator::WriteCVRecord(MDRawModule *module,
281
 
                                      const char *module_path) {
282
 
  TypedMDRVA<MDCVInfoPDB70> cv(&writer_);
 
447
bool WriteCVRecord(MinidumpFileWriter *minidump_writer,
 
448
                   MDRawModule *module,
 
449
                   const char *module_path,
 
450
                   char *realname) {
 
451
  TypedMDRVA<MDCVInfoPDB70> cv(minidump_writer);
283
452
 
284
453
  char path[PATH_MAX];
285
454
  const char *module_name = module_path ? module_path : "<Unknown>";
286
455
  snprintf(path, sizeof(path), "/proc/self/object/%s", module_name);
287
456
 
288
 
  size_t module_name_length = strlen(module_name);
 
457
  size_t module_name_length = strlen(realname);
289
458
  if (!cv.AllocateObjectAndArray(module_name_length + 1, sizeof(u_int8_t)))
290
459
    return false;
291
 
  if (!cv.CopyIndexAfterObject(0, const_cast<char *>(module_name),
292
 
                               module_name_length)) {
 
460
  if (!cv.CopyIndexAfterObject(0, realname, module_name_length))
293
461
    return false;
294
 
  }
295
462
 
296
463
  module->cv_record = cv.location();
297
464
  MDCVInfoPDB70 *cv_ptr = cv.get();
322
489
}
323
490
 
324
491
struct ModuleInfoCallbackCtx {
325
 
  MinidumpGenerator *generator;
326
492
  MinidumpFileWriter *minidump_writer;
 
493
  const WriterArgument *writer_args;
327
494
  TypedMDRVA<MDRawModuleList> *list;
328
495
  int module_index;
329
496
};
338
505
  MDRawModule module;
339
506
  memset(&module, 0, sizeof(module));
340
507
  MDLocationDescriptor loc;
341
 
  if (!callback_context->minidump_writer->WriteString(module_info.name,
342
 
                                                      0, &loc)) {
343
 
    return false;
344
 
  }
 
508
  char path[PATH_MAX];
 
509
  char buf[PATH_MAX];
 
510
  char *realname;
 
511
  int count;
 
512
 
 
513
  snprintf(path, sizeof (path), "/proc/self/path/%s", module_info.name);
 
514
  if ((count = readlink(path, buf, PATH_MAX)) < 0)
 
515
    return false;
 
516
  buf[count] = '\0';
 
517
 
 
518
  if ((realname = strrchr(buf, '/')) == NULL)
 
519
    return false;
 
520
  realname++;
 
521
 
 
522
  if (!callback_context->minidump_writer->WriteString(realname, 0, &loc))
 
523
    return false;
345
524
 
346
525
  module.base_of_image = (u_int64_t)module_info.start_addr;
347
526
  module.size_of_image = module_info.size;
348
527
  module.module_name_rva = loc.rva;
349
528
 
350
 
  if (!callback_context->generator->WriteCVRecord(&module, module_info.name))
 
529
  if (!WriteCVRecord(callback_context->minidump_writer, &module,
 
530
                     module_info.name, realname))
351
531
    return false;
352
532
 
353
533
  callback_context->list->CopyIndexAfterObject(
355
535
  return true;
356
536
}
357
537
 
358
 
bool MinidumpGenerator::WriteModuleListStream(MDRawDirectory *dir) {
359
 
  TypedMDRVA<MDRawModuleList> list(&writer_);
360
 
  int module_count = lwp_lister_->GetModuleCount();
 
538
bool WriteModuleListStream(MinidumpFileWriter *minidump_writer,
 
539
                           const WriterArgument *writer_args,
 
540
                           MDRawDirectory *dir) {
 
541
  TypedMDRVA<MDRawModuleList> list(minidump_writer);
 
542
  int module_count = writer_args->lwp_lister->GetModuleCount();
361
543
 
362
544
  if (module_count <= 0 ||
363
545
      !list.AllocateObjectAndArray(module_count, MD_MODULE_SIZE)) {
368
550
  dir->location = list.location();
369
551
  list.get()->number_of_modules = module_count;
370
552
  ModuleInfoCallbackCtx context;
371
 
  context.generator = this;
372
 
  context.minidump_writer = &writer_;
 
553
  context.minidump_writer = minidump_writer;
 
554
  context.writer_args = writer_args;
373
555
  context.list = &list;
374
556
  context.module_index = 0;
375
557
  CallbackParam<ModuleCallback> callback(ModuleInfoCallback, &context);
376
 
  return lwp_lister_->ListModules(&callback) == module_count;
 
558
  return writer_args->lwp_lister->ListModules(&callback) == module_count;
377
559
}
378
560
 
379
 
bool MinidumpGenerator::WriteSystemInfoStream(MDRawDirectory *dir) {
380
 
  TypedMDRVA<MDRawSystemInfo> sys_info(&writer_);
 
561
bool WriteSystemInfoStream(MinidumpFileWriter *minidump_writer,
 
562
                           const WriterArgument *writer_args,
 
563
                           MDRawDirectory *dir) {
 
564
  TypedMDRVA<MDRawSystemInfo> sys_info(minidump_writer);
381
565
 
382
566
  if (!sys_info.Allocate())
383
567
    return false;
386
570
  dir->location = sys_info.location();
387
571
 
388
572
  return WriteCPUInformation(sys_info.get()) &&
389
 
         WriteOSInformation(sys_info.get());
 
573
         WriteOSInformation(minidump_writer, sys_info.get());
390
574
}
391
575
 
392
 
bool MinidumpGenerator::WriteExceptionStream(MDRawDirectory *dir) {
393
 
  ucontext_t uc;
394
 
  gregset_t *gregs;
395
 
  fpregset_t fp_regs;
396
 
 
397
 
  if (getcontext(&uc) != 0)
 
576
bool WriteExceptionStream(MinidumpFileWriter *minidump_writer,
 
577
                          const WriterArgument *writer_args,
 
578
                          MDRawDirectory *dir) {
 
579
  // This happenes when this is not a crash, but a requested dump.
 
580
  if (writer_args->sig_ctx == NULL)
398
581
    return false;
399
582
 
400
 
  TypedMDRVA<MDRawExceptionStream> exception(&writer_);
 
583
  TypedMDRVA<MDRawExceptionStream> exception(minidump_writer);
401
584
  if (!exception.Allocate())
402
585
    return false;
403
586
 
404
587
  dir->stream_type = MD_EXCEPTION_STREAM;
405
588
  dir->location = exception.location();
406
 
  exception.get()->thread_id = requester_pid_;
407
 
  exception.get()->exception_record.exception_code = signo_;
 
589
  exception.get()->thread_id = writer_args->crashed_lwpid;
 
590
  exception.get()->exception_record.exception_code = writer_args->signo;
408
591
  exception.get()->exception_record.exception_flags = 0;
409
592
 
410
 
  gregs = &(uc.uc_mcontext.gregs);
411
 
  fp_regs = uc.uc_mcontext.fpregs;
412
593
#if TARGET_CPU_SPARC
413
 
  exception.get()->exception_record.exception_address = ((unsigned int *)gregs)[1];
 
594
  if (writer_args->sig_ctx != NULL) {
 
595
    exception.get()->exception_record.exception_address = 
 
596
      writer_args->sig_ctx->uc_mcontext.gregs[REG_PC];
 
597
  } else {
 
598
    return true;
 
599
  }
 
600
 
414
601
  // Write context of the exception.
415
 
  TypedMDRVA<MDRawContextSPARC> context(&writer_);
 
602
  TypedMDRVA<MDRawContextSPARC> context(minidump_writer);
416
603
  if (!context.Allocate())
417
604
    return false;
418
605
  exception.get()->thread_context = context.location();
419
606
  memset(context.get(), 0, sizeof(MDRawContextSPARC));
420
 
  
421
 
  // On Solaris i386, gregset_t = prgregset_t, fpregset_t = prfpregset_t
422
 
  // But on Solaris Sparc are diffrent, see sys/regset.h and sys/procfs_isa.h
423
 
  context.get()->context_flags = MD_CONTEXT_SPARC_FULL;
424
 
  context.get()->ccr = ((unsigned int *)gregs)[0];
425
 
  context.get()->pc = ((unsigned int *)gregs)[1];
426
 
  context.get()->npc = ((unsigned int *)gregs)[2];
427
 
  context.get()->y = ((unsigned int *)gregs)[3];
428
 
  context.get()->asi = ((unsigned int *)gregs)[19];
429
 
  context.get()->fprs = ((unsigned int *)gregs)[20];
430
 
  for (int i = 0; i < 32; ++i) {
431
 
    context.get()->g_r[i] = 0;
432
 
  }
433
 
  for (int i = 1; i < 16; ++i) {
434
 
    context.get()->g_r[i] = ((unsigned int *)gregs)[i + 3];
435
 
  }
436
 
 
437
 
  return true;
 
607
  return WriteContext(context.get(), writer_args->sig_ctx);
438
608
#elif TARGET_CPU_X86
439
 
  exception.get()->exception_record.exception_address = (*gregs)[EIP];
 
609
  if (writer_args->sig_ctx != NULL) {
 
610
    exception.get()->exception_record.exception_address =
 
611
      writer_args->sig_ctx->uc_mcontext.gregs[EIP];
 
612
  } else {
 
613
    return true;
 
614
  }
 
615
 
440
616
  // Write context of the exception.
441
 
  TypedMDRVA<MDRawContextX86> context(&writer_);
 
617
  TypedMDRVA<MDRawContextX86> context(minidump_writer);
442
618
  if (!context.Allocate())
443
619
    return false;
444
620
  exception.get()->thread_context = context.location();
445
621
  memset(context.get(), 0, sizeof(MDRawContextX86));
446
 
  return WriteContext(context.get(), (int *)gregs, &fp_regs);
447
 
#endif /* TARGET_CPU_XXX */
 
622
  return WriteContext(context.get(),
 
623
                      (int *)&writer_args->sig_ctx->uc_mcontext.gregs,
 
624
                      NULL);
 
625
#endif
448
626
}
449
627
 
450
 
bool MinidumpGenerator::WriteMiscInfoStream(MDRawDirectory *dir) {
451
 
  TypedMDRVA<MDRawMiscInfo> info(&writer_);
 
628
bool WriteMiscInfoStream(MinidumpFileWriter *minidump_writer,
 
629
                         const WriterArgument *writer_args,
 
630
                         MDRawDirectory *dir) {
 
631
  TypedMDRVA<MDRawMiscInfo> info(minidump_writer);
452
632
 
453
633
  if (!info.Allocate())
454
634
    return false;
457
637
  dir->location = info.location();
458
638
  info.get()->size_of_info = sizeof(MDRawMiscInfo);
459
639
  info.get()->flags1 = MD_MISCINFO_FLAGS1_PROCESS_ID;
460
 
  info.get()->process_id = requester_pid_;
 
640
  info.get()->process_id = writer_args->requester_pid;
461
641
 
462
642
  return true;
463
643
}
464
644
 
465
 
bool MinidumpGenerator::WriteBreakpadInfoStream(MDRawDirectory *dir) {
466
 
  TypedMDRVA<MDRawBreakpadInfo> info(&writer_);
 
645
bool WriteBreakpadInfoStream(MinidumpFileWriter *minidump_writer,
 
646
                             const WriterArgument *writer_args,
 
647
                             MDRawDirectory *dir) {
 
648
  TypedMDRVA<MDRawBreakpadInfo> info(minidump_writer);
467
649
 
468
650
  if (!info.Allocate())
469
651
    return false;
474
656
  info.get()->validity = MD_BREAKPAD_INFO_VALID_DUMP_THREAD_ID |
475
657
                         MD_BREAKPAD_INFO_VALID_REQUESTING_THREAD_ID;
476
658
  info.get()->dump_thread_id = getpid();
477
 
  info.get()->requesting_thread_id = requester_pid_;
 
659
  info.get()->requesting_thread_id = writer_args->requester_pid;
478
660
  return true;
479
661
}
480
662
 
486
668
  SolarisLwp *lwp_;
487
669
};
488
670
 
 
671
// Prototype of writer functions.
 
672
typedef bool (*WriteStreamFN)(MinidumpFileWriter *,
 
673
                              const WriterArgument *,
 
674
                              MDRawDirectory *);
 
675
 
 
676
// Function table to writer a full minidump.
 
677
const WriteStreamFN writers[] = {
 
678
  WriteLwpListStream,
 
679
  WriteModuleListStream,
 
680
  WriteSystemInfoStream,
 
681
  WriteExceptionStream,
 
682
  WriteMiscInfoStream,
 
683
  WriteBreakpadInfoStream,
 
684
};
 
685
 
489
686
// Will call each writer function in the writers table.
490
 
void* MinidumpGenerator::Write() {
491
 
  // Function table to writer a full minidump.
492
 
  const WriteStreamFN writers[] = {
493
 
    &MinidumpGenerator::WriteLwpListStream,
494
 
    &MinidumpGenerator::WriteModuleListStream,
495
 
    &MinidumpGenerator::WriteSystemInfoStream,
496
 
    &MinidumpGenerator::WriteExceptionStream,
497
 
    &MinidumpGenerator::WriteMiscInfoStream,
498
 
    &MinidumpGenerator::WriteBreakpadInfoStream,
499
 
  };
 
687
//void* MinidumpGenerator::Write(void *argument) {
 
688
void* Write(void *argument) {
 
689
  WriterArgument *writer_args = static_cast<WriterArgument *>(argument);
500
690
 
501
 
  if (!lwp_lister_->ControlAllLwps(true))
 
691
  if (!writer_args->lwp_lister->ControlAllLwps(true))
502
692
    return NULL;
503
693
 
504
 
  AutoLwpResumer lwpResumer(lwp_lister_);
505
 
 
506
 
  TypedMDRVA<MDRawHeader> header(&writer_);
507
 
  TypedMDRVA<MDRawDirectory> dir(&writer_);
 
694
  AutoLwpResumer lwpResumer(writer_args->lwp_lister);
 
695
 
 
696
  if (writer_args->sighandler_ebp != 0 &&
 
697
      writer_args->lwp_lister->FindSigContext(writer_args->sighandler_ebp,
 
698
                                              &writer_args->sig_ctx)) {
 
699
    writer_args->crashed_stack_bottom = 
 
700
      writer_args->lwp_lister->GetLwpStackBottom(
 
701
#if TARGET_CPU_SPARC
 
702
          writer_args->sig_ctx->uc_mcontext.gregs[REG_O6]
 
703
#elif TARGET_CPU_X86
 
704
          writer_args->sig_ctx->uc_mcontext.gregs[UESP]
 
705
#endif
 
706
      );
 
707
 
 
708
    int crashed_lwpid = FindCrashingLwp(writer_args->crashed_stack_bottom,
 
709
                                        writer_args->requester_pid,
 
710
                                        writer_args->lwp_lister);
 
711
    if (crashed_lwpid > 0)
 
712
      writer_args->crashed_lwpid = crashed_lwpid;
 
713
  }
 
714
 
 
715
  MinidumpFileWriter *minidump_writer = writer_args->minidump_writer;
 
716
  TypedMDRVA<MDRawHeader> header(minidump_writer);
 
717
  TypedMDRVA<MDRawDirectory> dir(minidump_writer);
508
718
  if (!header.Allocate())
509
719
    return 0;
510
720
 
521
731
  int dir_index = 0;
522
732
  MDRawDirectory local_dir;
523
733
  for (int i = 0; i < writer_count; ++i) {
524
 
    if ((this->*writers[i])(&local_dir))
 
734
    if ((*writers[i])(minidump_writer, writer_args, &local_dir))
525
735
      dir.CopyIndex(dir_index++, &local_dir);
526
736
  }
527
737
 
528
738
  return 0;
529
739
}
530
740
 
 
741
}  // namespace
 
742
 
 
743
namespace google_breakpad {
 
744
 
 
745
MinidumpGenerator::MinidumpGenerator() {
 
746
}
 
747
 
 
748
MinidumpGenerator::~MinidumpGenerator() {
 
749
}
 
750
 
531
751
// Write minidump into file.
532
752
// It runs in a different thread from the crashing thread.
533
753
bool MinidumpGenerator::WriteMinidumpToFile(const char *file_pathname,
534
 
                                            int signo) {
 
754
                                            int signo,
 
755
                                            uintptr_t sighandler_ebp,
 
756
                                            ucontext_t **sig_ctx) const {
 
757
  // The exception handler thread.
 
758
  pthread_t handler_thread;
 
759
 
535
760
  assert(file_pathname != NULL);
536
 
  assert(stack_ != NULL);
537
761
 
538
762
  if (file_pathname == NULL)
539
763
    return false;
540
764
 
541
 
  if (writer_.Open(file_pathname)) {
 
765
  MinidumpFileWriter minidump_writer;
 
766
  if (minidump_writer.Open(file_pathname)) {
 
767
    WriterArgument argument;
 
768
    memset(&argument, 0, sizeof(argument));
542
769
    SolarisLwp lwp_lister(getpid());
543
 
    lwp_lister_ = &lwp_lister;
544
 
    requester_pid_ = getpid();
545
 
    signo_ = signo;
546
 
    if (Write())
547
 
      return true;
 
770
    argument.lwp_lister = &lwp_lister;
 
771
    argument.minidump_writer = &minidump_writer;
 
772
    argument.requester_pid = getpid();
 
773
    argument.crashed_lwpid = pthread_self();
 
774
    argument.signo = signo;
 
775
    argument.sighandler_ebp = sighandler_ebp;
 
776
    argument.sig_ctx = NULL;
 
777
 
 
778
    pthread_create(&handler_thread, NULL, Write, (void *)&argument);
 
779
    pthread_join(handler_thread, NULL);
 
780
    return true;
548
781
  }
549
782
 
550
783
  return false;