jdk

Форк
0
/
porting_aix.cpp 
1190 строк · 37.0 Кб
1
/*
2
 * Copyright (c) 2012, 2024 SAP SE. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 *
23
 */
24
// needs to be defined first, so that the implicit loaded xcoff.h header defines
25
// the right structures to analyze the loader header of 64 Bit executable files
26
// this is needed for rtv_linkedin_libpath() to get the linked (burned) in library
27
// search path of an XCOFF executable
28
#define __XCOFF64__
29
#include <xcoff.h>
30

31
#include "asm/assembler.hpp"
32
#include "compiler/disassembler.hpp"
33
#include "loadlib_aix.hpp"
34
#include "memory/allocation.hpp"
35
#include "memory/allocation.inline.hpp"
36
#include "misc_aix.hpp"
37
#include "porting_aix.hpp"
38
#include "runtime/javaThread.hpp"
39
#include "runtime/os.hpp"
40
#include "utilities/align.hpp"
41
#include "utilities/debug.hpp"
42
#include <cxxabi.h>
43
#include <sys/debug.h>
44
#include <pthread.h>
45
#include <ucontext.h>
46

47
//////////////////////////////////
48
// Provide implementation for dladdr based on LoadedLibraries pool and
49
// traceback table scan
50

51
// Search traceback table in stack,
52
// return procedure name from trace back table.
53
#define MAX_FUNC_SEARCH_LEN 0x10000
54

55
#define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2))
56

57
// Typedefs for stackslots, stack pointers, pointers to op codes.
58
typedef unsigned long stackslot_t;
59
typedef stackslot_t* stackptr_t;
60
typedef unsigned int* codeptr_t;
61

62
// Unfortunately, the interface of dladdr makes the implementer
63
// responsible for maintaining memory for function name/library
64
// name. I guess this is because most OS's keep those values as part
65
// of the mapped executable image ready to use. On AIX, this doesn't
66
// work, so I have to keep the returned strings. For now, I do this in
67
// a primitive string map. Should this turn out to be a performance
68
// problem, a better hashmap has to be used.
69
class fixed_strings {
70
  struct node : public CHeapObj<mtInternal> {
71
    char* v;
72
    node* next;
73
  };
74

75
  node* first;
76

77
  public:
78

79
  fixed_strings() : first(0) {}
80
  ~fixed_strings() {
81
    node* n = first;
82
    while (n) {
83
      node* p = n;
84
      n = n->next;
85
      os::free(p->v);
86
      delete p;
87
    }
88
  }
89

90
  char* intern(const char* s) {
91
    for (node* n = first; n; n = n->next) {
92
      if (strcmp(n->v, s) == 0) {
93
        return n->v;
94
      }
95
    }
96
    node* p = new node;
97
    p->v = os::strdup_check_oom(s);
98
    p->next = first;
99
    first = p;
100
    return p->v;
101
  }
102
};
103

104
static fixed_strings dladdr_fixed_strings;
105

106
bool AixSymbols::get_function_name (
107
    address pc0,                     // [in] program counter
108
    char* p_name, size_t namelen,    // [out] optional: function name ("" if not available)
109
    int* p_displacement,             // [out] optional: displacement (-1 if not available)
110
    const struct tbtable** p_tb,     // [out] optional: ptr to traceback table to get further
111
                                     //                 information (null if not available)
112
    bool demangle                    // [in] whether to demangle the name
113
  ) {
114
  struct tbtable* tb = 0;
115
  unsigned int searchcount = 0;
116

117
  // initialize output parameters
118
  if (p_name && namelen > 0) {
119
    *p_name = '\0';
120
  }
121
  if (p_displacement) {
122
    *p_displacement = -1;
123
  }
124
  if (p_tb) {
125
    *p_tb = nullptr;
126
  }
127

128
  codeptr_t pc = (codeptr_t)pc0;
129

130
  // weed out obvious bogus states
131
  if (pc < (codeptr_t)0x1000) {
132
    trcVerbose("invalid program counter");
133
    return false;
134
  }
135

136
  // We see random but frequent crashes in this function since some months mainly on shutdown
137
  // (-XX:+DumpInfoAtExit). It appears the page we are reading is randomly disappearing while
138
  // we read it (?).
139
  // As the pc cannot be trusted to be anything sensible lets make all reads via SafeFetch. Also
140
  // bail if this is not a text address right now.
141
  if (!LoadedLibraries::find_for_text_address(pc, nullptr)) {
142
    trcVerbose("not a text address");
143
    return false;
144
  }
145

146
  // .. (Note that is_readable_pointer returns true if safefetch stubs are not there yet;
147
  // in that case I try reading the traceback table unsafe - I rather risk secondary crashes in
148
  // error files than not having a callstack.)
149
#define CHECK_POINTER_READABLE(p) \
150
  if (!os::is_readable_pointer(p)) { \
151
    trcVerbose("pc not readable"); \
152
    return false; \
153
  }
154

155
  codeptr_t pc2 = (codeptr_t) pc;
156

157
  // Make sure the pointer is word aligned.
158
  pc2 = (codeptr_t) align_up((char*)pc2, 4);
159
  CHECK_POINTER_READABLE(pc2)
160

161
  // Find start of traceback table.
162
  // (starts after code, is marked by word-aligned (32bit) zeros)
163
  while ((*pc2 != 0) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) {
164
    CHECK_POINTER_READABLE(pc2)
165
    pc2++;
166
  }
167
  if (*pc2 != 0) {
168
    trcVerbose("no traceback table found");
169
    return false;
170
  }
171
  //
172
  // Set up addressability to the traceback table
173
  //
174
  tb = (struct tbtable*) (pc2 + 1);
175

176
  // Is this really a traceback table? No way to be sure but
177
  // some indicators we can check.
178
  if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) {
179
    // Language specifiers, go from 0 (C) to 14 (Objective C).
180
    // According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm.
181
    trcVerbose("no traceback table found");
182
    return false;
183
  }
184

185
  // Existence of fields in the tbtable extension are contingent upon
186
  // specific fields in the base table.  Check for their existence so
187
  // that we can address the function name if it exists.
188
  pc2 = (codeptr_t) tb +
189
    sizeof(struct tbtable_short)/sizeof(int);
190
  if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0)
191
    pc2++;
192

193
  CHECK_POINTER_READABLE(pc2)
194

195
  if (tb->tb.has_tboff == TRUE) {
196

197
    // I want to know the displacement
198
    const unsigned int tb_offset = *pc2;
199
    codeptr_t start_of_procedure =
200
    (codeptr_t)(((char*)tb) - 4 - tb_offset);  // (-4 to omit leading 0000)
201

202
    // Weed out the cases where we did find the wrong traceback table.
203
    if (pc < start_of_procedure) {
204
      trcVerbose("no traceback table found");
205
      return false;
206
    }
207

208
    // return the displacement
209
    if (p_displacement) {
210
      (*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure);
211
    }
212

213
    pc2++;
214
  } else {
215
    // return -1 for displacement
216
    if (p_displacement) {
217
      (*p_displacement) = -1;
218
    }
219
  }
220

221
  if (tb->tb.int_hndl == TRUE)
222
    pc2++;
223

224
  if (tb->tb.has_ctl == TRUE)
225
    pc2 += (*pc2) + 1; // don't care
226

227
  CHECK_POINTER_READABLE(pc2)
228

229
  //
230
  // return function name if it exists.
231
  //
232
  if (p_name && namelen > 0) {
233
    if (tb->tb.name_present) {
234
      // Copy name from text because it may not be zero terminated.
235
      const short l = MIN2<short>(*((short*)pc2), namelen - 1);
236
      // Be very careful.
237
      int i = 0; char* const p = (char*)pc2 + sizeof(short);
238
      while (i < l && os::is_readable_pointer(p + i)) {
239
        p_name[i] = p[i];
240
        i++;
241
      }
242
      p_name[i] = '\0';
243

244
      // If it is a C++ name, try and demangle it using the __cxa_demangle interface(see demangle.h).
245
      if (demangle) {
246
        int status;
247
        char *demangled_name = abi::__cxa_demangle(p_name, nullptr, nullptr, &status);
248
        if ((demangled_name != nullptr) && (status == 0)) {
249
          strncpy(p_name, demangled_name, namelen-1);
250
          p_name[namelen-1] = '\0';
251
        }
252
        if (demangled_name != nullptr) {
253
          ALLOW_C_FUNCTION(::free, ::free(demangled_name));
254
        }
255
      }
256
    } else {
257
      strncpy(p_name, "<nameless function>", namelen-1);
258
      p_name[namelen-1] = '\0';
259
    }
260
  }
261

262
  // Return traceback table, if user wants it.
263
  if (p_tb) {
264
    (*p_tb) = tb;
265
  }
266

267
  return true;
268

269
}
270

271
bool AixSymbols::get_module_name(address pc,
272
                         char* p_name, size_t namelen) {
273

274
  if (p_name && namelen > 0) {
275
    p_name[0] = '\0';
276
    loaded_module_t lm;
277
    if (LoadedLibraries::find_for_text_address(pc, &lm)) {
278
      strncpy(p_name, lm.shortname, namelen);
279
      p_name[namelen - 1] = '\0';
280
      return true;
281
    }
282
  }
283

284
  return false;
285
}
286

287
bool AixSymbols::get_module_name_and_base(address pc,
288
                         char* p_name, size_t namelen,
289
                         address* p_base) {
290

291
  if (p_base && p_name && namelen > 0) {
292
    p_name[0] = '\0';
293
    loaded_module_t lm;
294
    if (LoadedLibraries::find_for_text_address(pc, &lm)) {
295
      strncpy(p_name, lm.shortname, namelen);
296
      p_name[namelen - 1] = '\0';
297
      *p_base = (address) lm.text;
298
      return true;
299
    }
300
  }
301

302
  return false;
303
}
304

305
// Special implementation of dladdr for Aix based on LoadedLibraries
306
// Note: dladdr returns non-zero for ok, 0 for error!
307
// Note: dladdr is not posix, but a non-standard GNU extension. So this tries to
308
//   fulfill the contract of dladdr on Linux (see http://linux.die.net/man/3/dladdr)
309
// Note: addr may be both an AIX function descriptor or a real code pointer
310
//   to the entry of a function.
311
extern "C"
312
int dladdr(void* addr, Dl_info* info) {
313

314
  if (!addr) {
315
    return 0;
316
  }
317

318
  assert(info, "");
319

320
  int rc = 0;
321

322
  const char* const ZEROSTRING = "";
323

324
  // Always return a string, even if a "" one. Linux dladdr manpage
325
  // does not say anything about returning null
326
  info->dli_fname = ZEROSTRING;
327
  info->dli_sname = ZEROSTRING;
328
  info->dli_saddr = nullptr;
329

330
  address p = (address) addr;
331
  loaded_module_t lm;
332
  bool found = false;
333

334
  enum { noclue, code, data } type = noclue;
335

336
  trcVerbose("dladdr(%p)...", p);
337

338
  // Note: input address may be a function. I accept both a pointer to
339
  // the entry of a function and a pointer to the function descriptor.
340
  // (see ppc64 ABI)
341
  found = LoadedLibraries::find_for_text_address(p, &lm);
342
  if (found) {
343
    type = code;
344
  }
345

346
  if (!found) {
347
    // Not a pointer into any text segment. Is it a function descriptor?
348
    const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p;
349
    p = pfd->entry();
350
    if (p) {
351
      found = LoadedLibraries::find_for_text_address(p, &lm);
352
      if (found) {
353
        type = code;
354
      }
355
    }
356
  }
357

358
  if (!found) {
359
    // Neither direct code pointer nor function descriptor. A data ptr?
360
    p = (address)addr;
361
    found = LoadedLibraries::find_for_data_address(p, &lm);
362
    if (found) {
363
      type = data;
364
    }
365
  }
366

367
  // If we did find the shared library this address belongs to (either
368
  // code or data segment) resolve library path and, if possible, the
369
  // symbol name.
370
  if (found) {
371

372
    // No need to intern the libpath, that one is already interned one layer below.
373
    info->dli_fname = lm.path;
374

375
    if (type == code) {
376

377
      // For code symbols resolve function name and displacement. Use
378
      // displacement to calc start of function.
379
      char funcname[256] = "";
380
      int displacement = 0;
381

382
      if (AixSymbols::get_function_name(p, funcname, sizeof(funcname),
383
                      &displacement, nullptr, true)) {
384
        if (funcname[0] != '\0') {
385
          const char* const interned = dladdr_fixed_strings.intern(funcname);
386
          info->dli_sname = interned;
387
          trcVerbose("... function name: %s ...", interned);
388
        }
389

390
        // From the displacement calculate the start of the function.
391
        if (displacement != -1) {
392
          info->dli_saddr = p - displacement;
393
        } else {
394
          info->dli_saddr = p;
395
        }
396
      } else {
397

398
        // No traceback table found. Just assume the pointer is it.
399
        info->dli_saddr = p;
400

401
      }
402

403
    } else if (type == data) {
404

405
      // For data symbols.
406
      info->dli_saddr = p;
407

408
    } else {
409
      ShouldNotReachHere();
410
    }
411

412
    rc = 1; // success: return 1 [sic]
413

414
  }
415

416
  // sanity checks.
417
  if (rc) {
418
    assert(info->dli_fname, "");
419
    assert(info->dli_sname, "");
420
    assert(info->dli_saddr, "");
421
  }
422

423
  return rc; // error: return 0 [sic]
424

425
}
426

427
/////////////////////////////////////////////////////////////////////////////
428
// Native callstack dumping
429

430
// Print the traceback table for one stack frame.
431
static void print_tbtable (outputStream* st, const struct tbtable* p_tb) {
432

433
  if (p_tb == nullptr) {
434
    st->print("<null>");
435
    return;
436
  }
437

438
  switch(p_tb->tb.lang) {
439
    case TB_C: st->print("C"); break;
440
    case TB_FORTRAN: st->print("FORTRAN"); break;
441
    case TB_PASCAL: st->print("PASCAL"); break;
442
    case TB_ADA: st->print("ADA"); break;
443
    case TB_PL1: st->print("PL1"); break;
444
    case TB_BASIC: st->print("BASIC"); break;
445
    case TB_LISP: st->print("LISP"); break;
446
    case TB_COBOL: st->print("COBOL"); break;
447
    case TB_MODULA2: st->print("MODULA2"); break;
448
    case TB_CPLUSPLUS: st->print("C++"); break;
449
    case TB_RPG: st->print("RPG"); break;
450
    case TB_PL8: st->print("PL8"); break;
451
    case TB_ASM: st->print("ASM"); break;
452
    case TB_HPJ: st->print("HPJ"); break;
453
    default: st->print("unknown");
454
  }
455
  st->print(" ");
456

457
  if (p_tb->tb.globallink) {
458
    st->print("globallink ");
459
  }
460
  if (p_tb->tb.is_eprol) {
461
    st->print("eprol ");
462
  }
463
  if (p_tb->tb.int_proc) {
464
    st->print("int_proc ");
465
  }
466
  if (p_tb->tb.tocless) {
467
    st->print("tocless ");
468
  }
469
  if (p_tb->tb.fp_present) {
470
    st->print("fp_present ");
471
  }
472
  if (p_tb->tb.int_hndl) {
473
    st->print("interrupt_handler ");
474
  }
475
  if (p_tb->tb.uses_alloca) {
476
    st->print("uses_alloca ");
477
  }
478
  if (p_tb->tb.saves_cr) {
479
    st->print("saves_cr ");
480
  }
481
  if (p_tb->tb.saves_lr) {
482
    st->print("saves_lr ");
483
  }
484
  if (p_tb->tb.stores_bc) {
485
    st->print("stores_bc ");
486
  }
487
  if (p_tb->tb.fixup) {
488
    st->print("fixup ");
489
  }
490
  if (p_tb->tb.fpr_saved > 0) {
491
    st->print("fpr_saved:%d ", p_tb->tb.fpr_saved);
492
  }
493
  if (p_tb->tb.gpr_saved > 0) {
494
    st->print("gpr_saved:%d ", p_tb->tb.gpr_saved);
495
  }
496
  if (p_tb->tb.fixedparms > 0) {
497
    st->print("fixedparms:%d ", p_tb->tb.fixedparms);
498
  }
499
  if (p_tb->tb.floatparms > 0) {
500
    st->print("floatparms:%d ", p_tb->tb.floatparms);
501
  }
502
  if (p_tb->tb.parmsonstk > 0) {
503
    st->print("parmsonstk:%d", p_tb->tb.parmsonstk);
504
  }
505
}
506

507
// Print information for pc (module, function, displacement, traceback table)
508
// on one line.
509
static void print_info_for_pc (outputStream* st, codeptr_t pc, char* buf,
510
                               size_t buf_size, bool demangle) {
511
  const struct tbtable* tb = nullptr;
512
  int displacement = -1;
513

514
  if (!os::is_readable_pointer(pc)) {
515
    st->print("(invalid)");
516
    return;
517
  }
518

519
  if (AixSymbols::get_module_name((address)pc, buf, buf_size)) {
520
    st->print("%s", buf);
521
  } else {
522
    st->print("(unknown module)");
523
  }
524
  st->print("::");
525
  if (AixSymbols::get_function_name((address)pc, buf, buf_size,
526
                                     &displacement, &tb, demangle)) {
527
    st->print("%s", buf);
528
  } else {
529
    st->print("(unknown function)");
530
  }
531
  if (displacement == -1) {
532
    st->print("+?");
533
  } else {
534
    st->print("+0x%x", displacement);
535
  }
536
  if (tb) {
537
    st->fill_to(64);
538
    st->print("  (");
539
    print_tbtable(st, tb);
540
    st->print(")");
541
  }
542
}
543

544
static void print_stackframe(outputStream* st, stackptr_t sp, char* buf,
545
                             size_t buf_size, bool demangle) {
546

547
  stackptr_t sp2 = sp;
548

549
  // skip backchain
550

551
  sp2++;
552

553
  // skip crsave
554

555
  sp2++;
556

557
  // retrieve lrsave. That is the only info I need to get the function/displacement
558

559
  codeptr_t lrsave = (codeptr_t) *(sp2);
560
  st->print (PTR_FORMAT " - " PTR_FORMAT " ", p2i(sp2), p2i(lrsave));
561

562
  if (lrsave != nullptr) {
563
    print_info_for_pc(st, lrsave, buf, buf_size, demangle);
564
  }
565

566
}
567

568
// Function to check a given stack pointer against given stack limits.
569
static bool is_valid_stackpointer(stackptr_t sp, stackptr_t stack_base, size_t stack_size) {
570
  if (((uintptr_t)sp) & 0x7) {
571
    return false;
572
  }
573
  if (sp > stack_base) {
574
    return false;
575
  }
576
  if (sp < (stackptr_t) ((address)stack_base - stack_size)) {
577
    return false;
578
  }
579
  return true;
580
}
581

582
// Returns true if function is a valid codepointer.
583
static bool is_valid_codepointer(codeptr_t p) {
584
  if (!p) {
585
    return false;
586
  }
587
  if (((uintptr_t)p) & 0x3) {
588
    return false;
589
  }
590
  return LoadedLibraries::find_for_text_address(p, nullptr);
591
}
592

593
// Function tries to guess if the given combination of stack pointer, stack base
594
// and stack size is a valid stack frame.
595
static bool is_valid_frame (stackptr_t p, stackptr_t stack_base, size_t stack_size) {
596

597
  if (!is_valid_stackpointer(p, stack_base, stack_size)) {
598
    return false;
599
  }
600

601
  // First check - the occurrence of a valid backchain pointer up the stack, followed by a
602
  // valid codeptr, counts as a good candidate.
603
  stackptr_t sp2 = (stackptr_t) *p;
604
  if (is_valid_stackpointer(sp2, stack_base, stack_size) && // found a valid stack pointer in the stack...
605
     ((sp2 - p) > 6) &&  // ... pointing upwards and not into my frame...
606
     is_valid_codepointer((codeptr_t)(*(sp2 + 2)))) // ... followed by a code pointer after two slots...
607
  {
608
    return true;
609
  }
610

611
  return false;
612
}
613

614
// Try to relocate a stack back chain in a given stack.
615
// Used in callstack dumping, when the backchain is broken by an overwriter
616
static stackptr_t try_find_backchain (stackptr_t last_known_good_frame,
617
                                      stackptr_t stack_base, size_t stack_size)
618
{
619
  if (!is_valid_stackpointer(last_known_good_frame, stack_base, stack_size)) {
620
    return nullptr;
621
  }
622

623
  stackptr_t sp = last_known_good_frame;
624

625
  sp += 6; // Omit next fixed frame slots.
626
  while (sp < stack_base) {
627
    if (is_valid_frame(sp, stack_base, stack_size)) {
628
      return sp;
629
    }
630
    sp ++;
631
  }
632

633
  return nullptr;
634
}
635

636
static void decode_instructions_at_pc(const char* header,
637
                                      codeptr_t pc, int num_before,
638
                                      int num_after, outputStream* st) {
639
  // TODO: PPC port Disassembler::decode(pc, 16, 16, st);
640
}
641

642

643
void AixNativeCallstack::print_callstack_for_context(outputStream* st, const ucontext_t* context,
644
                                                     bool demangle, char* buf, size_t buf_size) {
645

646
#define MAX_CALLSTACK_DEPTH 50
647

648
  unsigned long* sp;
649
  unsigned long* sp_last;
650
  int frame;
651

652
  // To print the first frame, use the current value of iar:
653
  // current entry indicated by iar (the current pc)
654
  codeptr_t cur_iar = 0;
655
  stackptr_t cur_sp = 0;
656
  codeptr_t cur_rtoc = 0;
657
  codeptr_t cur_lr = 0;
658

659
  const ucontext_t* uc = (const ucontext_t*) context;
660

661
  // fallback: use the current context
662
  ucontext_t local_context;
663
  if (!uc) {
664
    st->print_cr("No context given, using current context.");
665
    if (getcontext(&local_context) == 0) {
666
      uc = &local_context;
667
    } else {
668
      st->print_cr("No context given and getcontext failed. ");
669
      return;
670
    }
671
  }
672

673
  cur_iar = (codeptr_t)uc->uc_mcontext.jmp_context.iar;
674
  cur_sp = (stackptr_t)uc->uc_mcontext.jmp_context.gpr[1];
675
  cur_rtoc = (codeptr_t)uc->uc_mcontext.jmp_context.gpr[2];
676
  cur_lr = (codeptr_t)uc->uc_mcontext.jmp_context.lr;
677

678
  // syntax used here:
679
  //  n   --------------   <-- stack_base,   stack_to
680
  //  n-1 |            |
681
  //  ... | older      |
682
  //  ... |   frames   | |
683
  //      |            | | stack grows downward
684
  //  ... | younger    | |
685
  //  ... |   frames   | V
686
  //      |            |
687
  //      |------------|   <-- cur_sp, current stack ptr
688
  //      |            |
689
  //      |  unused    |
690
  //      |    stack   |
691
  //      |            |
692
  //      .            .
693
  //      .            .
694
  //      .            .
695
  //      .            .
696
  //      |            |
697
  //   0  --------------   <-- stack_from
698
  //
699

700
  // Retrieve current stack base, size from the current thread. If there is none,
701
  // retrieve it from the OS.
702
  stackptr_t stack_base = nullptr;
703
  size_t stack_size = 0;
704
  {
705
    AixMisc::stackbounds_t stackbounds;
706
    if (!AixMisc::query_stack_bounds_for_current_thread(&stackbounds)) {
707
      st->print_cr("Cannot retrieve stack bounds.");
708
      return;
709
    }
710
    stack_base = (stackptr_t)stackbounds.base;
711
    stack_size = stackbounds.size;
712
  }
713

714
  st->print_cr("Native frame:");
715
  st->print("iar:  " PTR_FORMAT " ", p2i(cur_iar));
716
  print_info_for_pc(st, cur_iar, buf, buf_size, demangle);
717
  st->cr();
718

719
  if (cur_iar && os::is_readable_pointer(cur_iar)) {
720
    decode_instructions_at_pc(
721
      "Decoded instructions at iar:",
722
      cur_iar, 32, 16, st);
723
  }
724

725
  // Print out lr too, which may be interesting if we did jump to some bogus location;
726
  // in those cases the new frame is not built up yet and the caller location is only
727
  // preserved via lr register.
728
  st->print("lr:   " PTR_FORMAT " ", p2i(cur_lr));
729
  print_info_for_pc(st, cur_lr, buf, buf_size, demangle);
730
  st->cr();
731

732
  if (cur_lr && os::is_readable_pointer(cur_lr)) {
733
    decode_instructions_at_pc(
734
      "Decoded instructions at lr:",
735
      cur_lr, 32, 16, st);
736
  }
737

738
  // Check and print sp.
739
  st->print("sp:   " PTR_FORMAT " ", p2i(cur_sp));
740
  if (!is_valid_stackpointer(cur_sp, stack_base, stack_size)) {
741
    st->print("(invalid) ");
742
    goto cleanup;
743
  } else {
744
    st->print("(base - 0x%lX) ", PTRDIFF_BYTES(stack_base, cur_sp));
745
  }
746
  st->cr();
747

748
  // Check and print rtoc.
749
  st->print("rtoc: "  PTR_FORMAT " ", p2i(cur_rtoc));
750
  if (cur_rtoc == nullptr || cur_rtoc == (codeptr_t)-1 ||
751
      !os::is_readable_pointer(cur_rtoc)) {
752
    st->print("(invalid)");
753
  } else if (((uintptr_t)cur_rtoc) & 0x7) {
754
    st->print("(unaligned)");
755
  }
756
  st->cr();
757

758
  st->print_cr("|---stackaddr----|   |----lrsave------|:   <function name>");
759

760
  ///
761
  // Walk callstack.
762
  //
763
  // (if no context was given, use the current stack)
764
  sp = (unsigned long*)(*(unsigned long*)cur_sp); // Stack pointer
765
  sp_last = cur_sp;
766

767
  frame = 0;
768

769
  while (frame < MAX_CALLSTACK_DEPTH) {
770

771
    // Check sp.
772
    bool retry = false;
773
    if (sp == nullptr) {
774
      // The backchain pointer was null. This normally means the end of the chain. But the
775
      // stack might be corrupted, and it may be worth looking for the stack chain.
776
      if (is_valid_stackpointer(sp_last, stack_base, stack_size) && (stack_base - 0x10) > sp_last) {
777
        // If we are not within <guess> 0x10 stackslots of the stack base, we assume that this
778
        // is indeed not the end of the chain but that the stack was corrupted. So lets try to
779
        // find the end of the chain.
780
        st->print_cr("*** back chain pointer is null - end of stack or broken backchain ? ***");
781
        retry = true;
782
      } else {
783
        st->print_cr("*** end of backchain ***");
784
        goto end_walk_callstack;
785
      }
786
    } else if (!is_valid_stackpointer(sp, stack_base, stack_size)) {
787
      st->print_cr("*** stack pointer invalid - backchain corrupted (" PTR_FORMAT ") ***", p2i(sp));
788
      retry = true;
789
    } else if (sp < sp_last) {
790
      st->print_cr("invalid stack pointer: " PTR_FORMAT " (not monotone raising)", p2i(sp));
791
      retry = true;
792
    }
793

794
    // If backchain is broken, try to recover, by manually scanning the stack for a pattern
795
    // which looks like a valid stack.
796
    if (retry) {
797
      st->print_cr("trying to recover and find backchain...");
798
      sp = try_find_backchain(sp_last, stack_base, stack_size);
799
      if (sp) {
800
        st->print_cr("found something which looks like a backchain at " PTR_FORMAT ", after 0x%lx bytes... ",
801
            p2i(sp), PTRDIFF_BYTES(sp, sp_last));
802
      } else {
803
        st->print_cr("did not find a backchain, giving up.");
804
        goto end_walk_callstack;
805
      }
806
    }
807

808
    // Print stackframe.
809
    print_stackframe(st, sp, buf, buf_size, demangle);
810
    st->cr();
811
    frame ++;
812

813
    // Next stack frame and link area.
814
    sp_last = sp;
815
    sp = (unsigned long*)(*sp);
816
  }
817

818
  // Prevent endless loops in case of invalid callstacks.
819
  if (frame == MAX_CALLSTACK_DEPTH) {
820
    st->print_cr("...(stopping after %d frames.", MAX_CALLSTACK_DEPTH);
821
  }
822

823
end_walk_callstack:
824

825
  st->print_cr("-----------------------");
826

827
cleanup:
828

829
  return;
830

831
}
832

833

834
bool AixMisc::query_stack_bounds_for_current_thread(stackbounds_t* out) {
835

836
  // Information about this api can be found (a) in the pthread.h header and
837
  // (b) in http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/pthread_getthrds_np.htm
838
  //
839
  // The use of this API to find out the current stack is kind of undefined.
840
  // But after a lot of tries and asking IBM about it, I concluded that it is safe
841
  // enough for cases where I let the pthread library create its stacks. For cases
842
  // where I create an own stack and pass this to pthread_create, it seems not to
843
  // work (the returned stack size in that case is 0).
844

845
  pthread_t tid = pthread_self();
846
  struct __pthrdsinfo pinfo;
847
  char dummy[1]; // Just needed to satisfy pthread_getthrds_np.
848
  int dummy_size = sizeof(dummy);
849

850
  memset(&pinfo, 0, sizeof(pinfo));
851

852
  const int rc = pthread_getthrds_np(&tid, PTHRDSINFO_QUERY_ALL, &pinfo,
853
                                     sizeof(pinfo), dummy, &dummy_size);
854

855
  if (rc != 0) {
856
    fprintf(stderr, "pthread_getthrds_np failed (%d)\n", rc);
857
    fflush(stdout);
858
    return false;
859
  }
860

861
  // The following may happen when invoking pthread_getthrds_np on a pthread
862
  // running on a user provided stack (when handing down a stack to pthread
863
  // create, see pthread_attr_setstackaddr).
864
  // Not sure what to do then.
865
  if (pinfo.__pi_stackend == nullptr || pinfo.__pi_stackaddr == nullptr) {
866
    fprintf(stderr, "pthread_getthrds_np - invalid values\n");
867
    fflush(stdout);
868
    return false;
869
  }
870

871
  // Note: we get three values from pthread_getthrds_np:
872
  //       __pi_stackaddr, __pi_stacksize, __pi_stackend
873
  //
874
  // high addr    ---------------------                                                           base, high
875
  //
876
  //    |         pthread internal data, like ~2K
877
  //    |
878
  //    |         ---------------------   __pi_stackend   (usually not page aligned, (xxxxF890))
879
  //    |
880
  //    |
881
  //    |
882
  //    |
883
  //    |
884
  //    |
885
  //    |          ---------------------   (__pi_stackend - __pi_stacksize)
886
  //    |
887
  //    |          padding to align the following AIX guard pages, if enabled.
888
  //    |
889
  //    V          ---------------------   __pi_stackaddr                                        low, base - size
890
  //
891
  // low addr      AIX guard pages, if enabled (AIXTHREAD_GUARDPAGES > 0)
892
  //
893

894
  out->base = (address)pinfo.__pi_stackend;
895
  address low = (address)pinfo.__pi_stackaddr;
896
  out->size = out->base - low;
897
  return true;
898

899
}
900

901
// variables needed to emulate linux behavior in os::dll_load() if library is loaded twice
902
static pthread_mutex_t g_handletable_mutex = PTHREAD_MUTEX_INITIALIZER;
903

904
struct TableLocker {
905
  TableLocker() { pthread_mutex_lock(&g_handletable_mutex); }
906
  ~TableLocker() { pthread_mutex_unlock(&g_handletable_mutex); }
907
};
908
struct handletableentry{
909
    void*         handle;
910
    ino64_t       inode;
911
    dev64_t       devid;
912
    char*         member;
913
    uint          refcount;
914
};
915
constexpr unsigned init_num_handles = 128;
916
static unsigned max_handletable = 0;
917
static unsigned g_handletable_used = 0;
918
// We start with an empty array. At first use we will dynamically allocate memory for 128 entries.
919
// If this table is full we dynamically reallocate a memory reagion of double size, and so on.
920
static struct handletableentry* p_handletable = nullptr;
921

922
// get the library search path burned in to the executable file during linking
923
// If the libpath cannot be retrieved return an empty path
924
static const char* rtv_linkedin_libpath() {
925
  constexpr int bufsize = 4096;
926
  static char buffer[bufsize];
927
  static const char* libpath = 0;
928

929
  // we only try to retrieve the libpath once. After that try we
930
  // let libpath point to buffer, which then contains a valid libpath
931
  // or an empty string
932
  if (libpath != nullptr) {
933
    return libpath;
934
  }
935

936
  // retrieve the path to the currently running executable binary
937
  // to open it
938
  snprintf(buffer, 100, "/proc/%ld/object/a.out", (long)getpid());
939
  FILE* f = nullptr;
940
  struct xcoffhdr the_xcoff;
941
  struct scnhdr the_scn;
942
  struct ldhdr the_ldr;
943
  constexpr size_t xcoffsz = FILHSZ + _AOUTHSZ_EXEC;
944
  STATIC_ASSERT(sizeof(the_xcoff) == xcoffsz);
945
  STATIC_ASSERT(sizeof(the_scn) == SCNHSZ);
946
  STATIC_ASSERT(sizeof(the_ldr) == LDHDRSZ);
947
  // read the generic XCOFF header and analyze the substructures
948
  // to find the burned in libpath. In any case of error perform the assert
949
  if (nullptr == (f = fopen(buffer, "r")) ||
950
      xcoffsz != fread(&the_xcoff, 1, xcoffsz, f) ||
951
      the_xcoff.filehdr.f_magic != U64_TOCMAGIC ||
952
      0 != fseek(f, (FILHSZ + the_xcoff.filehdr.f_opthdr + (the_xcoff.aouthdr.o_snloader -1)*SCNHSZ), SEEK_SET) ||
953
      SCNHSZ != fread(&the_scn, 1, SCNHSZ, f) ||
954
      0 != strcmp(the_scn.s_name, ".loader") ||
955
      0 != fseek(f, the_scn.s_scnptr, SEEK_SET) ||
956
      LDHDRSZ != fread(&the_ldr, 1, LDHDRSZ, f) ||
957
      0 != fseek(f, the_scn.s_scnptr + the_ldr.l_impoff, SEEK_SET) ||
958
      0 == fread(buffer, 1, bufsize, f)) {
959
    buffer[0] = 0;
960
    assert(false, "could not retrieve burned in library path from executables loader section");
961
  }
962

963
  if (f) {
964
    fclose(f);
965
  }
966
  libpath = buffer;
967

968
  return libpath;
969
}
970

971
// Simulate the library search algorithm of dlopen() (in os::dll_load)
972
static bool search_file_in_LIBPATH(const char* path, struct stat64x* stat) {
973
  if (path == nullptr)
974
    return false;
975

976
  char* path2 = os::strdup(path);
977
  // if exist, strip off trailing (shr_64.o) or similar
978
  char* substr;
979
  if (path2[strlen(path2) - 1] == ')' && (substr = strrchr(path2, '('))) {
980
    *substr = 0;
981
  }
982

983
  bool ret = false;
984
  // If FilePath contains a slash character, FilePath is used directly,
985
  // and no directories are searched.
986
  // But if FilePath does not start with / or . we have to prepend it with ./
987
  if (strchr(path2, '/')) {
988
    stringStream combined;
989
    if (*path2 == '/' || *path2 == '.') {
990
      combined.print("%s", path2);
991
    } else {
992
      combined.print("./%s", path2);
993
    }
994
    ret = (0 == stat64x(combined.base(), stat));
995
    os::free(path2);
996
    return ret;
997
  }
998

999
  const char* env = getenv("LIBPATH");
1000
  if (env == nullptr) {
1001
    // no LIBPATH, try with LD_LIBRARY_PATH
1002
    env = getenv("LD_LIBRARY_PATH");
1003
  }
1004

1005
  stringStream Libpath;
1006
  if (env == nullptr) {
1007
    // no LIBPATH or LD_LIBRARY_PATH given -> try only with burned in libpath
1008
    Libpath.print("%s", rtv_linkedin_libpath());
1009
  } else if (*env == 0) {
1010
    // LIBPATH or LD_LIBRARY_PATH given but empty -> try first with burned
1011
    //  in libpath and with current working directory second
1012
    Libpath.print("%s:.", rtv_linkedin_libpath());
1013
  } else {
1014
    // LIBPATH or LD_LIBRARY_PATH given with content -> try first with
1015
    // LIBPATH or LD_LIBRARY_PATH and second with burned in libpath.
1016
    // No check against current working directory
1017
    Libpath.print("%s:%s", env, rtv_linkedin_libpath());
1018
  }
1019

1020
  char* libpath = os::strdup(Libpath.base());
1021

1022
  char *saveptr, *token;
1023
  for (token = strtok_r(libpath, ":", &saveptr); token != nullptr; token = strtok_r(nullptr, ":", &saveptr)) {
1024
    stringStream combined;
1025
    combined.print("%s/%s", token, path2);
1026
    if ((ret = (0 == stat64x(combined.base(), stat))))
1027
      break;
1028
  }
1029

1030
  os::free(libpath);
1031
  os::free(path2);
1032
  return ret;
1033
}
1034

1035
// specific AIX versions for ::dlopen() and ::dlclose(), which handles the struct g_handletable
1036
// This way we mimic dl handle equality for a library
1037
// opened a second time, as it is implemented on other platforms.
1038
void* Aix_dlopen(const char* filename, int Flags, int *eno, const char** error_report) {
1039
  assert(error_report != nullptr, "error_report is nullptr");
1040
  void* result;
1041
  struct stat64x libstat;
1042

1043
  if (false == search_file_in_LIBPATH(filename, &libstat)) {
1044
    // file with filename does not exist
1045
  #ifdef ASSERT
1046
    result = ::dlopen(filename, Flags);
1047
    assert(result == nullptr, "dll_load: Could not stat() file %s, but dlopen() worked; Have to improve stat()", filename);
1048
  #endif
1049
    *error_report = "Could not load module .\nSystem error: No such file or directory";
1050
    *eno = ENOENT;
1051
    return nullptr;
1052
  }
1053
  else {
1054
    // extract member string if exist duplicate it and store pointer of it
1055
    // if member does not exist store nullptr
1056
    char* member = nullptr;
1057
    const char* substr;
1058
    if (filename[strlen(filename) - 1] == ')' && (substr = strrchr(filename, '('))) {
1059
      member = os::strdup(substr);
1060
    }
1061

1062
    unsigned i = 0;
1063
    TableLocker lock;
1064
    // check if library belonging to filename is already loaded.
1065
    // If yes use stored handle from previous ::dlopen() and increase refcount
1066
    for (i = 0; i < g_handletable_used; i++) {
1067
      if ((p_handletable + i)->handle &&
1068
          (p_handletable + i)->inode == libstat.st_ino &&
1069
          (p_handletable + i)->devid == libstat.st_dev &&
1070
          (((p_handletable + i)->member == nullptr && member == nullptr) ||
1071
           ((p_handletable + i)->member != nullptr && member != nullptr &&
1072
           strcmp((p_handletable + i)->member, member) == 0))) {
1073
        (p_handletable + i)->refcount++;
1074
        result = (p_handletable + i)->handle;
1075
        break;
1076
      }
1077
    }
1078
    if (i == g_handletable_used) {
1079
      // library not yet loaded. Check if there is space left in array
1080
      // to store new ::dlopen() handle
1081
      if (g_handletable_used == max_handletable) {
1082
        // No place in array anymore; increase array.
1083
        unsigned new_max = MAX2(max_handletable * 2, init_num_handles);
1084
        struct handletableentry* new_tab = (struct handletableentry*)::realloc(p_handletable, new_max * sizeof(struct handletableentry));
1085
        assert(new_tab != nullptr, "no more memory for handletable");
1086
        if (new_tab == nullptr) {
1087
          *error_report = "dlopen: no more memory for handletable";
1088
          return nullptr;
1089
        }
1090
        max_handletable = new_max;
1091
        p_handletable = new_tab;
1092
      }
1093
      // Library not yet loaded; load it, then store its handle in handle table
1094
      errno = 0;
1095
      result = ::dlopen(filename, Flags);
1096
      if (result != nullptr) {
1097
        g_handletable_used++;
1098
        (p_handletable + i)->handle = result;
1099
        (p_handletable + i)->inode = libstat.st_ino;
1100
        (p_handletable + i)->devid = libstat.st_dev;
1101
        (p_handletable + i)->member = member;
1102
        (p_handletable + i)->refcount = 1;
1103
      }
1104
      else {
1105
        // error analysis when dlopen fails
1106
        *eno = errno;
1107
        *error_report = ::dlerror();
1108
        if (*error_report == nullptr) {
1109
          *error_report = "dlerror returned no error description";
1110
        }
1111
      }
1112
    }
1113
  }
1114
  return result;
1115
}
1116

1117
bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {
1118
  unsigned i = 0;
1119
  bool res = false;
1120

1121
  if (ebuf && ebuflen > 0) {
1122
    ebuf[0] = '\0';
1123
    ebuf[ebuflen - 1] = '\0';
1124
  }
1125

1126
  {
1127
    TableLocker lock;
1128
    // try to find handle in array, which means library was loaded by os::dll_load() call
1129
    for (i = 0; i < g_handletable_used; i++) {
1130
      if ((p_handletable + i)->handle == libhandle) {
1131
        // handle found, decrease refcount
1132
        assert((p_handletable + i)->refcount > 0, "Sanity");
1133
        (p_handletable + i)->refcount--;
1134
        if ((p_handletable + i)->refcount > 0) {
1135
          // if refcount is still >0 then we have to keep library and just return true
1136
          return true;
1137
        }
1138
        // refcount == 0, so we have to ::dlclose() the lib
1139
        // and delete the entry from the array.
1140
        break;
1141
      }
1142
    }
1143

1144
    // If we reach this point either the libhandle was found with refcount == 0, or the libhandle
1145
    // was not found in the array at all. In both cases we have to ::dlclose the lib and perform
1146
    // the error handling. In the first case we then also have to delete the entry from the array
1147
    // while in the second case we simply have to nag.
1148
    res = (0 == ::dlclose(libhandle));
1149
    if (!res) {
1150
      // error analysis when dlclose fails
1151
      const char* error_report = ::dlerror();
1152
      if (error_report == nullptr) {
1153
        error_report = "dlerror returned no error description";
1154
      }
1155
      if (ebuf != nullptr && ebuflen > 0) {
1156
        snprintf(ebuf, ebuflen - 1, "%s", error_report);
1157
      }
1158
      assert(false, "os::pd_dll_unload() ::dlclose() failed");
1159
    }
1160

1161
    if (i < g_handletable_used) {
1162
      if (res) {
1163
        // First case: libhandle was found (with refcount == 0) and ::dlclose successful,
1164
        // so delete entry from array (do not forget to free member-string space if member exists)
1165
        if ((p_handletable + i)->member) {
1166
          os::free((p_handletable + i)->member);
1167
          (p_handletable + i)->member = nullptr;
1168
        }
1169
        g_handletable_used--;
1170
        // If the entry was the last one of the array, the previous g_handletable_used--
1171
        // is sufficient to remove the entry from the array, otherwise we move the last
1172
        // entry of the array to the place of the entry we want to remove and overwrite it
1173
        if (i < g_handletable_used) {
1174
          *(p_handletable + i) = *(p_handletable + g_handletable_used);
1175
          (p_handletable + g_handletable_used)->handle = nullptr;
1176
        }
1177
      }
1178
    }
1179
    else {
1180
      // Second case: libhandle was not found (library was not loaded by os::dll_load())
1181
      // therefore nag
1182
      assert(false, "os::pd_dll_unload() library was not loaded by os::dll_load()");
1183
    }
1184
  }
1185

1186
  // Update the dll cache
1187
  LoadedLibraries::reload();
1188

1189
  return res;
1190
} // end: os::pd_dll_unload()
1191

1192

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.