LinuxϵͳץȡCoreÎļþʵս¼¼ÇÉ
linux×¥core

Ê×Ò³ 2024-12-08 03:07:31



Linux×¥Core£º¸ßЧ¹ÊÕÏÅŲéµÄ±Ø±¸¼¼ÄÜ ÔÚÈí¼þ¿ª·¢ºÍϵͳÔËά¹ý³ÌÖУ¬³ÌÐò±ÀÀ£»òÒì³£ÊÇÄÑÃâÓöµ½µÄÎÊÌâ

    ÎªÁË¿ìËÙ¶¨Î»ºÍ½â¾öÕâЩÎÊÌ⣬LinuxϵͳÌṩÁËÒ»ÖÖÇ¿´óµÄ»úÖÆ¡ª¡ªÉú³ÉcoreÎļþ£¨Ò²³ÆΪºËÐÄת´¢Îļþ£¬coredump£©

    ±¾ÎĽ«Ïêϸ½éÉÜÈçºÎÔÚLinuxϵͳÖÐÊÕ¼¯ºÍ·ÖÎöcoreÎļþ£¬°ïÖú¶ÁÕßÕÆÎÕÕâÒ»ÖØÒªµÄ¹ÊÕÏÅŲ鼼ÄÜ

     Ò»¡¢CoreÎļþµÄÖØÒªÐÔ CoreÎļþÊDzÙ×÷ϵͳÔÚ½ø³ÌÔËÐÐÒì³£ÖÕÖ¹»ò±ÀÀ£Ê±£¬½«³ÌÐòµ±Ê±µÄÄÚ´æ״̬¼Ç¼ÏÂÀ´£¬±£´æÔÚÒ»¸öÎļþÖеĻúÖÆ

    Õâ¸öÎļþ°üº¬Á˳ÌÐò±ÀÀ£Ê±µÄÄÚ´æÐÅÏ¢¡¢¼Ä´æÆ÷״̬¡¢ÄÚ´æ¹ÜÀíÐÅÏ¢ÒÔ¼°ÆäËû´¦ÀíÆ÷ºÍ²Ù×÷ϵͳ״̬ÐÅÏ¢

    ¿ÉÒÔ½«ÆäÊÓΪ¡°ÄÚ´æ¿ìÕÕ¡±£¬¶ÔÓÚ±à³ÌÈËÔ±Õï¶ÏºÍµ÷ÊÔ³ÌÐò·Ç³£ÓаïÖú

     ÀýÈ磬µ±³ÌÐò·¢ÉúSegmentation fault£¨¶Î´íÎó£©Ê±£¬Í¨³£ÊÇÓÉÓÚÄÚ´æ²»µ±²Ù×÷Ôì³ÉµÄ£¬Èç¿ÕÖ¸Õë¡¢Ò°Ö¸ÕëµÄ¶Áд²Ù×÷£¬Êý×éÔ½½ç·ÃÎʵÈ

    ÕâЩ´íÎóºÜÄÑÖØÏÖ£¬µ«coreÎļþ¿ÉÒÔÔÙÏÖ³ÌÐò³ö´íʱµÄÇé¾°£¬´Ó¶ø°ïÖú¿ª·¢ÈËԱѸËÙÕÒµ½ÎÊÌâ¸ùÔ´

     ¶þ¡¢ÉèÖÃÓëÉú³ÉCoreÎļþ ÔÚLinuxϵͳÖУ¬Éú³ÉcoreÎļþÐèÒª½øÐÐһЩÉèÖÃ

    ÒÔÏÂÊǾßÌå²½Ö裺 1.¼ì²éulimitÉèÖà ʹÓÃ`ulimit`ÃüÁî¿ÉÒԲ鿴ºÍÐÞ¸ÄÓû§¼¶±ðµÄ×ÊÔ´ÏÞÖÆ

    È·±£core file×ÊÔ´ÏÞÖƲ»Îª0£¬ÕâÑù²ÅÄÜÉú³ÉcoreÎļþ

    ¿ÉÒÔͨ¹ýÒÔÏÂÃüÁî²é¿´µ±Ç°coreÎļþ´óСÏÞÖÆ£º bash ulimit -c Èç¹ûÊä³öΪ0£¬±íʾ½ûÓÃÁËcoreÎļþÉú³É

    ¿ÉÒÔͨ¹ýÒÔÏÂÃüÁîÀ´ÐÞ¸ÄÏÞÖÆ£º bash ulimit -c unlimited Õ⽫ÉèÖÃcoreÎļþ´óСÏÞÖÆΪÎÞÏÞÖÆ

     2.È·¶¨coreÎļþµÄÉú³ÉĿ¼ ĬÈÏÇé¿öÏ£¬coreÎļþ½«Éú³ÉÔÚµ±Ç°¹¤×÷Ŀ¼

    µ«¿ÉÒÔͨ¹ý²é¿´ºÍÐÞ¸Ä`/proc/sys/kernel/core_pattern`ÎļþÀ´Ö¸¶¨Éú³ÉĿ¼

    Ê¹ÓÃÒÔÏÂÃüÁî²é¿´µ±Ç°coreÎļþ´æ´¢Ä¿Â¼£º bash cat /proc/sys/kernel/core_pattern Èç¹ûÐèÒª¸ü¸ÄÉú³ÉĿ¼£¬¿ÉÒԱ༭`/etc/sysctl.conf`Îļþ£¬Ìí¼Ó»òÐÞ¸ÄÒÔϲÎÊý£º bash kernel.core_pattern = /path/to/corefile/core-%e-%p ÆäÖУ¬`%e`±íʾÃüÁîÃû£¬`%p`±íʾ½ø³ÌID

    ±£´æÎļþºó£¬Ö´ÐÐ`sysctl -p`ʹÉèÖü´Ê±ÉúЧ

     3.´¥·¢coreÎļþÉú³É ÏÖÔÚ£¬¿ÉÒÔͨ¹ý¹ÊÒâÈóÌÐò³ö´í¡¢·¢ËÍÐźţ¨ÈçSIGSEGV£©»òÖ´ÐÐÆäËûµ¼Ö³ÌÐòÒì³£Í˳öµÄ²Ù×÷À´´¥·¢coreÎļþÉú³É

    ÀýÈ磬±àдһ¸ö¹ÊÒâµ¼Ö¶δíÎóµÄC³ÌÐò£º c include include intmain(){ intp = NULL; p = 10; // Õ⽫µ¼Ö¶δíÎó return 0; } ±àÒë²¢ÔËÐиóÌÐò£¬Ëü½«±ÀÀ£²¢Éú³ÉcoreÎļþ

     Èý¡¢²éÕÒÓë¹ÜÀíCoreÎļþ ÔÚLinuxϵͳÖУ¬²éÕÒcoreÎļþµÄλÖÃÐèҪһЩÃüÁîºÍ¼¼ÇÉ

    ÒÔÏÂÊǾßÌå·½·¨£º 1.ʹÓÃfindÃüÁî²éÕÒ ¿ÉÒÔʹÓÃ`find`ÃüÁîÀ´²éÕÒϵͳÖÐËùÓеÄcoreÎļþ

    ÀýÈ磬ÔÚÕû¸öÎļþϵͳÖÐËÑË÷ÒÔ¡°core¡±¿ªÍ·µÄÎļþ£º bash find / -name core -print Õ⽫ÁгöËùÓÐÕÒµ½µÄcoreÎļþ·¾¶

     2.²é¿´ÏµÍ³Ä¬ÈϵÄcoreÎļþ´æ´¢Ä¿Â¼ ͨ¹ý`cat /proc/sys/kernel/core_pattern`ÃüÁî¿ÉÒԲ鿴ϵͳµ±Ç°µÄcoreÎļþ´æ´¢Ä¿Â¼

    ÕâÓÐÖúÓÚ¿ìËÙ¶¨Î»coreÎļþµÄλÖÃ

     3.ʹÓÃgcoreÃüÁîÉú³ÉcoreÎļþ Èç¹û³ÌÐòÔÚÔËÐйý³ÌÖÐûÓÐÉú³ÉcoreÎļþ£¬¿ÉÒÔʹÓÃ`gcore`ÃüÁîÖ÷¶¯Éú³ÉÒ»¸öcoreÎļþ

    ÀýÈ磺 bash