|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import subprocess |
| 4 | + |
| 5 | + |
| 6 | +def diagnose_binary(binary_path): |
| 7 | + """Diagnose why a binary file can't be executed.""" |
| 8 | + results = {} |
| 9 | + |
| 10 | + # Check if file exists |
| 11 | + results["exists"] = os.path.exists(binary_path) |
| 12 | + if not results["exists"]: |
| 13 | + return f"Error: {binary_path} does not exist" |
| 14 | + |
| 15 | + # Get system architecture |
| 16 | + results["system_arch"] = platform.machine() |
| 17 | + results["system_os"] = platform.system() |
| 18 | + |
| 19 | + # Check file type using 'file' command |
| 20 | + try: |
| 21 | + file_output = subprocess.check_output( |
| 22 | + ["file", binary_path], universal_newlines=True |
| 23 | + ) |
| 24 | + results["file_type"] = file_output.strip() |
| 25 | + except subprocess.CalledProcessError: |
| 26 | + results["file_type"] = "Could not determine file type (file command failed)" |
| 27 | + except FileNotFoundError: |
| 28 | + results["file_type"] = ( |
| 29 | + "Could not determine file type (file command not available)" |
| 30 | + ) |
| 31 | + |
| 32 | + # For Linux, check ELF header |
| 33 | + if platform.system() == "Linux": |
| 34 | + try: |
| 35 | + readelf_output = subprocess.check_output( |
| 36 | + ["readelf", "-h", binary_path], universal_newlines=True |
| 37 | + ) |
| 38 | + # Extract architecture from readelf output |
| 39 | + arch_line = [ |
| 40 | + line for line in readelf_output.splitlines() if "Machine:" in line |
| 41 | + ] |
| 42 | + if arch_line: |
| 43 | + results["binary_arch"] = arch_line[0].strip() |
| 44 | + else: |
| 45 | + results["binary_arch"] = "Could not determine binary architecture" |
| 46 | + except subprocess.CalledProcessError: |
| 47 | + results["binary_arch"] = "Not a valid ELF binary (readelf command failed)" |
| 48 | + except FileNotFoundError: |
| 49 | + results["binary_arch"] = ( |
| 50 | + "Could not check ELF header (readelf command not available)" |
| 51 | + ) |
| 52 | + |
| 53 | + # Print summary |
| 54 | + print(f"Diagnosis for {binary_path}:") |
| 55 | + print(f"File exists: {results['exists']}") |
| 56 | + print(f"System architecture: {results['system_arch']}") |
| 57 | + print(f"System OS: {results['system_os']}") |
| 58 | + print(f"File type: {results['file_type']}") |
| 59 | + if "binary_arch" in results: |
| 60 | + print(f"Binary architecture: {results['binary_arch']}") |
| 61 | + |
| 62 | + # Provide potential solution |
| 63 | + if "ELF" in results.get("file_type", "") and results["system_os"] == "Linux": |
| 64 | + if ( |
| 65 | + "64-bit" in results["file_type"] |
| 66 | + and "x86-64" in results["file_type"] |
| 67 | + and results["system_arch"] != "x86_64" |
| 68 | + ): |
| 69 | + return ( |
| 70 | + "Error: Binary was compiled for x86_64 architecture but your system is " |
| 71 | + + results["system_arch"] |
| 72 | + ) |
| 73 | + elif ( |
| 74 | + "ARM" in results.get("binary_arch", "") |
| 75 | + and "arm" not in results["system_arch"].lower() |
| 76 | + ): |
| 77 | + return ( |
| 78 | + "Error: Binary was compiled for ARM architecture but your system is " |
| 79 | + + results["system_arch"] |
| 80 | + ) |
| 81 | + |
| 82 | + return "Binary format may be incompatible with your system. You need a version compiled specifically for your architecture and operating system." |
0 commit comments