|
16 | 16 |
|
17 | 17 | import bdx |
18 | 18 | from bdx import debug, error, info, log, make_progress_bar, trace |
19 | | -from bdx.binary import BinaryDirectory, Exclusion, find_compilation_database |
| 19 | +from bdx.binary import ( |
| 20 | + BinaryDirectory, |
| 21 | + Exclusion, |
| 22 | + find_compilation_database, |
| 23 | + find_symbol_definition, |
| 24 | +) |
20 | 25 | from bdx.index import ( |
21 | 26 | IndexingOptions, |
22 | 27 | PathField, |
@@ -652,6 +657,54 @@ def disass( |
652 | 657 | subprocess.check_call(cmd, shell=True) |
653 | 658 |
|
654 | 659 |
|
| 660 | +@cli.command() |
| 661 | +@_common_options(index_must_exist=True) |
| 662 | +@click.argument( |
| 663 | + "query", |
| 664 | + nargs=-1, |
| 665 | + shell_complete=_complete_query, |
| 666 | +) |
| 667 | +@click.option( |
| 668 | + "-n", |
| 669 | + "--num", |
| 670 | + help="Limit the number of results", |
| 671 | + type=click.IntRange(1), |
| 672 | + metavar="LIMIT", |
| 673 | + default=None, |
| 674 | +) |
| 675 | +def find_definition(_directory, index_path, query, num): |
| 676 | + """Find definition of symbols matching some query.""" |
| 677 | + fmt = "{file}:{line}: {name}" |
| 678 | + |
| 679 | + results = search_index( |
| 680 | + index_path=index_path, query=" ".join(query), limit=num |
| 681 | + ) |
| 682 | + |
| 683 | + while True: |
| 684 | + try: |
| 685 | + res = next(results) |
| 686 | + except QueryParser.Error as e: |
| 687 | + error(f"Invalid query: {str(e)}") |
| 688 | + exit(1) |
| 689 | + except StopIteration: |
| 690 | + break |
| 691 | + |
| 692 | + if res.symbol_outdated: |
| 693 | + error("Information outdated, re-index needed") |
| 694 | + |
| 695 | + defn = find_symbol_definition(res.symbol) |
| 696 | + debug("Found definition: {}", defn) |
| 697 | + |
| 698 | + if defn: |
| 699 | + click.echo( |
| 700 | + fmt.format( |
| 701 | + file=defn.source, |
| 702 | + line=defn.line, |
| 703 | + name=res.symbol.name, |
| 704 | + ) |
| 705 | + ) |
| 706 | + |
| 707 | + |
655 | 708 | @cli.command() |
656 | 709 | @_common_options(index_must_exist=True) |
657 | 710 | def files(_directory, index_path): |
|
0 commit comments