Skip to content

Commit

Permalink
Tool for using Intel(R) Processor Trace hardware feature
Browse files Browse the repository at this point in the history
Summary:
1. Provide single library for all Intel specific hardware features instead
    of individual libraries for each feature
2. Added Intel(R) Processor Trace hardware feature in this single library.
    Details about the tool implementing this feature is as follows:

     Tool developed on top of LLDB to provide its users the execution
     trace of the debugged inferiors. Tool's API are exposed as C++ object
     oriented interface in a shared library. API are designed especially to be
     easily integrable with IDEs providing LLDB as an application debugger.
     Entire API is also available as Python functions through a script bridging
     interface allowing development of python modules.

     This patch also provides a CLI wrapper to use the Tool through LLDB's command
     line. Highlights of the Tool and the wrapper are given below:

  ******************************
  Intel(R) Processor Trace Tool:
  ******************************
       - Provides execution trace of the debugged application
       - Uses Intel(R) Processor Trace hardware feature (already implemented inside LLDB)
         for this purpose
           -- Collects trace packets generated by this feature from LLDB, decodes and
              post-processes them
           -- Constructs the execution trace of the application
           -- Presents execution trace as a list of assembly instructions
       - Provides 4 APIs (exposed as C++ object oriented interface)
           -- start trace with configuration options for a thread/process,
           -- stop trace for a thread/process,
           -- get the execution flow (assembly instructions) for a thread,
           -- get trace specific information for a thread
       - Easily integrable into IDEs providing LLDB as application debugger
       - Entire API available as Python functions through script bridging interface
           -- Allows developing python apps on top of Tool
       - README_TOOL.txt provides more details about the Tool, its dependencies, building
         steps and API usage
       - Tool ready to use through LLDB's command line
           -- CLI wrapper has been developed on top of the Tool for this purpose

  *********************************
  CLI wrapper: cli-wrapper-pt.cpp
  *********************************
       - Provides 4 commands (syntax similar to LLDB's CLI commands):
           -- processor-trace start
           -- processor-trace stop
           -- processor-trace show-trace-options
           -- processor-trace show-instr-log
       - README_CLI.txt provides more details about commands and their options

Signed-off-by: Abhishek Aggarwal <[email protected]>

Reviewers: clayborg, jingham, lldb-commits, labath

Reviewed By: clayborg

Subscribers: ravitheja, emaste, krytarowski, mgorny

Differential Revision: https://reviews.llvm.org/D33035

llvm-svn: 310261
  • Loading branch information
Abhishek Aggarwal committed Aug 7, 2017
1 parent 9581b42 commit 307db0f
Show file tree
Hide file tree
Showing 23 changed files with 2,492 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lldb/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ add_subdirectory(lldb-mi)
if (LLDB_CAN_USE_LLDB_SERVER)
add_subdirectory(lldb-server)
endif()
add_subdirectory(intel-mpx)
add_subdirectory(intel-features)
66 changes: 66 additions & 0 deletions lldb/tools/intel-features/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
include(${LLDB_PROJECT_ROOT}/cmake/LLDBDependencies.cmake)

# Flags to control each individual feature
option(LLDB_BUILD_INTEL_MPX "Enable Building of Intel(R) Memory Protection Extensions" ON)
option(LLDB_BUILD_INTEL_PT "Enable Building of Intel(R) Processor Trace Tool" OFF)

# Return if all features are OFF
if (NOT LLDB_BUILD_INTEL_MPX AND NOT LLDB_BUILD_INTEL_PT)
return()
endif()

LIST (APPEND FEATURE_LIBS "")

# Add feature specific subdirectories based on flags
if (LLDB_BUILD_INTEL_MPX AND CMAKE_SYSTEM_NAME MATCHES "Linux")
add_subdirectory(intel-mpx)
LIST (APPEND FEATURE_LIBS ${FEATURE_LIBS} lldbIntelMPX)
SET (CLI_WRAPPER_PREPROCESSORS "${CLI_WRAPPER_PREPROCESSORS} -DBUILD_INTEL_MPX")
endif()

if (LLDB_BUILD_INTEL_PT)
add_subdirectory(intel-pt)
LIST (APPEND FEATURE_LIBS ${FEATURE_LIBS} lldbIntelPT)
SET (CLI_WRAPPER_PREPROCESSORS "${CLI_WRAPPER_PREPROCESSORS} -DBUILD_INTEL_PT")
endif()

# Add python wrapper if python not disabled
if (NOT LLDB_DISABLE_PYTHON AND LLDB_BUILD_INTEL_PT)
set(LLDB_INTEL_FEATURES_PYTHON_WRAP
${LLDB_BINARY_DIR}/tools/intel-features/scripts/IntelFeaturesPythonWrap.cpp)
set_source_files_properties(${LLDB_INTEL_FEATURES_PYTHON_WRAP}
PROPERTIES GENERATED 1)

if (CLANG_CL)
set_source_files_properties(${LLDB_INTEL_FEATURES_PYTHON_WRAP}
PROPERTIES COMPILE_FLAGS -Wno-unused-function)
endif()

if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND
NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")
set_property(SOURCE ${LLDB_INTEL_FEATURES_PYTHON_WRAP}
APPEND_STRING PROPERTY COMPILE_FLAGS
" -Wno-sequence-point -Wno-cast-qual")
endif ()
add_subdirectory(scripts)
endif()

set_source_files_properties(cli-wrapper.cpp PROPERTIES
COMPILE_FLAGS ${CLI_WRAPPER_PREPROCESSORS})

add_lldb_library(lldbIntelFeatures SHARED
cli-wrapper.cpp
${LLDB_INTEL_FEATURES_PYTHON_WRAP}

LINK_LIBS
${FEATURE_LIBS}
)

# Add link dependencies for python wrapper
if (NOT LLDB_DISABLE_PYTHON AND LLDB_BUILD_INTEL_PT)
add_dependencies(lldbIntelFeatures intel-features-swig_wrapper)
target_link_libraries(lldbIntelFeatures PRIVATE ${LLDB_SYSTEM_LIBS})
endif()

install(TARGETS lldbIntelFeatures
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX})
73 changes: 73 additions & 0 deletions lldb/tools/intel-features/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
****************************************************************************
* README *
* *
* This file provides all the information regarding new CLI commands that *
* enable using various hardware features of Intel(R) architecture based *
* processors from LLDB's CLI. *
****************************************************************************


============
Introduction
============
A shared library has been developed to use various hardware features of
Intel(R) architecture based processors through LLDB's command line. The library
currently comprises of hardware features namely Intel(R) Processor Trace and
Intel(R) Memory Protection Extensions.


============
Details
============
A C++ based cli wrapper (cli-wrapper.cpp) has been developed here that
agglomerates all cli commands for various hardware features. This wrapper is
build to generate a shared library (lldbIntelFeatures) to provide all these
commands.

For each hardware feature, separate cli commands have been developed that are
provided by wrappers (cli-wrapper-pt.cpp and cli-wrapper-mpxtable.cpp) residing
in feature specific folders ("intel-pt" and "intel-mpx" respectively).

For details regarding cli commands of each feature, please refer to these
feature specific wrappers.



============
How to Build
============
The shared library (lldbIntelFeatures) has a cmake based build and can be built
while building LLDB with cmake. "cli-wrapper.cpp" file is compiled along with all
the feature specific source files (residing in feature specific folders).

Furthermore, flexibility is provided to the user to include/exclude a particular
feature while building lldbIntelFeatures library. This is done by flags described
below:

- LLDB_BUILD_INTEL_PT - The flag enables building of Intel(R) Processor Trace
feature (inside intel-pt folder). This flag defaults to "OFF" meaning the
feature is excluded while building lldbIntelFeatures library. Set it to "ON"
in order to include it.

- LLDB_BUILD_INTEL_MPX - Enables building Intel(R) Memory Protection Extensions
feature (inside intel-mpx folder). This flag defaults to "ON" meaning
the feature is excluded while building lldbIntelFeatures library.

Please refer to README files in feature specific folders to know about additional
flags that need to be set in order to build that feature successfully.


============
How to Use
============
All CLI commands provided by this shared library can be used through the LLDB's
CLI by executing "plugin load <shared_lib_name>" on LLDB CLI. shared_lib_name here
is lldbIntelFeatures



============
Description
============
Please refer to README_CLI file of each feature to know about details of CLI
commands.
37 changes: 37 additions & 0 deletions lldb/tools/intel-features/cli-wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===-- cli-wrapper.cpp -----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
// CLI Wrapper for hardware features of Intel(R) architecture based processors
// to enable them to be used through LLDB's CLI. For details, please refer to
// cli wrappers of each individual feature, residing in their respective
// folders.
//
// Compile this into a shared lib and load by placing at appropriate locations
// on disk or by using "plugin load" command at the LLDB command line.
//
//===----------------------------------------------------------------------===//

#include "intel-mpx/cli-wrapper-mpxtable.h"
#include "intel-pt/cli-wrapper-pt.h"
#include "lldb/API/SBDebugger.h"

namespace lldb {
bool PluginInitialize(lldb::SBDebugger debugger);
}

bool lldb::PluginInitialize(lldb::SBDebugger debugger) {

#ifdef BUILD_INTEL_PT
PTPluginInitialize(debugger);
#endif

#ifdef BUILD_INTEL_MPX
MPXPluginInitialize(debugger);
#endif

return true;
}
9 changes: 9 additions & 0 deletions lldb/tools/intel-features/intel-mpx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_lldb_library(lldbIntelMPX
cli-wrapper-mpxtable.cpp

LINK_LIBS
liblldb

LINK_COMPONENTS
Support
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//===-- IntelMPXTablePlugin.cpp----------------------------------*- C++ -*-===//
//===-- cli-wrapper-mpxtable.cpp----------------------------------*- C++
//-*-===//
//
// The LLVM Compiler Infrastructure
//
Expand All @@ -12,6 +13,7 @@
#include <string>

// Project includes
#include "cli-wrapper-mpxtable.h"
#include "lldb/API/SBCommandInterpreter.h"
#include "lldb/API/SBCommandReturnObject.h"
#include "lldb/API/SBMemoryRegionInfo.h"
Expand All @@ -21,10 +23,6 @@

#include "llvm/ADT/Triple.h"

namespace lldb {
bool PluginInitialize(lldb::SBDebugger debugger);
}

static bool GetPtr(char *cptr, uint64_t &ptr, lldb::SBFrame &frame,
lldb::SBCommandReturnObject &result) {
if (!cptr) {
Expand Down Expand Up @@ -285,8 +283,8 @@ static bool GetInitInfo(lldb::SBDebugger debugger, lldb::SBTarget &target,

lldb::SBValue bndcfgu_val = frame.FindRegister("bndcfgu");
if (!bndcfgu_val.IsValid()) {
result.SetError(
"Cannot access register BNDCFGU. Does the target support MPX?");
result.SetError("Cannot access register BNDCFGU. Does the target support "
"Intel(R) Memory Protection Extensions (Intel(R) MPX)?");
result.SetStatus(lldb::eReturnStatusFailed);
return false;
}
Expand Down Expand Up @@ -409,17 +407,17 @@ class MPXTableSet : public lldb::SBCommandPluginInterface {
}
};

bool lldb::PluginInitialize(lldb::SBDebugger debugger) {
bool MPXPluginInitialize(lldb::SBDebugger &debugger) {
lldb::SBCommandInterpreter interpreter = debugger.GetCommandInterpreter();
lldb::SBCommand mpxTable = interpreter.AddMultiwordCommand(
"mpx-table", "A utility to access the MPX table entries.");
"mpx-table", "A utility to access the Intel(R) MPX table entries.");

const char *mpx_show_help = "Show the MPX table entry of a pointer.\n"
"mpx-table show <pointer>";
const char *mpx_show_help = "Show the Intel(R) MPX table entry of a pointer."
"\nmpx-table show <pointer>";
mpxTable.AddCommand("show", new MPXTableShow(), mpx_show_help);

const char *mpx_set_help =
"Set the MPX table entry of a pointer.\n"
"Set the Intel(R) MPX table entry of a pointer.\n"
"mpx-table set <pointer> <lower bound> <upper bound>";
mpxTable.AddCommand("set", new MPXTableSet(), mpx_set_help);

Expand Down
12 changes: 12 additions & 0 deletions lldb/tools/intel-features/intel-mpx/cli-wrapper-mpxtable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===-- cli-wrapper-mpxtable.h----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/API/SBDebugger.h"

bool MPXPluginInitialize(lldb::SBDebugger &debugger);
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ In order to run this test, create the following directory:

packages/Python/lldbsuite/test/functionalities/plugins/commands/mpxtablecmd

and copy into it the contents of this direcotry.
and copy into it the contents of this directory.

Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ def setUp(self):
@skipIf(compiler="clang")
@skipIf(oslist=no_match(['linux']))
@skipIf(archs=no_match(['i386', 'x86_64']))
@skipIf(compiler="gcc", compiler_version=["<", "5"]) #GCC version >= 5 supports Intel(R) MPX.
@skipIf(compiler="gcc", compiler_version=["<", "5"]) #GCC version >= 5 supports
#Intel(R) Memory Protection Extensions (Intel(R) MPX).
def test_show_command(self):
"""Test 'mpx-table show' command"""
self.build()

lldb_exec_dir = os.environ["LLDB_IMPLIB_DIR"]
lldb_lib_dir = os.path.join(lldb_exec_dir, os.pardir, "lib")
plugin_file = os.path.join(lldb_lib_dir, "liblldb-intel-mpxtable.so")
plugin_file = os.path.join(lldb_lib_dir, "liblldbIntelFeatures.so")
if not os.path.isfile(plugin_file):
self.skipTest("Intel(R) mpx-table plugin missing.")
self.skipTest("features plugin missing.")
plugin_command = " "
seq = ("plugin", "load", plugin_file)
plugin_command = plugin_command.join(seq)
Expand Down Expand Up @@ -123,9 +124,9 @@ def test_set_command(self):

lldb_exec_dir = os.environ["LLDB_IMPLIB_DIR"]
lldb_lib_dir = os.path.join(lldb_exec_dir, os.pardir, "lib")
plugin_file = os.path.join(lldb_lib_dir, "liblldb-intel-mpxtable.so")
plugin_file = os.path.join(lldb_lib_dir, "liblldbIntelFeatures.so")
if not os.path.isfile(plugin_file):
self.skipTest("Intel(R) mpx-table plugin missing.")
self.skipTest("features plugin missing.")
plugin_command = " "
seq = ("plugin", "load", plugin_file)
plugin_command = plugin_command.join(seq)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ void func(int *ptr) {
int
main(int argc, char const *argv[])
{
// This call returns 0 only if the CPU and the kernel support Intel(R) MPX.
// This call returns 0 only if the CPU and the kernel support
// Intel(R) Memory Protection Extensions (Intel(R) MPX).
if (prctl(PR_MPX_ENABLE_MANAGEMENT, 0, 0, 0, 0) != 0)
return -1;

Expand Down
31 changes: 31 additions & 0 deletions lldb/tools/intel-features/intel-pt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if (NOT LIBIPT_INCLUDE_PATH)
message (FATAL_ERROR "libipt include path not provided")
endif()

if (NOT EXISTS "${LIBIPT_INCLUDE_PATH}")
message (FATAL_ERROR "invalid libipt include path provided")
endif()
include_directories(${LIBIPT_INCLUDE_PATH})

if (NOT LIBIPT_LIBRARY_PATH)
find_library(LIBIPT_LIBRARY ipt)
else()
if (NOT EXISTS "${LIBIPT_LIBRARY_PATH}")
message (FATAL_ERROR "invalid libipt library path provided")
endif()
find_library(LIBIPT_LIBRARY ipt PATHS ${LIBIPT_LIBRARY_PATH})
endif()

if (NOT LIBIPT_LIBRARY)
message (FATAL_ERROR "libipt library not found")
endif()

add_lldb_library(lldbIntelPT
PTDecoder.cpp
Decoder.cpp
cli-wrapper-pt.cpp

LINK_LIBS
${LIBIPT_LIBRARY}
liblldb
)
Loading

0 comments on commit 307db0f

Please sign in to comment.