1# Copyright Louis Dionne 2016 2# Copyright Zach Laine 2016 3# Distributed under the Boost Software License, Version 1.0. 4# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) 5# 6# 7# This CMake module provides a way to get the disassembly of a function within 8# an executable created with `add_executable`. The module provides a `disassemble` 9# function that creates a target which, when built, outputs the disassembly of 10# the given function within an executable to standard output. 11# 12# Parameters 13# ---------- 14# target: 15# The name of the target to create. Building this target will generate the 16# requested disassembly. 17# 18# EXECUTABLE executable: 19# The name of an executable to disassemble. This must be the name of a valid 20# executable that was created with `add_executable`. The disassembly target 21# thus created will be made dependent on the executable, so that it is built 22# automatically when the disassembly is requested. 23# 24# FUNCTION function-name: 25# The name of the function to disassemble in the executable. 26# 27# [ALL]: 28# If provided, the generated target is included in the 'all' target. 29# 30function(disassemble target) 31 cmake_parse_arguments(ARGS "ALL" # options 32 "EXECUTABLE;FUNCTION" # 1 value args 33 "" # multivalued args 34 ${ARGN}) 35 36 if (NOT ARGS_EXECUTABLE) 37 message(FATAL_ERROR "The `EXECUTABLE` argument must be provided.") 38 endif() 39 if (NOT TARGET ${ARGS_EXECUTABLE}) 40 message(FATAL_ERROR "The `EXECUTABLE` argument must be the name of a valid " 41 "executable created with `add_executable`.") 42 endif() 43 44 if (NOT ARGS_FUNCTION) 45 message(FATAL_ERROR "The `FUNCTION` argument must be provided.") 46 endif() 47 48 if (ARGS_ALL) 49 set(ARGS_ALL "ALL") 50 else() 51 set(ARGS_ALL "") 52 endif() 53 54 if (DISASSEMBLE_lldb) 55 add_custom_target(${target} ${ARGS_ALL} 56 COMMAND ${DISASSEMBLE_lldb} -f $<TARGET_FILE:${ARGS_EXECUTABLE}> 57 -o "disassemble --name ${ARGS_FUNCTION}" 58 -o quit 59 DEPENDS ${ARGS_EXECUTABLE} 60 ) 61 elseif(DISASSEMBLE_gdb) 62 add_custom_target(${target} ${ARGS_ALL} 63 COMMAND ${DISASSEMBLE_gdb} -batch -se $<TARGET_FILE:${ARGS_EXECUTABLE}> 64 -ex "disassemble ${ARGS_FUNCTION}" 65 DEPENDS ${ARGS_EXECUTABLE} 66 ) 67 endif() 68endfunction() 69 70find_program(DISASSEMBLE_gdb gdb) 71find_program(DISASSEMBLE_lldb lldb) 72