Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

echo "Your stdio isn't very std." -- Larry Wall in Configure from the perl distribution


devel / comp.lang.c / Debugging a C program by attaching gdb to the separately running process

SubjectAuthor
* Debugging a C program by attaching gdb to the separately runningJanis Papanagnou
`* Re: Debugging a C program by attaching gdb to the separately running processScott Lurndal
 `* Re: Debugging a C program by attaching gdb to the separately running processKenny McCormack
  `* Re: Debugging a C program by attaching gdb to the separately runningJanis Papanagnou
   +- Re: Debugging a C program by attaching gdb to the separatelyKaz Kylheku
   `- Re: Debugging a C program by attaching gdb to the separately runningKenny McCormack

1
Debugging a C program by attaching gdb to the separately running process

<umbrml$31obq$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30396&group=comp.lang.c#30396

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.network!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Debugging a C program by attaching gdb to the separately running
process
Date: Mon, 25 Dec 2023 13:14:12 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <umbrml$31obq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Dec 2023 12:14:13 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a6e3e67dd4004c54e10a505e69935d07";
logging-data="3203450"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OnU7QH/J8567FR2zCs1yD"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:w+5QNqh1A4T4fiTWN0EI4dLv/J8=
X-Mozilla-News-Host: news://news.eternal-september.org:119
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Mon, 25 Dec 2023 12:14 UTC

(I hope the post is not OT here.)

I have to debug my C program and I'm using gdb on Linux. Since my
program is writing output to the screen (and I want separate control
of the debugger I/O) I started the program from one terminal window
and the debugger from another one.

'man gdb' says:
You can, instead, specify a process ID as a second argument,
if you want to debug a running process: gdb program 1234

Starting the program and calling the debugger reports this failure:

Attaching to process 29631
Could not attach to process.
If your uid matches the uid of the target process, check the setting
of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user.
For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Operation not permitted.

The mentioned information sources show:

$ cat /proc/sys/kernel/yama/ptrace_scope
1

$ cat /etc/sysctl.d/10-ptrace.conf
# The PTRACE system is used for debugging. With it, a single user process
# can attach to any other dumpable process owned by the same user. [...]
kernel.yama.ptrace_scope = 1

Given the comment in 10-ptrace.conf I wonder what's going wrong here.
I am running my program and gdb both under my UID. I am reluctant to
"try again as the root user" (as suggested by the gdb output); to me,
at least, that seems not to be the "right way". - Any suggestions how
to fix that?

Thanks.

Janis

Re: Debugging a C program by attaching gdb to the separately running process

<FuhiN.6171$SyNd.490@fx33.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30397&group=comp.lang.c#30397

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx33.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Debugging a C program by attaching gdb to the separately running process
Newsgroups: comp.lang.c
References: <umbrml$31obq$1@dont-email.me>
Lines: 49
Message-ID: <FuhiN.6171$SyNd.490@fx33.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 25 Dec 2023 15:47:49 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 25 Dec 2023 15:47:49 GMT
X-Received-Bytes: 2692
 by: Scott Lurndal - Mon, 25 Dec 2023 15:47 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>(I hope the post is not OT here.)
>
>I have to debug my C program and I'm using gdb on Linux. Since my
>program is writing output to the screen (and I want separate control
>of the debugger I/O) I started the program from one terminal window
>and the debugger from another one.
>
>'man gdb' says:
> You can, instead, specify a process ID as a second argument,
> if you want to debug a running process: gdb program 1234
>
>Starting the program and calling the debugger reports this failure:
>
>Attaching to process 29631
>Could not attach to process.
>If your uid matches the uid of the target process, check the setting
>of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user.
>For more details, see /etc/sysctl.d/10-ptrace.conf
>ptrace: Operation not permitted.
>
>The mentioned information sources show:
>
>$ cat /proc/sys/kernel/yama/ptrace_scope
>1

Set it to zero and reboot.

The sysctl settings (writable only with CAP_SYS_PTRACE) are:

0 - classic ptrace permissions: a process can PTRACE_ATTACH to any other
process running under the same uid, as long as it is dumpable (i.e.
did not transition uids, start privileged, or have called
prctl(PR_SET_DUMPABLE...) already). Similarly, PTRACE_TRACEME is
unchanged.

1 - restricted ptrace: a process must have a predefined relationship
with the inferior it wants to call PTRACE_ATTACH on. By default,
this relationship is that of only its descendants when the above
classic criteria is also met. To change the relationship, an
inferior can call prctl(PR_SET_PTRACER, debugger, ...) to declare
an allowed debugger PID to call PTRACE_ATTACH on the inferior.
Using PTRACE_TRACEME is unchanged.

2 - admin-only attach: only processes with CAP_SYS_PTRACE may use ptrace
with PTRACE_ATTACH, or through children calling PTRACE_TRACEME.

3 - no attach: no processes may use ptrace with PTRACE_ATTACH nor via
PTRACE_TRACEME. Once set, this sysctl value cannot be changed.

Re: Debugging a C program by attaching gdb to the separately running process

<umchaq$17ob8$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30398&group=comp.lang.c#30398

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: Debugging a C program by attaching gdb to the separately running process
Date: Mon, 25 Dec 2023 18:23:22 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <umchaq$17ob8$1@news.xmission.com>
References: <umbrml$31obq$1@dont-email.me> <FuhiN.6171$SyNd.490@fx33.iad>
Injection-Date: Mon, 25 Dec 2023 18:23:22 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1302888"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 25 Dec 2023 18:23 UTC

In article <FuhiN.6171$SyNd.490@fx33.iad>,
Scott Lurndal <slp53@pacbell.net> wrote:
....
>>$ cat /proc/sys/kernel/yama/ptrace_scope
>>1
>
>Set it to zero and reboot.

Easiest is to just do

# echo 0 > /proc/sys/kernel/yama/ptrace_scope

No reboot needed.

--
Just like Donald Trump today, Jesus Christ had a Messiah complex.

And, in fact, the similarities between the two figures are quite striking.
For example, both have a ragtag band of followers, whose faith cannot be shaken.

Re: Debugging a C program by attaching gdb to the separately running process

<umdqhd$3d2fh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30403&group=comp.lang.c#30403

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: Debugging a C program by attaching gdb to the separately running
process
Date: Tue, 26 Dec 2023 07:06:36 +0100
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <umdqhd$3d2fh$1@dont-email.me>
References: <umbrml$31obq$1@dont-email.me> <FuhiN.6171$SyNd.490@fx33.iad>
<umchaq$17ob8$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 26 Dec 2023 06:06:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0d2367935904de2fe2f82925b52e3e60";
logging-data="3574257"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18I05l+pnWRW/YTyfAhfixh"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:opg+Hz8LHJso9XRmjnXg4jIuLZc=
In-Reply-To: <umchaq$17ob8$1@news.xmission.com>
 by: Janis Papanagnou - Tue, 26 Dec 2023 06:06 UTC

On 25.12.2023 19:23, Kenny McCormack wrote:
> In article <FuhiN.6171$SyNd.490@fx33.iad>,
> Scott Lurndal <slp53@pacbell.net> wrote:
> ...
>>> $ cat /proc/sys/kernel/yama/ptrace_scope
>>> 1
>>
>> Set it to zero and reboot.
>
> Easiest is to just do
>
> # echo 0 > /proc/sys/kernel/yama/ptrace_scope
>
> No reboot needed.
>

Thanks, Scott and Kenny.

It had been very convenient that no reboot was necessary.

Janis

Re: Debugging a C program by attaching gdb to the separately running process

<20231225221332.87@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30404&group=comp.lang.c#30404

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Debugging a C program by attaching gdb to the separately
running process
Date: Tue, 26 Dec 2023 06:14:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <20231225221332.87@kylheku.com>
References: <umbrml$31obq$1@dont-email.me> <FuhiN.6171$SyNd.490@fx33.iad>
<umchaq$17ob8$1@news.xmission.com> <umdqhd$3d2fh$1@dont-email.me>
Injection-Date: Tue, 26 Dec 2023 06:14:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4ed03db719374b13a72cfc71b24465a9";
logging-data="3575834"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CCiuCPiFz6TGQ74T18dQZqCtZnxh6ycE="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:gIz5vHBlwcnb8IqdnzJ4c1fTrTU=
 by: Kaz Kylheku - Tue, 26 Dec 2023 06:14 UTC

On 2023-12-26, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 25.12.2023 19:23, Kenny McCormack wrote:
>> In article <FuhiN.6171$SyNd.490@fx33.iad>,
>> Scott Lurndal <slp53@pacbell.net> wrote:
>> ...
>>>> $ cat /proc/sys/kernel/yama/ptrace_scope
>>>> 1
>>>
>>> Set it to zero and reboot.
>>
>> Easiest is to just do
>>
>> # echo 0 > /proc/sys/kernel/yama/ptrace_scope
>>
>> No reboot needed.
>>
>
> Thanks, Scott and Kenny.
>
> It had been very convenient that no reboot was necessary.

However, a reboot will wipe out that setting, unless
you record the configuration in /etc/sysctl.conf,
or else stick that echo command into some "rc.local"
boot script.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Debugging a C program by attaching gdb to the separately running process

<umep7s$18s1e$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30405&group=comp.lang.c#30405

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: Debugging a C program by attaching gdb to the separately running
process
Date: Tue, 26 Dec 2023 14:50:36 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <umep7s$18s1e$1@news.xmission.com>
References: <umbrml$31obq$1@dont-email.me> <FuhiN.6171$SyNd.490@fx33.iad> <umchaq$17ob8$1@news.xmission.com> <umdqhd$3d2fh$1@dont-email.me>
Injection-Date: Tue, 26 Dec 2023 14:50:36 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1339438"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 26 Dec 2023 14:50 UTC

In article <umdqhd$3d2fh$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>On 25.12.2023 19:23, Kenny McCormack wrote:
>> In article <FuhiN.6171$SyNd.490@fx33.iad>,
>> Scott Lurndal <slp53@pacbell.net> wrote:
>> ...
>>>> $ cat /proc/sys/kernel/yama/ptrace_scope
>>>> 1
>>>
>>> Set it to zero and reboot.
>>
>> Easiest is to just do
>>
>> # echo 0 > /proc/sys/kernel/yama/ptrace_scope
>>
>> No reboot needed.
>>
>
>Thanks, Scott and Kenny.
>
>It had been very convenient that no reboot was necessary.

Another way to solve this problem - applicable here because the target
program was written and compiled by you - is to use prctl(2) to make the
target traceable (even if tracing is turned off at the system level).

Do "man prctl" and search for PR_SET_PTRACER for the details. You can
either specify that one specific program (pid) can trace (i.e., debug) the
calling process - or you can specify PR_SET_PTRACER_ANY.

I've never actually tested this, but I could see it being useful if you are
working on a system where you don't have root.

Changing the subject a bit, and just out of curiosity, which distros do this?
It seems to me to be a Ubuntu thing. I have one Ubuntu system - and that
system is the only one that does this "YAMA" thing. Is it unique to Ubuntu
or do others do it too?

--
Many North Koreans believe Kim-il-Sung is an "almighty god" who "created
the world" in seven days as a divine spirit millions of years ago, and
came to Earth as a human in 1912 as a messianic figure.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor