위키로그:2019/08/07 - Perl의 내장 open() 함수

From PGWiki

GDB

# gdb --args `which perl` -e "open(my \$fh, '>', 'tmp.txt');"
...
(gdb) breakpoint main
...
(gdb) run
...
(gdb) breakpoint open
...
(gdb) continue
...
(gdb) backtrace
#0  0x00007ffff7f47190 in open64 () from /usr/lib/libpthread.so.0
#1  0x0000555555725b14 in PerlIOUnix_open (self=0x5555557b40e0 <PerlIO_unix>, layers=0x5555557caa30, n=0, mode=0x7fffffffd440 "w", fd=-1, imode=577, perm=438, f=0x0, narg=1, args=0x5555557bddb8) at perlio.c:2640
#2  0x0000555555727461 in PerlIOBuf_open (self=0x5555557b42a0 <PerlIO_perlio>, layers=0x5555557caa30, n=1, mode=0x7fffffffd440 "w", fd=-1, imode=0, perm=0, f=0x0, narg=1, args=0x5555557bddb8) at perlio.c:3845
#3  0x0000555555723c4f in PerlIO_openn (layers=0x5555557caa11 "", mode=0x7fffffffd440 "w", fd=-1, imode=0, perm=0, f=0x0, narg=1, args=0x5555557bddb8) at perlio.c:1537
#4  0x00005555556ed794 in Perl_do_open6 (gv=0x5555557bb3c8, oname=0x5555557e1240 ">", len=1, supplied_fp=0x0, svp=0x5555557bddb8, num_svs=1) at doio.c:478
#5  0x00005555556d53d9 in Perl_pp_open () at pp_sys.c:654
#6  0x000055555566d549 in Perl_runops_standard () at run.c:41
#7  0x00005555555ac0ab in S_run_body (oldscope=1) at perl.c:2483
#8  0x00005555555abc56 in perl_run (my_perl=0x5555557b9260) at perl.c:2406
#9  0x0000555555582067 in main (argc=3, argv=0x7fffffffd7b8, env=0x7fffffffd7d8) at perlmain.c:116
(gdb) frame 3
#3  0x0000555555723c4f in PerlIO_openn (layers=0x5555557caa11 "", mode=0x7fffffffd440 "w", fd=-1, imode=0, perm=0, f=0x0, narg=1, args=0x5555557bddb8) at perlio.c:1537
1537                     f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
(gdb) p *args
$1 = (SV *) 0x5555557dc210
(gdb) p **args
$2 = {
  sv_any = 0x5555557bc190, 
  sv_refcnt = 1, 
  sv_flags = 402736131, 
  sv_u = {
    svu_pv = 0x5555557ca420 "tmp.txt", 
    svu_iv = 93824994812960, 
    svu_uv = 93824994812960, 
    svu_nv = 4.6355706658316689e-310, 
    svu_rv = 0x5555557ca420, 
    svu_rx = 0x5555557ca420, 
    svu_array = 0x5555557ca420, 
    svu_hash = 0x5555557ca420, 
    svu_gp = 0x5555557ca420, 
    svu_fp = 0x5555557ca420
  }
}
(gdb) # 여기서부터 해당 SV가 파일 핸들이 할당되는 과정을 확인할 것