python c++ summary pdb gdb frame registers 调试器,栈帧,寄存器的查看

发布时间:2023年12月17日

pdb

  • import pdb
  • pdb.set_trace() # 设置追踪断点
命令说明
p a打印a
q退出
n下一行
s执行下一行(能够进入函数体)
r执行下一行(在函数中时会直接执行到函数返回处)
w打印堆栈信息
unt N执行到第N行
disasdisplay assembler code

pdb 显示的结果过长

如果 pdb 显示的结果过长,您可以使用 q 命令来退出当前的 pdb 调试会话,然后使用 less 命令在终端中查看 pdb 输出的结果。例如,您可以在调试会话中使用以下命令:

(Pdb) !less some_file.txt

这将会使用 less 命令来查看名为 some_file.txt 的文件,您可以将 some_file.txt 替换为您要查看的文件名或变量名。在使用 less 命令时,您可以使用 q 键来退出查看模式。

另外,如果您只是想查看变量的值,您可以使用 p 命令来打印变量的值,例如:

(Pdb) p some_var

这将会打印变量 some_var 的值。如果变量的值很长,您可以使用 pprint 模块来格式化打印输出。例如:

(Pdb) import pprint
(Pdb) pprint.pprint(some_var)

这将会使用 pprint 模块来格式化打印变量 some_var 的值。

inspect

  • https://docs.python.org/3/library/inspect.html
(Pdb) p opts
<modules.shared.Options object at 0x7fbdc46dad10>
(Pdb) p dir(opts)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add_option', 'cast_value', 'data', 'data_labels', 'dumpjson', 'get_default', 'load', 'onchange', 'reorder', 'same_type', 'save', 'set', 'typemap']

(Pdb) import inspect 
(Pdb) inspect.getmembers(opts)
[('__class__', <class 'modules.shared.Options'>), ('__delattr__', <method-wrapper '__delattr__' of Options object at 0x7fbdc46dad10>), ('__dict__', {'data': {'samples_save': True, 'samples_format': 'png', 'samples_filename_pattern': '', 'save_images_add_number': True, 'grid_save': True, 'grid_format': 'png', 'grid_extended_filename': False, 'grid_only_if_multiple': True, 'grid_prevent_empty_spots': False, 'n_rows': -1, 'enable_pnginfo': True, 'save_txt': False, 'save_images_before_face_restoration': True, 'save_images_before_highres_fix': True, 'save_images_before_color_correction': False, 'save_mask': False, 'save_mask_composite': False, 'jpeg_quality': 80, 'webp_lossless': False, 'export_for_4chan': True, 'img_downscale_threshold': 4.0, 'target_side_length': 4000, 'img_max_size_mp': 200, 'use_original_name_batch': True, 'use_upscaler_name_as_suffix': False, 'save_selected_only': True, 'save_init_img': False, 'temp_dir': '', 'clean_temp_dir_at_start': False, 'outdir_samples': '', 'outdir_txt2img_samples': 'outputs/txt2img-images', 'outdir_img2img_samples': 'outputs/img2img-images', 'outdir_extras_samples': 'outputs/extras-images', 'outdir_grids': '', 'outdir_txt2img_grids': 'outputs/txt2img-grids', 'outdir_img2img_grids': 'outputs/img2img-grids', 'outdir_save': 'log/images', 'outdir_init_images': 'outputs/init-images', 'save_to_dirs': True, 'grid_save_to_dirs': True, 'use_save_to_dirs_for_ui': False, 'directories_filename_pattern': '[date]', 'directories_max_prompt_words': 8, 'ESRGAN_tile': 192, 'ESRGAN_tile_overlap': 8, 'realesrgan_enabled_models': ['R-ESRGAN 4x+', 'R-ESRGAN 4x+ Anime6B'], 'upscaler_for_img2img': None, 'face_restoration_model': 'CodeFormer', 'code_former_weight': 0.5, 'face_restoration_unload': False, 'show_warnings': False, 'memmon_poll_rate': 8, 'samples_log_stdout': False, 'multiple_tqdm': True, 'print_hypernet_extra': False, 'list_hidden_files': True, 'unload_models_when_training': False, 'pin_memory': False, 'save_optimizer_state': False, 'save_training_settings_to_txt': True, 'dataset_filename_word_regex': '', 'dataset_filename_join_string': ' ', 'training_image_repeats_per_epoch': 1, 'training_write_csv_every': 500, 'training_xattention_optimizations': False, 'training_enable_tensorboard': False, 'training_tensorboard_save_images': False

gdb

one example

~/Downloads/zip$ vim test.cpp 
~/Downloads/zip$ gcc test.cpp -o test
~/Downloads/zip$ ./test 
段错误
~/Downloads/zip$ gcc test.cpp -g -rdynamic
~/Downloads/zip$ ls
a.out   test  test.cpp  
~/Downloads/zip$ gdb a.out 
GNU gdb (Ubuntu 12.0.90-0ubuntu1) 12.0.90
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...
(gdb) n
The program is not being run.
(gdb) r
Starting program: /home/pdd/Downloads/zip/a.out 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00005555555551a0 in main () at test.cpp:7
7       int main(){
(gdb) q
A debugging session is active.

        Inferior 1 [process 9706] will be killed.

Quit anyway? (y or n) y
rrun
print *arr@5将打印指针arr所指向的连续5个元素的值,p sizeof(*hf_adam::opt), print *arr@60
nnew line
continue或c继续执行程序直到下一个断点处停止
list显示后续代码
backtrace或bt显示当前调用堆栈的完整信息
b 7第七行断点
b ff函数断点
info查看断点
sstep into
watch *0x6fffffffffffffff

frame

  • "frame"是GDB命令之一,它的作用是跳转和操作调用栈中的帧(Frame)。通过使用"frame"命令,你可以切换到不同的栈帧,以查看和修改正在调用堆栈中的活动函数。栈帧是函数调用过程中存储局部变量、参数和返回地址的区域。使用"frame"命令可以帮助你在调试过程中深入了解函数调用的细节,并检查变量的状态和执行路径。同时,它还提供了调试乃至修改代码的能力。

下面是一些"frame"命令的常用子命令和用法:

  1. “frame”:不带任何参数,显示当前栈帧的信息。
(gdb) frame
#0  stack_usage_test (tag=171, stack_size=65536, top=0x7ffffffee300 "\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253"...) at /home/pdd/leetcode/untitled1/main.cpp:14
14	    return stack_size - unused;
  1. “frame n”:切换到第n个栈帧,其中n是栈帧的索引号。
  2. “up"或"down”:在栈帧之间进行上下切换。
  3. “info frame”:显示当前栈帧的完整信息。
(gdb) info frame
Stack level 0, frame at 0x7fffffffe340:
 rip = 0x5555555551d6 in stack_usage_test (/home/pdd/leetcode/untitled1/main.cpp:14); saved rip = 0x555555555392
 called by frame at 0x7fffffffe370
 source language c++.
 Arglist at 0x7fffffffe330, args: tag=171, stack_size=65536, top=0x7ffffffee300 "\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253"...
 Locals at 0x7fffffffe330, Previous frame's sp is 0x7fffffffe340
 Saved registers:
  rbp at 0x7fffffffe330, rip at 0x7fffffffe338

(gdb) info frame 是gdb调试器中用于显示当前执行上下文信息的命令。根据提供的信息,以下是每个字段的解释:

  • Stack level 0, frame at 0x7fffffffe340:表示当前栈帧的级别,0表示当前栈帧;0x7fffffffe340 是栈帧的地址。

  • rip = 0x5555555551d6 in stack_usage_test (/home/pdd/leetcode/untitled1/main.cpp:14):表示当前指令的指令指针(程序计数器)的值为 0x5555555551d6。同时,还提供了函数所在源文件和行号信息(/home/pdd/leetcode/untitled1/main.cpp:14),表明当前指令位于main.cpp文件的第14行。

  • saved rip = 0x555555555392:表示保存着上一个栈帧的返回地址(返回指令指针值)为 0x555555555392

  • called by frame at 0x7fffffffe370:表示当前栈帧被位于 0x7fffffffe370 地址的上一个栈帧调用。

  • Source language c++:表示该栈帧中代码的编程语言为C++。

  • Arglist at 0x7fffffffe330, args: tag=171, stack_size=65536, top=0x7ffffffee300 “\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253”:表示函数的参数存储在 0x7fffffffe330 地址处,具体的参数值为 tag=171, stack_size=65536, top=0x7ffffffee300

  • Locals at 0x7fffffffe330:表示函数的局部变量存储在 0x7fffffffe330 地址处。

  • Previous frame’s sp is 0x7fffffffe340:表示上一个栈帧的栈指针(栈顶指针)值为 0x7fffffffe340

  • Saved registers: 列出在该栈帧中保存的寄存器的值,其中 rbp 的值为 0x7fffffffe330rip 的值为 0x7fffffffe338

  1. “info args”:显示当前栈帧的函数参数信息。
(gdb) info args
tag = 171
stack_size = 65536
top = 0x7ffffffee300 "\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253\253"...

  1. “info locals”:显示当前栈帧的局部变量信息。

当使用alloca分配地址时,rip的变化时增加还是减少?

在使用动态分配函数 alloca 分配内存地址时,rip 寄存器的变化方式取决于编译器和平台的具体实现。

alloca 函数用于在栈上动态分配内存,分配的内存空间在函数返回后自动释放。当在函数内部调用 alloca 分配内存时,rip 寄存器的变化通常是增加的。

这是因为 alloca 在大多数编译器和平台上实现为在栈上分配内存。栈的生长方向是向下的,也就是说,栈的高地址是先分配的,低地址是后分配的。而 rip 寄存器则指向当前执行的指令地址,随着指令的执行,它通常会递增,指向下一条指令。因此,当 alloca 在栈上分配内存时,栈的指针会向低地址方向移动,而 rip 寄存器的值会随之增加。

需要注意的是,这是一个一般情况的描述,具体的实现可能会有所不同,特别是在不同的编译器和体系结构上。因此,在实际应用中,如果对 rip 寄存器的值有特定的依赖或需求,建议查阅相关编译器和平台的文档或规范以了解更多细节。

gdb 查看 寄存器

  • 可以使用 info registers 命令或 i r 命令查看所有寄存器的值。
(gdb) i r
rax            0x7fffffffe2d0      140737488347856
rbx            0x0                 0
rcx            0x7fffffffe2f0      140737488347888
rdx            0x20                32
rsi            0xa                 10
rdi            0x10                16
rbp            0x7fffffffe330      0x7fffffffe330
rsp            0x7fffffffe2d0      0x7fffffffe2d0
r8             0x7ffff7f8ff10      140737353678608
r9             0x7ffff7fc9040      140737353912384
r10            0x7ffff7fc3908      140737353890056
r11            0x7ffff7fde680      140737354000000
r12            0x7fffffffe478      140737488348280
r13            0x555555555573      93824992236915
r14            0x555555557db0      93824992247216
r15            0x7ffff7ffd040      140737354125376
rip            0x55555555528e      0x55555555528e <stack_usage_fill(int, size_t)+174>
eflags         0x202               [ IF ]
cs             0x33                51
ss             0x2b                43
ds             0x0                 0
es             0x0                 0
fs             0x0                 0
gs             0x0                 0
k0             0x10000000          268435456
k1             0x40001             262145
k2             0x0                 0
k3             0x0                 0
k4             0x0                 0
k5             0x0                 0
k6             0x0                 0
k7             0x0                 0
  • 可以在 gdb 命令行中输入 info registers rsp 或者简写为 i r rsp 命令来查看 rsp 寄存器的值。
(gdb) i r rsp
rsp            0x7fffffffe2d0      0x7fffffffe2d0

Breakpoint 4, stack_usage_fill (tag=171, stack_size=10) at /home/pdd/valgrind/untitled1/main.cpp:20
20	    char* top3 = (char*)alloca(1);
(gdb) i r rsp
rsp            0x7fffffffe2c0      0x7fffffffe2c0
  • 在某些情况下,调用 alloc(1) 分配内存,实际分配的内存大小可能会大于请求的字节数。这通常是由于内存对齐的需要。内存对齐是指在分配内存时,要求按照特定的字节边界对齐数据。在许多系统上,内存对齐的默认边界是 8 字节或 16 字节。这意味着即使请求分配 1 字节的内存,系统也可能分配更多的字节,以满足对齐的要求。因此,当你调用 alloc(1) 分配内存时,返回的指针可能会增加为 16 字节。

core

  • ulimit -a 查看用户的使用限制
  • ulimit -c unlimited // 不限制core大小,也可指定具体大小ulimit -c 5000
  • gdb ./a.out core.***

正在运行的程序

  • gdb -p pid 即可

CG

文章来源:https://blog.csdn.net/ResumeProject/article/details/130132421
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。