r"""Drops a core file for a running local process. Note: You should use :meth:`.process.corefile` instead of using this method directly. Arguments: process: Process to dump Returns: :class:`.Core`: The generated core file Example: >>> io = process
(process)
| 1489 | return rv |
| 1490 | |
| 1491 | def corefile(process): |
| 1492 | r"""Drops a core file for a running local process. |
| 1493 | |
| 1494 | Note: |
| 1495 | You should use :meth:`.process.corefile` instead of using this method directly. |
| 1496 | |
| 1497 | Arguments: |
| 1498 | process: Process to dump |
| 1499 | |
| 1500 | Returns: |
| 1501 | :class:`.Core`: The generated core file |
| 1502 | |
| 1503 | Example: |
| 1504 | |
| 1505 | >>> io = process('bash') |
| 1506 | >>> core = gdb.corefile(io) |
| 1507 | >>> core.exe.name # doctest: +ELLIPSIS |
| 1508 | '.../bin/bash' |
| 1509 | """ |
| 1510 | |
| 1511 | if context.noptrace: |
| 1512 | log.warn_once("Skipping corefile since context.noptrace==True") |
| 1513 | return |
| 1514 | |
| 1515 | corefile_path = './core.%s.%i' % (os.path.basename(process.executable), |
| 1516 | process.pid) |
| 1517 | |
| 1518 | # Due to https://sourceware.org/bugzilla/show_bug.cgi?id=16092 |
| 1519 | # will disregard coredump_filter, and will not dump private mappings. |
| 1520 | if version() < (7,11): |
| 1521 | log.warn_once('The installed GDB (%s) does not emit core-dumps which ' |
| 1522 | 'contain all of the data in the process.\n' |
| 1523 | 'Upgrade to GDB >= 7.11 for better core-dumps.' % binary()) |
| 1524 | |
| 1525 | # This is effectively the same as what the 'gcore' binary does |
| 1526 | gdb_args = ['-batch', |
| 1527 | '-q', |
| 1528 | '-nx', |
| 1529 | '-ex', 'set pagination off', |
| 1530 | '-ex', 'set height 0', |
| 1531 | '-ex', 'set width 0', |
| 1532 | '-ex', 'set use-coredump-filter on', |
| 1533 | '-ex', 'generate-core-file %s' % corefile_path, |
| 1534 | '-ex', 'detach'] |
| 1535 | |
| 1536 | with context.local(terminal = ['sh', '-c']): |
| 1537 | with context.quiet: |
| 1538 | pid = attach(process, gdb_args=gdb_args) |
| 1539 | log.debug("Got GDB pid %d", pid) |
| 1540 | try: |
| 1541 | psutil.Process(pid).wait() |
| 1542 | except psutil.Error: |
| 1543 | pass |
| 1544 | |
| 1545 | if not os.path.exists(corefile_path): |
| 1546 | log.error("Could not generate a corefile for process %d", process.pid) |
| 1547 | |
| 1548 | return elf.corefile.Core(corefile_path) |