github.com/tompreston/snapd@v0.0.0-20210817193607-954edfcb9611/cmd/snap-gdb-shim/snap-gdbserver-shim.c (about) 1 /* 2 * Copyright (C) 2020 Canonical Ltd 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 3 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * 16 */ 17 18 #include <signal.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <unistd.h> 22 23 #include "../libsnap-confine-private/utils.h" 24 25 int main(int argc, char **argv) { 26 if (sc_is_debug_enabled()) { 27 for (int i = 0; i < argc; i++) { 28 fprintf(stderr, "-%s-\n", argv[i]); 29 } 30 } 31 if (argc < 2) { 32 fprintf(stderr, "missing a command to execute"); 33 abort(); 34 } 35 // Signal to "snap run" that we are ready to get a debugger attached. When a 36 // debugger gets attached it will stop the binary at whatever point the 37 // binary is executing. So we cannot have clever code here that e.g. waits 38 // for a debugger to get attached because that code would also get 39 // stoppped/debugged by that debugger and that would be confusing for the 40 // user. 41 // 42 // once a debugger is attached we expect it to send: 43 // "continue; signal SIGCONT" 44 raise(SIGSTOP); 45 46 // signal gdb to stop here 47 printf("\n\n"); 48 printf("Welcome to `snap run --gdbserver`.\n"); 49 printf("You are right before your application is execed():\n"); 50 printf("- set any options you may need\n"); 51 printf("- (optionally) set a breakpoint in 'main'\n"); 52 printf("- use 'cont' to start\n"); 53 printf("\n\n"); 54 raise(SIGTRAP); 55 56 const char *executable = argv[1]; 57 execv(executable, (char *const *)&argv[1]); 58 perror("execv failed"); 59 // very different exit code to make an execve failure easy to distinguish 60 return 101; 61 }