hns - 日記自動生成システム - Version 2.19.9

先月 2014年06月 来月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Namazu for hns による簡易全文検索
詳しくは 詳細指定/ヘルプを参照して下さい
検索式:

2014年06月27日(金) 旧暦 [n年日記] [更新:"2014/06/27 23:14:48"]

#1 [debug] fgetc Segmentation Fault at malloc

#0  0x00007f7ff789df92 in ?? () from /usr/lib/libc.so.12
#1  0x00007f7ff789e3d0 in ?? () from /usr/lib/libc.so.12
#2  0x00007f7ff789e689 in malloc () from /usr/lib/libc.so.12
#3  0x00007f7ff78ed28c in __smakebuf () from /usr/lib/libc.so.12
#4  0x00007f7ff78e24f8 in __srefill () from /usr/lib/libc.so.12
#5  0x00007f7ff78e2384 in __srget () from /usr/lib/libc.so.12
#6  0x00007f7ff7843997 in fgetc () from /usr/lib/libc.so.12
これはどういう時かな。確かに読んでいるファイルは大きいのだけれど。
% wc hoge.5itmp 
   43213  152201 1897759 hoge.5itmp
1.9MByte かな。でもそんなにやたらに大きということでもないと思う
次のような文字を教えても google は見向きもしない(ようだ)
in malloc ()
in __smakebuf ()
in __srefill ()
in __srget ()
in fgetc () from
ちなみに、次のところで説明されている問題は、まだ理解出来ていない。
fgetc が malloc を(勝手に)呼んでいるとして、そのメモリは、いつ free されるのだろうか ?

上記の答えは、「file descriptor を close すれば、開放される」
include/stdio.h

   112  typedef struct __sFILE {
   113          unsigned char *_p;      /* current position in (some) buffer */
   114          int     _r;             /* read space left for getc() */
   115          int     _w;             /* write space left for putc() */
   116          unsigned short _flags;  /* flags, below; this FILE is free if 0 */
   117          short   _file;          /* fileno, if Unix descriptor, else -1 */
   118          struct  __sbuf _bf;     /* the buffer (at least 1 byte, if !NULL) */
   119          int     _lbfsize;       /* 0 or -_bf._size, for inline putc */
   120  
   121          /* operations */
   122          void    *_cookie;       /* cookie passed to io functions */
   123          int     (*_close)(void *);
   124          int     (*_read) (void *, char *, int);
   125          __off_t (*_seek) (void *, __off_t, int);
   126          int     (*_write)(void *, const char *, int);
   127  
   128          /* file extension */
   129          struct  __sbuf _ext;
   130  
   131          /* separate buffer for long sequences of ungetc() */
   132          unsigned char *_up;     /* saved _p when _p is doing ungetc data */
   133          int     _ur;            /* saved _r when _r is counting ungetc data */
   134  
   135          /* tricks to meet minimum requirements even when malloc() fails */
   136          unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
   137          unsigned char _nbuf[1]; /* guarantee a getc() buffer */
   138  
   139          /* Formerly used by fgetln/fgetwln; kept for binary compatibility */
   140          struct  __sbuf _lb__unused;
   141  
   142          /* Unix stdio files get aligned to block boundaries on fseek() */
   143          int     _blksize;       /* stat.st_blksize (may be != _bf._size) */
   144          __off_t _offset;        /* current lseek offset */
   145  } FILE;
   146  
lib/libc/stdio/makebuf.c
    55  /*
    56   * Allocate a file buffer, or switch to unbuffered I/O.
    57   * Per the ANSI C standard, ALL tty devices default to line buffered.
    58   *
    59   * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
    60   * optimisation) right after the fstat() that finds the buffer size.
    61   */
    62  void
    63  __smakebuf(fp)
    64          FILE *fp;
    65  {
    66          void *p;
    67          int flags;
    68          size_t size;
    69          int couldbetty;
    70
    71          _DIAGASSERT(fp != NULL);
    72
    73          if (fp->_flags & __SNBF) {
    74                  fp->_bf._base = fp->_p = fp->_nbuf;
    75                  fp->_bf._size = 1;
    76                  return;
    77          }
    78          flags = __swhatbuf(fp, &size, &couldbetty);
    79          if ((p = malloc(size)) == NULL) {
    80                  fp->_flags |= __SNBF;
    81                  fp->_bf._base = fp->_p = fp->_nbuf;
    82                  fp->_bf._size = 1;
    83                  return;
    84          }
    85          __cleanup = _cleanup;
    86          flags |= __SMBF;
    87          fp->_bf._base = fp->_p = p;
    88          fp->_bf._size = size;
    89          if (couldbetty && isatty(__sfileno(fp)))
    90                  flags |= __SLBF;
    91          fp->_flags |= flags;
    92  }
上の中で size が気になる時には __swhatbuf も見る必要がある
    94  /*
    95   * Internal routine to determine `proper' buffering for a file.
    96   */
    97  int
    98  __swhatbuf(fp, bufsize, couldbetty)
    99          FILE *fp;
   100          size_t *bufsize;
   101          int *couldbetty;
   102  {
   103          struct stat st;
   104
   105          _DIAGASSERT(fp != NULL);
   106          _DIAGASSERT(bufsize != NULL);
   107          _DIAGASSERT(couldbetty != NULL);
   108
   109          if (__sfileno(fp) == -1 || fstat(__sfileno(fp), &st) < 0) {
   110                  *couldbetty = 0;
   111                  *bufsize = BUFSIZ;
   112                  return (__SNPT);
   113          }
   114
   115          /* could be a tty iff it is a character device */
   116          *couldbetty = S_ISCHR(st.st_mode);
   117          if (st.st_blksize == 0) {
   118                  *bufsize = BUFSIZ;
   119                  return (__SNPT);
   120          }
   121
   122          /*
   123           * Optimise fseek() only if it is a regular file.  (The test for
   124           * __sseek is mainly paranoia.)  It is safe to set _blksize
   125           * unconditionally; it will only be used if __SOPT is also set.
   126           */
   127          *bufsize = st.st_blksize;
   128          fp->_blksize = st.st_blksize;
   129          return ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ?
   130              __SOPT : __SNPT);
   131  }
st というのは 103 行目 にあるが struct stat の構成要素(member)だ
modena@makoto 23:14:17/140627(/export/src)% (cd include;mkid; gid BUFSIZ)
stdio.h:181:#define     BUFSIZ  1024            /* size of buffer used by setbuf */
というのもある



最近の日記
2024年05月08日
comparison on ./buildsh tools
2024年05月06日
py-setuptools (python 3.11.9)
make release took 1 hours and 10 min
qemu invocation for 10.99.10
2024年05月05日
Windows 10 version
serial connection
bc bench
2024年05月04日
Trial on 10.99.10
another version (later trial) to succeed
2024年04月29日
dkim
以上、1 日分です。
タイトル一覧
カテゴリ分類
Powered by hns-2.19.9, HyperNikkiSystem Project

Count.cgi (since 2000/02/05)