Menu
  • HOME
  • TAGS

GDB and LLDB “swallow” status set by child process in OS X

Tag: gdb,fork,osx-yosemite,lldb,sigsegv

Given the following code:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int ac, char** av) {
    int status;
    pid_t cpid = fork();
    if(0 == cpid) { /* Child */
        return *(volatile int*) 0; /* exits with signal 11 */
    } else { /* Parent */
        do {
            waitpid(cpid, &status, WUNTRACED);
            if(WIFSIGNALED(status)) {
                printf("\nChild exited with signal %d\n\n", WTERMSIG(status));
                return 0;
            }
        } while (!WIFEXITED(status) && !WIFSIGNALED(status));
        printf("\nChild exited normally\n\n");
    }
    return 0;
}

I get the expected result running the app from Terminal:

$ ./Fork4GdbTests.exe

Child exited with signal 11

Running the app within LLDB (or GDB), strangely, I get:

$ lldb ./Fork4GdbTests.exe
(lldb) target create "./Fork4GdbTests.exe"
Current executable set to './Fork4GdbTests.exe' (x86_64).
(lldb) r
Process 46815 launched: './Fork4GdbTests.exe' (x86_64)

Child exited normally

Process 46815 exited with status = 0 (0x00000000) 

My Makefile looks like this (used for Cygwin, also):

CC = gcc
CFLAGS = -Wextra -Wall -Werror -Wno-unused-parameter -g
.PHONY: all clean
all: Fork4GdbTests.exe
%.exe: %.o
    $(CC) $(CFLAGS) $? $(LIBS) -o [email protected]
clean:
    rm -f *.o *.exe *.stackdump;

In Cygwin, I get the expected result both running from the command prompt and in the debugger. Similar behavior occurs for all kinds of other signals, such as SIGFPE or any signals sent to the child by means of kill().

What is going on?

Best How To :

That's just a bug. Given it affects both gdb & lldb it is probably in CoreOS not the debuggers (though the same folks did the Mach specific layer of both debuggers so that's not a guarantee...)

Anyway, please file a bug report with http://bugreporter.apple.com.

How to write/read multiple times to a pipe

c,pipe,fork,ipc

Your code causes a deadlock. You're using read() function in a wrong way. read(pipe_a[0],buf,sizeof(buf) +sizeof(buf) ) You expect twice of size of your buffer and want to put that amount of bytes in your buffer. So read in child A waits for pipe_b and vice versa. Therefore child B can't...

Debugin UDI program by gdb

linux,gdb

Change the order of your struct like this struct user_info { char name[16]; uid_t uid; }; It will override as your expect....

My 64 bit machine can only store 4 bytes each memory location

c,memory,gdb,64bit,32bit-64bit

Each memory location can only store eight bits, because the memory is byte addressable. A 64-bit machine doesn't give you 64 bits in every memory location, it simply means that it can naturally handle 64 bits at a time. For example, registers are 64 bits wide (unless you intentionally manipulate...

How do I fork an external process in java

java,process,operating-system,fork

I suggest you prefer a ProcessBuilder over Runtime.exec. Also, if I understand your qestion, then you can pass the full path to the exe file to your ProcessBuilder. Something like, ProcessBuilder pb = new ProcessBuilder("C:\\Windows\\system32\\calc.exe"); pb.inheritIO(); // <-- passes IO from forked process. try { Process p = pb.start(); //...

fork is giving weird output when trying to add variable from child to father after killing child in c

c,arrays,linux,fork

Not really orthodox but you could do this: if(pid!=0){ for(i=0;i<max/2;i++){ if(vet[i]==k){ l++; } } //printf("trovati figlio %i\n",l); wait(&j); j = WEXITSTATUS(j); }else{ for(i=max/2;i<max;i++){ if(vet[i]==k){ j++; } } return j; } ...

Set a breakpoint into LibC with gdb

linker,gdb,debug-symbols,libc

I don't know how you came up with the symbol name that you are using, but here's what I see on my system (Ubuntu 14.04.1): $ objdump --dynamic-syms /lib/x86_64-linux-gnu/libc.so.6 |grep vfprintf 0000000000049cf0 g DF .text 00000000000051a8 GLIBC_2.2.5 _IO_vfprintf 00000000001097e0 g DF .text 0000000000000111 GLIBC_2.3.4 __vfprintf_chk 0000000000049cf0 g DF .text 00000000000051a8...

Stuck on wait() after fork()

c,fork,wait

It is an error for a process to call wait() when it has no uncollected children (wait() will return -1 and set errno to ECHILD in that case), so the fact that wait() hangs indicates that there is a child process that has not been collected. Once that child process...

How to get the symbol name of JVM native level function?

java,gdb

I think the JNI spec pointed to by the comment is the principled way to do this. However, it also is not too hard to find the function interactively from gdb. All you need to know is the name of your method (let's call it "hello") and that the JNI...

Wait for all processes reach some point in program, then resume

c,linux,synchronization,fork

This seems tailor-made for a semaphore. Specifically, this is easy to implement with "System V semaphores". See semget(2) and semop(2). The idea is that you obtain a semaphore in the parent, initialize its value to N, then have each child as it's "ready" decrement the value by 1. All children...

Program stuck on Pipe (exec ls grep sort)

c,linux,pipe,fork,dup2

You never close pipe_fd1 on the parent, so grep and sort doen't know when to stop reading input: because the pipe read and write ends are never closed on the parent, the reader blocks waiting for more input that will never arrive. You need to close it. Also, you don't...

Drawing with ncurses, sockets and fork

c,sockets,fork

The problem is that your parent process is trying to do two different things that need waiting: "barber sleeps until someone wakes him" accept() Without some kind of joint waiting, this architecture does not work. Both pieces of code sleeps waiting for a single kind of event, disregarding the other...

I'm confused how this execvp() is handled in this sample function which uses fork() to clone a process

c++,c,linux,exec,fork

You can have a look at the manpage for execvp, it says: The exec() family of functions replaces the current process image with a new process image. So, what does that mean? It means, if execvp succeeds, your program wont be in memory anymore, thus it wont ever reach the...

How to find illegal instructions in a program?

c++,c,assembly,gdb,benchmarking

Your hex dump is just a bunch of function names so it doesn't tell us much. And you didn't mention an operating system either... I'll assume if you can run gdb on it, you can use GNU binutils too. For a start you can try objdump -h myprog. It will...

gdb giving a function name followed by a number instead of file and line number

c,debugging,segmentation-fault,gdb

Most likely, in your case, debug symbols are not present in the binary. That is why, gdb is not able to read the debugging info and display them. Re-compile your code, with the debugging enabled. Example: for gcc, use the -g options....

how stack differs in 32 bit and 64 bit processors

c,assembly,gdb,disassembling

char * args[2]; Is an array of two pointers. On 32-bit machines, pointers (addresses, but not necessarily) are 32 bits wide, and on 64-bit machines, pointers (and never addresses) are 64 bits wide. That means, that the area on the stack in the current stack frame must be 2 *...

how to use a GDB input file for multiple input

c,input,gdb

I am wondering how GDB's input works. Your problem doesn't appear to have anything to with GDB, and everything to do with bugs in your program itself. First, if you run the program outside of GDB in the same way, namely: ./a.out < in you should see the same...

How can I debug the Bourne Shell with gdb?

bash,gdb,tar

Shells come with their own debugging tools. The most simple is running them with -x (bash -x ...script...) which will print each command after variable expansion but before it is executed. That's usually enough to determine the problem. For more ideas, see How to debug a bash script? You should...

Messing with signals, pipes and forks in C

c,bash,shell,signals,fork

A simple (but wasteful) option is that the initial program forks and then wait for input. The child process can update the counter. When the parent program receive "pause" it sends the signal SIGTSTP to the child. When the parent program receive "continue" it sends the signal SIGCONT to the...

Concurrent programming - Is it necessary to manually lock files that multiple processes will be accessing?

c,concurrency,process,fork

Short answer: you have to do it manually. There are certain guarantees on the atomicity of each write, but you'll still need to synchronize the processes to avoid interleaving writes. There are a lot of techniques for synchronizing processes. Since all of your writers are descendants of a common process,...

Unix - Pipe, forks, execlp, dup2, c program

c,linux,unix,pipe,fork

Things to be done: Main Process create a pipe (see: man pipe()) start 2 child processes (see: man fork) wait for both of them to exit (see: man wait) exit Chid 1 redirect stdout to write end of pipe (see: man dup) run ps -aux (see: man exec) exit Child...

How to set breakpoints in a php script using gdb

php,gdb

Short answer: If you want to debug PHP scripts use xdebug. Currently, gdb can only really debug compiled languages. It has a lot of knowledge baked in about executable file formats, debug info formats, how to unwind stack frames, and low-level stuff like that. What it doesn't have is a...

How set breakpoint to class member variable in gdb

c++,gdb,breakpoints,watchpoint

You can set breakpoint to the setter and getter function as shown below break TestClass::setVal(int) break TestClass::getVal() Note that, we have to specify the type of arguments that the function is receiving. Find more details here...

Shared Memory UNIX Segmentation Fault (fork()) in C++

c++,unix,segmentation-fault,fork,shared-memory

Check the return value of shmget (which you are storing in idShMem). It is possible for the call to fail. If it is negative then you are never successfully allocating the memory in the first place. If idShMem is negative, that is your problem. This is analogous to checking a...

Missing gdb symbols in Backtrace?

java,gdb

In ordinary code, gdb relies on debug information (and to a lesser extent the "linker" symbols) to find the names of functions as it unwinds. Debugging information is described in various standards, DWARF being the current best one for Linux. Compilers emit the debugging information that is then read by...

Unknown Segfault

c,segmentation-fault,gdb

In the create_string() function you should properly set the values of malloced str so that it does not contain any random values. Especially, str->value is not set to NULL. And then you are checking it in set_string() which may not succeed and you would strcpy() to unallocated memory causing undefined...

How to tell if child Node.js Process was from fork() or not?

javascript,node.js,process,fork

It's a bit of a hack, but you check if process.send exists in your application. When it was started using fork() it will exist. if (process.send === undefined) { console.log('started directly'); } else { console.log('started from fork()'); } Personally, I would probably set an environment variable in the parent and...

Why does print (absolute value) gives me back ASCII code in gdb

assembly,gdb,stackframe

print prints its arguments. Here, the argument is the integer 0x80484d0, which in decimal is 134513872. If you want to print a string, you can use the eXamine command, asking for 1 string. E.g: x/1s 0x80484d0 Or, you could also print an expression: print (char *) 0x80484d0 ...

How to get return value from a forked / spawned process in Ruby?

ruby,process,output,fork,spawn

There are a whole bunch of ways to run commands from Ruby. The simplest for your case is to use backticks, which capture output: `sleep 10; date` # "Tue Jun 23 10:15:39 EDT 2015\n" If you want something more similar to Process.spawn, use the open3 stdlib: require 'open3' stdin, stdout,...

pipe can not read/write all the chars

c++,linux,ubuntu,pipe,fork

You invoke std::strlen() on an uninitialized char array, which is your mistake. std::strlen() looks for the first occurence of a null byte in the array and returns its position. But the array is uninitialized, thus making the first occurence of the null byte undefined. Besides, you should check the return...

How to set breakpoints and make it break when debugging core dump with gdb?

gdb,coredump

You can restart your program once loading the core if you want to trace the steps leading to the crash. Use 'start', this will take you to the first line in your program. Then set breakpoints between main() and the crash point. See sample below: <pre> [[email protected] src]$ gdb -n...

Detect execution flow divergence

c++,debugging,gdb

Is it possible to use GDB to detect divergence between executions? If the application is single-threaded, reverse debugging can help. See this article for an example using UndoDB. You should also make sure there are no uninitialized memory reads, using either Valgrind or MemorySantizer. If the application is multi-threaded,...

R packages: breakpoint setup for C function with gdb in Debian (Testing)

c,r,debugging,gdb,debian

guess_fc() calls rval <- .Call("reg_wrong_fc", surname, name, year, month, day, female, codice_catastale, PACKAGE = "ifctools") so maybe you are simply not calling the C code you thought you were? (I'd have entered c to (c)ontinue execution, rather than signal 0.)...

Creating shared_ptr seems to cause segfault

c++,segmentation-fault,gdb,valgrind

As the functions are recursive insert calls insert_if_leq and vice versa, I had to make node_ptr root in insert_if_leq a reference, because the assignment in insert depended on it. The new function definitions are as follows: node_ptr scene_manager::insert(node_ptr & root, node_ptr other) node_ptr scene_manager::insert_if_leq(node_ptr & root, node_ptr other)...

gdb exiting instead of spawning a shell

c,bash,shell,gdb,suid

You can see the problem by compiling a simpler test program int main() { execlp("/bin/sed", "-e", "s/^/XXX:/", NULL); } All this does is start a version of sed (rather than the shell) and converts input by prepending "XXX:". If you run the resulting program, and type in the Terminal you...

nm versus gdb break

gdb,virtual-address-space,nm

Barring unusual things like -fPIE, what is going on here is that the gdb command break function actually means "break after the function prologue for function". This way, arguments are set up properly by the time the breakpoint is hit. If you want to break exactly at the first instruction...

C++ Fork child, ask child for process list, kill a process in Linux

c++,linux,pipe,fork

Compilation Errors occurred on line number:9,53,64 can be solved by using these: line 9: FILE* pipe = popen(cmd.data(), "r"); line 53: write(C2P[1], getlistOfProcesses("ps").data(), 1024); line 64: printf("%s",listOfProcesses.data()); Reason: These popen,write,printf requires char* as their arguments but you are passing them std::string. You have to use std::string.data() function instead as it...

Print addresses of all local variables in C

debugging,pointers,gcc,gdb,memory-address

There is no built-in command to do this. There is an open feature request in gdb bugzilla to have a way to show the meaning of all the known slots in the current stack frame, but nobody has ever implemented this. This can be done with a bit of gdb...

redirect I/Os in GDB in MI mode

gdb

You can use -exec-arguments for this: -exec-arguments < /path/to/somefile -exec-run ...

Trouble Understanding Fork Logic

process,fork,system-calls

Two things here: First, fork() return 0 in child process while it returns a non zero pid to the parent process. Second, short circuit of &&. So in the beginning of the first process (p0), it runs to i < 5 && !fork(). Now i = 0 and another process...

How to drill down into shared_ptr [Netbeans, clang++, gdb]

c++,debugging,gdb,clang,netbeans-8

One flaw of gdb is that the pretty-printing code is only for printing, and can't be used to dig deeper. This flaw also affects the "varobj" feature, which is what most GUIs use when communicating with gdb about value display. There are some possibilities that can make this better. First,...

signal() in C doesn't work

c,signals,fork

It seems to be a bug here: if (pidA != 0 & pidB != 0) { The correct should be "&&" and not "&": if (pidA != 0 && pidB != 0) { Edit: I got it working, for the kill function, As Chris Dodd said, you have to pass...

Same file descriptor after fork()

fork,file-descriptor,contention

When you see the phrase "duplicate a file descriptor", you need to understand it as "create a new file descriptor which points to the same thing the other one is pointing to". So when you duplicate fd 3, you get fd 4. The aren't the same number, but they identify...

gdb debugging stripped executables

linux,debugging,gdb,stripping

I would like to do step-by-step debugging in the assembler code What is stopping you from doing just that? readelf -h a.out will tell you what address the start is at. Set a breakpoint there and continue with stepi or nexti. This will actually take a really long time,...

Composer: Package that extends another from a fork

git,dependencies,fork,composer-php

I managed to this fix. According to composer docs, this is not an error, it's something that happens by design due to security. From a GitHub comment: Consider this: you have a project that depends on one of my packages. I depend on a package that is critical to your...

Running daemon as regular application (debugging in KDevelop)

c++,debugging,gdb,daemon,kdevelop

It's reasonably normal to add a special debugging mode to daemons to enable this. That is, add in a command-line option to disable the daemonizing step, having the program run in the foreground. Then it is is simple to run the daemon under gdb. I'm not aware of any generic...

Remote debugging of pure C program with GDB

android,c,android-ndk,gdb,gdbserver

So, at host, in gdb shell, before specifying the remote's target port, I should type shared. This command loads the shared symbols. Also, for compiling, I used -ggdb....

Segmentation fault strcmp in c

c,debugging,segmentation-fault,gdb,strcmp

You should check that you properly use strcmp(), the API is: int strcmp(const char *str1, const char *str2) You must: 1) Validate that st[i], your first argument is a pointer. 2) Make sure that st[i] & s has the Null terminator '\0'`. 3) Check that st[i] & s pointing to...

correct output for this fork concept in C

c,output,fork,atoi

Many of the questions you have asked can be answered by just looking into the man pages. Anyway I will try to explain them. 1) int atoi(const char *str) str -- This is the string representation of an integral number. This function atoi returns the converted integral number as an...

Best way to print information when debugging a race condition

c,debugging,gdb,printf,race-condition

Is this any better then putting a printf in my code? No, it's much worse. Every breakpoint that is hit in GDB triggers the following chain of events: context switch from running thread to GDB GDB stops all other threads (assuming default all-stop mode) GDB evaluates breakpoint commands GDB...

Fork creates a new process that is exactly the same as its parent

linux,fork

As others mentioned in the comments, a technique called Copy-On-Write mitigates the heavy cost of copying the entire memory space of the parent process. Copy-on-write means that memory pages are shared read-only between parent and child until either of them decides to write -- at which point the page is...