c++交互式执行: cling | inspector-repl; gdb搜索内存指定int32

c++交互式执行: inspector-repl

readme.md

build cling

参考 build cling


llvmRepoUrl=http://root.cern.ch/git/llvm.git
clingRepoUrl=http://root.cern.ch/git/cling.git
clangRepoUrl=http://root.cern.ch/git/clang.git
branch=cling-patches

rD=/app3/inspector-repl-home
llvmD=$rD/llvm.git
llvmToolsD=$llvmD/tools
llvmBldD=$llvmD/build
llvmInstD=$llvmD/inst

mkdir -p $llvmBldD $llvmInstD

git clone -b $branch $llvmRepoUrl $llvmD
#/app3/inspector-repl-home/llvm.git/.git/config

( cd $llvmD ; git checkout $branch ;)
( cd $llvmToolsD ; git clone $clingRepoUrl ;)
#/app3/inspector-repl-home/llvm.git/tools/cling/.git/config
( cd $llvmToolsD ; git clone -b $branch  $clangRepoUrl ;)
#/app3/inspector-repl-home/llvm.git/tools/clang/.git/config

cmake -DCMAKE_INSTALL_PREFIX=$llvmInstD -B $llvmBldD -S $llvmD
cmake --build $llvmBldD
##
#cmake --build $llvmBldD --target install

build inspector

build-inspector


inspectorRepoUrl=https://github.com/inspector-repl/inspector.git

rD=/app3/inspector-repl-home
inspectorD=$rD/inspector.git
inspectorBldD=$inspectorD/build


git clone $inspectorRepoUrl $inspectorD
mkdir $inspectorBldD
cmake -B $inspectorBldD -S $inspectorD -DCMAKE_PREFIX_PATH="../inst" 
cmake --build $inspectorBldD

usage

usage


# bring python bindings
# (requires clang 5/trunk for python3 support, the clang branch from cling is new enough)
# and libclang into path
export PYTHONPATH=$(readlink -f $llvmD/src/tools/clang/bindings/python/)
export LD_LIBRARY_PATH=$(readlink -f $llvmD/inst/lib)
inspectorF=$inspectorBldD/inspector
testSrcF=$inspectorD/test/test.cpp
$inspectorF prebuild $testSrcF
gcc -o test-proc $($inspectorF print-cflags) $testSrcF
# start repl cli
$inspectorF repl
# let program connect to repl
$inspectorBldD/test-proc

gdb搜索内存指定int32

cling交互式

cling (见三、cling编译、使用)

#include <stdint.h>
uint32_t *flag_uint32__ptr=new uint32_t(0xABCCBA02);

#include <stdio.h>
printf("%p\n",flag_uint32__ptr);

pmap -x -p $(pidof cling)

gdb 基本

sudo gdb --pid $(pidof cling)

set pagination off
set logging file /tmp/gdb.log
set logging on
info proc mappings

gdb查看cling进程heap地址范围

echo "info proc mappings" |  sudo gdb --pid `pidof cling`  | grep heap
#    Start Addr       End Addr       Size        Offset Perms  objfile
#0x5fa3a43b0000     0x5fa3a4631000   0x281000      0x0  rw-p   [heap]

gdb搜索cling进程的heap范围内给定int32

read heap_begin_adr heap_end_adr <<< $( echo "info proc mappings" |  sudo gdb --pid `pidof cling`  2>/dev/null | grep heap | awk '{print $1,$2}' )

flag_uint32=0xABCCBA02
echo "find /w $heap_begin_adr,$heap_end_adr, $flag_uint32"  |  sudo gdb --pid `pidof cling` 

demo.cc

//demo.cc
#include <unistd.h> // :sleep
#include <stdint.h> // :uint32_t
#include <stdio.h> // :printf


int main(char** argv, int argc){
uint32_t *flag_uint32__ptr=new uint32_t(0xABCCBA02);

//循环: 休眠、打印
for(int i=0; i<60*60; i+=3){
printf("flag_uint32__ptr:adr=%p,content=0x%X\n",flag_uint32__ptr,*flag_uint32__ptr);
//flag_uint32__ptr:adr=0x63294bc3ceb0,content=0xABCCBA02
sleep(3); // 休眠3秒
}

//结束
return 0;
}

g++ -g -o demo.elf demo.cc && ./demo.elf

gdb搜索demo.elf进程的heap范围内给定int32

#获得进程demo.elf的heap区的地址范围
read heap_begin_adr heap_end_adr <<< $( echo "info proc mappings" |  sudo gdb --pid `pidof demo.elf`  2>/dev/null | grep heap | awk '{print $1,$2}' )

flag_uint32=0xABCCBA02

#gdb搜索demo.elf进程给定地址范围(heap区)内的 flag_uint32
echo "find /w $heap_begin_adr,$heap_end_adr, $flag_uint32"  |  sudo gdb --pid `pidof demo.elf` 
#(gdb) 0x63294bc3ceb0


#gdb打印搜索到的地址的内容
echo '''set $flag_uint32__adr=(unsigned int*)0x63294bc3ceb0
printf "*$flag_uint32__adr=0x%X\n",*$flag_uint32__adr
#若  并稍微修改 ,上面的 demo.cc 中循环打印将有对应变化
#(*$flag_uint32__adr)=(*$flag_uint32__adr)+1
'''  |  sudo gdb --pid `pidof demo.elf` 
#(gdb) *$flag_uint32__adr=0xABCCBA02

linux man搜索c结构体定义、c函数定义

man搜索 结构体定义struct sockaddr_un

man -K "struct sockaddr_un {" | grep -A 3 SYNOPSIS

SYNOPSIS
       #include <sys/socket.h>
       #include <sys/un.h>

man搜索 函数定义getcwd

man getcwd | grep -A 3 SYNOPSIS

SYNOPSIS
       #include <unistd.h>
       char *getcwd(char *buf, size_t size);

三、cling编译、使用

2. cling使用

经过 1. cling编译 后 ,即可使用如下(c++像脚本一样被使用):

上手 : On-the-fly-C++/cling-cpp-11-interpreter/

中文utf8编码范围

根据: chinsese-character-table.git/unicode中文字符表.txt

unicode编码中文第一个汉字: unicode编码=\u4e00 ---> 汉字=一

unicode编码中文最后一个汉字:unicode编码=\u9FA5 ---> 汉字=龥

std::string stdStr("\u4e00\u9FA5");
stdStr // "一龥"
stdStr.c_str()[0] // (const char) '0xe4'
stdStr.size()  // (unsigned long) 6
for(int i=0; i < stdStr.size()+1; i++)   printf("%02x ",(unsigned char)(s2.c_str()[i])  );
// e4 b8 80 e9 be a5 00 

unicode-utf8编码中文第一个汉字: unicode编码=\u4e00 ----> utf8编码=0xe4_b8_80 ---> 汉字=一

unicode-utf8编码中文最后一个汉字:unicode编码=\u9FA5 ---> utf8编码=0xe9_be_a5 ----> 汉字=龥

由此可知 粗略判断 utf8编码的std::string stdStr首个字符是否为汉字 可以判定此首个字节stdStr.c_str()[0] 是否在范围0xe4~0xe9

hello
/app5/cling-build/bin/cling #进入交互式界面

#也可以脚本样式执行
echo '''
#include <iostream>
std::cout << "hello" << std::endl;
''' | /app5/cling-build/bin/cling
#输出hello
cling无子进程 且 默认只有一个线程
#cling无子进程:
pgrep --parent  `pidof cling`

#cling默认只有一个线程: 
ls /proc/`pidof cling`/task/ | wc -l  # == 1
查函数签名
#include <pthread.h>

pthread //tab tab 后 有相关结构体补全

pthread_create //回车后有函数签名
/*
(int (*)(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict) noexcept(true)) Function @0x7d1d8a494c40
  at /usr/include/pthread.h:202:
extern int pthread_create (pthread_t *__restrict __newthread,
			   const pthread_attr_t *__restrict __attr,
			   void *(*__start_routine) (void *),
			   void *__restrict __arg) __THROWNL __nonnull ((1, 3))
*/
c++ std 文本文件按行读取
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
std::string ln;
std::vector<std::string> lnVec;
std::ifstream ifile("f1.txt");
if(!ifile.is_open()) exit(1);
ifile.fail(); //false
while(true){
  std::getline(ifile,ln);
  if(ifile.fail()){ break; }
  lnVec.push_back(ln);
}
ifile.fail(); //true
lnVec;// (std::vector<std::string> &) { "aaaa", "bbb", "ccc" }
ifile.close();
ifile.is_open(); //false
mmap
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
int fsize=1024;
int fd = open("my_map_file", O_RDWR|O_CREAT,0666); // 正常: 非-1
ftruncate(fd, fsize); // 正常: 0
void * map = mmap(NULL, fsize, PROT_WRITE, MAP_SHARED, fd, 0); // 正常: 非MAP_FAILED
((char*)map)[0]='a';
((char*)map)[1]='b';
munmap(map,fsize); // 正常: 0
close(fd); // 正常: 0
ls -l my_map_file # == 1024
cat my_map_file # == ab
.help

/app5/cling-build/bin/cling 进入交互式界面



****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ .help

 Cling (C/C++ interpreter) meta commands usage
 All commands must be preceded by a '.', except
 for the evaluation statement { }
 ==============================================================================
 Syntax: .Command [arg0 arg1 ... argN]

   .L <filename>		- Load the given file or library

   .(x|X) <filename>[(args)]	- Same as .L and runs a function with
				  signature: ret_type filename(args)

   .> <filename>		- Redirect command to a given file
      '>' or '1>'		- Redirects the stdout stream only
      '2>'			- Redirects the stderr stream only
      '&>' (or '2>&1')		- Redirects both stdout and stderr
      '>>'			- Appends to the given file

   .undo [n]			- Unloads the last 'n' inputs lines

   .U <filename>		- Unloads the given file

   .(I|include) [path]		- Shows all include paths. If a path is given,
				  adds the path to the include paths.

   .O <level>			- Sets the optimization level (0-3)
				  If no level is given, prints the current setting.

   .class <name>		- Prints out class <name> in a CINT-like style (one-level).
				  If no name is given, prints out list of all classes.

   .Class <name>		- Prints out class <name> in a CINT-like style (all-levels).
				  If no name is given, prints out list of all classes.

   .namespace			- Prints list of all known namespaces

   .typedef <name>		- Prints out typedef <name> in a CINT-like style
				  If no name is given, prints out list of all typedefs.

   .files			- Prints names of all included (parsed) files

   .fileEx			- Prints out included (parsed) file statistics
				  as well as a list of their names

   .g <var>			- Prints out information about global variable
				  'var' - if no name is given, print them all

   .@ 				- Cancels and ignores the multiline input

   .rawInput [0|1]		- Toggle wrapping and printing the
				  execution results of the input

   .dynamicExtensions [0|1]	- Toggles the use of the dynamic scopes
				  and the late binding

   .debug <level>		- Generates debug symbols (level is optional, 0 to disable)

   .printDebug [0|1]		- Toggles the printing of input's corresponding
				  state changes

   .storeState <filename>	- Store the interpreter's state to a given file

   .compareState <filename>	- Compare the interpreter's state with the one
				  saved in a given file

   .stats [name]		- Show stats for internal data structures
				  'ast'  abstract syntax tree stats
				  'asttree [filter]'  abstract syntax tree layout
				  'decl' dump ast declarations
				  'undo' show undo stack

   .T <filePath> <comment>	- Generate autoload map

   .trace <repr> <id>		- Dump trace of requested respresentation
				  (see .stats arguments for <repr>)

   .help			- Shows this information (also .?)

   .q				- Exit the program


1. cling编译

vgvassilev/cling.git/readme.md/编译步骤

binD=/app5/cling-build
mkdir $binD

D=/app5/llvm-home
mkdir $D 

#欧洲高能物理数据分析 ROOT
git clone https://github.com/root-project/llvm-project.git $D
#/app5/llvm-home/llvm-project/.git/config
llvmD=$D/llvm-project # == /app5/llvm-home/llvm-project


git clone https://github.com/root-project/cling.git $D
#/app5/llvm-home/cling/.git/config
clingD=$D/cling # == /app5/llvm-home/cling

#配置
cmake -DLLVM_EXTERNAL_PROJECTS=cling -DLLVM_EXTERNAL_CLING_SOURCE_DIR=$clingD -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_TARGETS_TO_BUILD="host;NVPTX" -DCMAKE_BUILD_TYPE=Release $llvmD/llvm
#命令展开: cmake -DLLVM_EXTERNAL_PROJECTS=cling -DLLVM_EXTERNAL_CLING_SOURCE_DIR=/app5/llvm-home/cling -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_TARGETS_TO_BUILD="host;" -DCMAKE_BUILD_TYPE=Release /app5/llvm-home/llvm-project/llvm

#编译clang
cmake --build $binD  --target clang -j10

#编译cling
cmake --build  $binD --target cling -j10

issues/531#issuecomment-2337234891

所用版本

#/app5/llvm-home/llvm-project/.git/config
#llvmD=$D/llvm-project # == /app5/llvm-home/llvm-project
git --git-dir=$llvmD/.git rev-parse HEAD # == 66d752c5c714ac57b468e1b4d62a52f9207b5d44
git --git-dir=$llvmD/.git   branch  # == ROOT-llvm18
git --git-dir=$llvmD/.git --no-pager  log  --format=oneline | head -n 1
#66d752c5c714ac57b468e1b4d62a52f9207b5d44 Do not install {Clang,Cling}Config.cmake in the project lib dir.
 

#/app5/llvm-home/cling/.git/config
#clingD=$D/cling # == /app5/llvm-home/cling
git --git-dir=$clingD/.git rev-parse HEAD # == 1d4925536b9f89015ad2afdc24e260207dd69ebb
git --git-dir=$clingD/.git   branch  # == master
git --git-dir=$clingD/.git --no-pager  log  --format=oneline | head -n 1
#1d4925536b9f89015ad2afdc24e260207dd69ebb Move static init function renaming to `BackendPasses.cpp`

1B. cling编译备忘

若不编译clang会报错 resource directory /app5/llvm-home/cling-build/lib/clang/18 not found!

cd /app5/llvm-home/cling-build/
./bin/cling 
ERROR in cling::CIFactory::createCI():
  resource directory /app5/llvm-home/cling-build/lib/clang/18 not found!

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ .q

1C. cling运行依赖

/app5/cling-build/bin/cling

  1. /app5/llvm-home/cling/include/cling/**.h

  2. sudo apt install -y build-essential

cling-官方编译好的

vgvassilev/cling.git

夜间编译 / 2020-Dec-17-cling-Ubuntu-16.04-x86_64-0.8.dev-c054ac2.tar.bz2

上手 : On-the-fly-C++/cling-cpp-11-interpreter/

 cat /etc/issue # == Ubuntu 22.04.5 LTS \n \l

aria2c --all-proxy=http://westgw:7890   https://github.com/vgvassilev/cling/releases/download/cling-nightlies/2020-Dec-17-cling-Ubuntu-16.04-x86_64-0.8.dev-c054ac2.tar.bz2
tar -jxf 2020-Dec-17-cling-Ubuntu-16.04-x86_64-0.8.dev-c054ac2.tar.bz2

cling_D=/app5/cling-Ubuntu-16.04-x86_64-0.8~dev-c054ac2

export PATH_BASE=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH=$PATH_BASE:$jdk8_d/bin:$cling_D/bin

which cling # == /app5/cling-Ubuntu-16.04-x86_64-0.8~dev-c054ac2/bin/cling

cling运行报错, 估计 原因是 这cling 是基于ubuntu16编译的, 而我这是ubuntu22

ERROR in cling::CIFactory::createCI(): cannot extract standard library include paths!
Invoking:
  LC_ALL=C g++-5  -O3 -DNDEBUG -xc++ -E -v /dev/null 2>&1 | sed -n -e '/^.include/,${' -e '/^ \/.*++/p' -e '}'
Results was:
With exit code 0
input_line_1:1:10: fatal error: 'new' file not found
#include <new>
         ^~~~~
Warning in cling::IncrementalParser::CheckABICompatibility():
  Failed to extract C++ standard library version.

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ .q

欧洲高能物理数据分析 ROOT内置的cling

改为用 高能物理数据分析 ROOT / root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4.tar.gz 其内置了cling

结论: rootcling bare-cling 没有交互式界面, 直接退出了

aria2c https://root.cern/download/root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4.tar.gz

tar -xf root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4.tar.gz

#清理PATH
export PATH_BASE=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH=$PATH_BASE

rootD=/app5/root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4

#激活 高能物理ROOT 环境
source $rootD/bin/thisroot.sh
#命令展开: source /app5/root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4/bin/thisroot.sh

echo $PATH #== $rootD/bin:$PATH_BASE
echo $LD_LIBRARY_PATH # == $rootD/lib
echo  $ROOTSYS # == $rootD
which rootcling # == $rootD/bin/rootcling

rootcling  --help-list | grep bare
#bare-cling - Call directly cling and exit.


rootcling  bare-cling #没有交互式界面, 直接退出了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ziix

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值