Restore Freezing Emacs’s Buffer

Published on 5月 05, 2021

Freezzzzzzzzzzzzzzzzzzzzzzzzzzzzzing

I’ve started blogging with org mode several months ago (a custom modified org mode publish: ox-blog).

However, the emacs was freezing for unknown reason this afternoon. And my work was still in buffer and did not save to disk.

So I used pgrep to find emacs process id, and used radare2 to attach onto it.

The memory map of emacs was huge, but I guess the buffer content laying in the heap. So I tried to search it.

[0x560b8f2614a6]> dm~heap
0x0000560b904ea000 - 0x0000560b9a1c9000 - usr 156.9M s rw- [heap] [heap] ; map.heap_.rw

I did not know how to search non ascii string, so I started with a ascii word:

[0x560b8f2614a6]> / lixia @ 0x0000560b904ea000
Searching 5 bytes in [0x560b904ea000-0x560b9a1c9000]
hits: 67
0x560b90564643 hit9_0 .TURE-2021-05-05-lixia.org  Doom Em.
0x560b90ca9c9a hit9_1 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b90ca9cc3 hit9_2 . 28544) :title "lixia" :subtitle "The.
0x560b90ccb46a hit9_3 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b90ccb493 hit9_4 . 28544) :title "lixia" :subtitle "The.
0x560b92b9d46a hit9_5 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b92b9d493 hit9_6 . 28544) :title "lixia" :subtitle "The.
0x560b92d57f03 hit9_7 .TURE-2021-05-05-lixia.org  Doom Em.
0x560b92d58da7 hit9_8 .osts/2021-05-05-lixia.org.
0x560b92d58dd7 hit9_9 .osts/2021-05-05-lixia.orgmouse-1: Pr.
0x560b92d58e29 hit9_10 .osts/2021-05-05-lixia.org0qV.
0x560b9363333a hit9_11 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b93633363 hit9_12 . 28544) :title "lixia" :subtitle "The.
0x560b93b43dc5 hit9_13 .osts/2021-05-05-lixia.org.
0x560b93b43ebb hit9_14 .05-lixia.org.
0x560b93b43f95 hit9_15 .osts/2021-05-05-lixia.org.
0x560b93b44085 hit9_16 .osts/2021-05-05-lixia.org.
0x560b93fc8ed3 hit9_17 .05-lixia.orgsea.
0x560b93fc8fc5 hit9_18 .osts/2021-05-05-lixia.orgt.
0x560b93fc90b5 hit9_19 .osts/2021-05-05-lixia.orgg.
0x560b93fc914b hit9_20 .05-lixia.org.
0x560b93fc9225 hit9_21 .osts/2021-05-05-lixia.orgt.
0x560b93fc9315 hit9_22 .osts/2021-05-05-lixia.orgb.
0x560b9433ac53 hit9_23 .TURE-2021-05-05-lixia.org  Doom Em.
0x560b946ad66f hit9_24 .osts/2021-05-05-lixia.org"2021-05-05.
0x560b946ad685 hit9_25 .org"2021-05-05-lixia.org" 45L, 466C .
0x560b946c4289 hit9_26 .#+TITLE: lixia#+DATE: <2021-0.
0x560b9527899a hit9_27 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b952789c3 hit9_28 . 28544) :title "lixia" :subtitle "The.
0x560b954f39fb hit9_29 .05-lixia.org.
0x560b954f3aed hit9_30 .osts/2021-05-05-lixia.orgu.
0x560b954f3bdd hit9_31 .osts/2021-05-05-lixia.org/.
0x560b959d76ba hit9_32 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b959d76e3 hit9_33 . 28544) :title "lixia" :subtitle "The.
0x560b95d27a23 hit9_34 .05-lixia.org.
0x560b95d27b15 hit9_35 .osts/2021-05-05-lixia.orgt.
0x560b95d27c05 hit9_36 .osts/2021-05-05-lixia.org.
0x560b95d27c83 hit9_37 .05-lixia.org.
0x560b95d27d5d hit9_38 .osts/2021-05-05-lixia.org.
0x560b95d27e4d hit9_39 .osts/2021-05-05-lixia.org.
0x560b95d864ea hit9_40 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b95d86513 hit9_41 . 28544) :title "lixia" :subtitle "The.
0x560b95e5223a hit9_42 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b95e52263 hit9_43 . 28544) :title "lixia" :subtitle "The.
0x560b96d733f2 hit9_44 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b96d7341b hit9_45 . 28544) :title "lixia" :subtitle "The.
0x560b9765b218 hit9_46 .resize)Vlixia-iVlixia.
0x560b9765b228 hit9_47 .lixia-iVlixia-i@+V2021-.
0x560b9765b243 hit9_48 .V2021-05-05-lixia.orgde/S$V.
0x560b9765b26f hit9_49 .osts/2021-05-05-lixia.org.el.gzS$.
0x560b9765b2ad hit9_50 .osts/2021-05-05-lixia.org/R$V/h.
0x560b97e5251b hit9_51 .05-lixia.org.
0x560b97e5260d hit9_52 .osts/2021-05-05-lixia.org.
0x560b97e526fd hit9_53 .osts/2021-05-05-lixia.org.
0x560b97e5277b hit9_54 .05-lixia.org.
0x560b97e52855 hit9_55 .osts/2021-05-05-lixia.org.
0x560b97e52945 hit9_56 .osts/2021-05-05-lixia.org.
0x560b9829408a hit9_57 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b982940b3 hit9_58 . 28544) :title "lixia" :subtitle "The.
0x560b98385c1a hit9_59 .osts/2021-05-05-lixia.org" '(:date (2.
0x560b98385c43 hit9_60 . 28544) :title "lixia" :subtitle "The.
0x560b99a90271 hit9_61 .V#+TITLE: lixia#+DATE: <2021-0.
0x560b99a902f3 hit9_62 .TURE-2021-05-05-lixia.orgmanSV.
0x560b99a9032f hit9_63 .osts/2021-05-05-lixia.org  Capture.
0x560b99b0ead5 hit9_64 .osts/2021-05-05-lixia.orgVor.
0x560b99b0eb40 hit9_65 .erPVlixiaEVThe s.
0x560b99b32500 hit9_66 .VlixiaVsymbo.

the only interesting hit is hit9_26 and hit9_61, so I inspected them seperately,

[0x560b8f2614a6]> ps 800 @ 0x560b946c4289
lixia
#+DATE: <2021-05-05 \xe4\xb8\x89>
#+SUBTITLE: The summer comes
#+CATEGORY: Life
#+FILETAGS: :Life:

\xe6\x88\x91\xe4\xb8\x8d\xe7\x94\xb1\xe6\x83\xb3\xe5\x86\x99\xe4\xb8\x80\xe7\x82\xb9\xe4\xb8\x9c\xe8\xa5\xbf\xef\xbc\x8c\xe4\xb9\x83\xe6\x98\xaf\xe4\xbb\x8e\xe7\xbb\x9d\xe6\x9c\x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97\x9b\xe8\x8b\xa6\xe3\x80\x82

\xe6\x88\x91\xe5\x88\x87\xe5\x88\x87\xe9\x9a\xbe\xe7\x9c\xa0\xef\xbc\x8c\xe9\x81\x81\xe5\x85\xa5\xe9\xa3\x98\xe6\xb8\xba\xe7\x9a\x84\xe6\x82\xb2\xe4\xbc\xa4\xe3\x80\x82


\xe7\xab\x8b\xe5\xa4\x8f\xe8\x87\xb3\xe7\x9f\xa3\xef\xbc\x8c\xe5\x8f\x88\xe6\x98\xaf\xe4\xb8

that is, I found the buffer is at hit9_26, flag it as blog

[0x560b8f2614a6]> f blog 4000 @ hit9_26-9

Another problem was how long the blog is. I notice the chinese character is encoded in utf8(I guess), but I noticed the buffer is ended with \x00\x00, so I searched \x00 instead. however, r2 was panic…

So I constrained the search scope

# seek blog first
[0x560b946c4280]> e search.from = $$
[0x560b946c4280]> e search.to = $$ + 3000
[0x560b946c4280]> /x 0000
Searching 2 bytes in [0x560b946c4280-0x560b946c4e38]
hits: 243
0x560b946c4bce hit0_0 0000
0x560b946c4bd0 hit0_1 0000
0x560b946c4bd2 hit0_2 0000
0x560b946c4bd4 hit0_3 0000
0x560b946c4bd6 hit0_4 0000
0x560b946c4bd8 hit0_5 0000
0x560b946c4bda hit0_6 0000

I double checked that the last chars is my blog content.

[0x560b946c4280]> ps @0x560b946c4bce

[0x560b946c4280]> ps @0x560b946c4bcd
\xa0
[0x560b946c4280]> ps @0x560b946c4bcc
\xab\xa0

I found the only way to print utf-8 encoded chinese character is use psi, but with some weired…

[0x560b946c4280]> psi 0x560b946c4bce-$$
#+TITLE: lixia
#+DATE: <2021-05-05 三>
#+SUBTITLE: The summer comes
#+CATEGORY: Life
#+FILETAGS: :Life:

我不由想写一点东西,乃是从绝望之中冥冥求生,在这夏日的首日,献给一切一切不可知的神。

#+BEGIN_QUOTE
立夏至矣,阴云凝聚。

我一整天躺在床上,不想说一句话。

家是温暖的港湾,也是无情的长夜。家是爱的浓情,家是爱的负担。

我回想往事,她强加我身上的枷锁。

我历数前程,她鼓舞我旗帜与�

Then I dumped the memory to file.

[0x560b946c4280]> wtf blog.org 0x560b946c4bce-$$
Dumped 2382 bytes from 0x560b946c4280 into blog.org

However, when I opened the dumped file, it was a messy…

there maybe some non-utf8?

I checked it with python.

In [5]: s.decode('utf-8', errors="replace")
Out[5]: '#+TITLE: lixia\n#+DATE: <2021-05-05 三>\n#+SUBTITLE: The summer comes\n#+CATEGORY: Life\n#+FILETAGS: :Life:\n\n我不由想写一点东西,乃是从绝望之中冥冥求生,在这夏日的首日,献给一切一切不可知的神。\n\n#+BEGIN_QUOTE\n立夏至矣,阴云凝聚。\n\n我一整天躺在床上,不想说一句话。\n\n家是温暖的港湾,也是无情的长夜。家是爱的浓情,家是爱的负担。\n\n我回想往事,她强加我身上的枷锁。\n\n我历数前程,她鼓舞我旗帜与甘甜。\n\n\n立夏至矣,晚风微寒。\n\n我冷冷的回应,家人热热的期切。\n\n家是春日的风,家是夏日的阳,家是夏至时节春去夏来的勇气和颓丧。\n\n我念念不忘,沉入甜蜜的痛苦。\n\n我切切难眠,遁入飘渺的悲伤。\n\n\n立夏至矣,又是一年。\n\n情绪没有尽头,也没有希望。\n\n我们终将在九泉之下相会,\n\n挣脱我们的心,挣脱我们的情,\n\n挣脱所有压在身上的,\n\n彼此谅解,不再悲伤。\n\n-- 夏至 2021.05.05\n#+END_QUOTE\n\n#+BEGIN_QUOTE\n我想起那天晚上的梦,在她之前居住的老房子里。\n\n那老房子在废弃的厂区家属院。窗外的杉树清脆欲滴,鸟鸣乘着晨风穿堂而过。他在窗边的竹躺椅坐着,那是又十年之前,刚从大病中恢复,他说不出他想说的,我似乎闻到芝麻糖的味道。\n\n这就是我关于那里几乎全部记忆,其他的种种本来很重要,却被时光吹散,无影无踪。\n\n梦中我在那老房子的另一间里,那是她也走了之后。我环顾房间的桌子和空荡荡的木床,零落着无法名状的奇异悲伤。\n\n桌子上一个小灵通手机, 忽然亮了。我把它拿起挂断,看了看时间,心想着,人走都走了,这有什么用呢?\n\n我准备离开那里, 关上灯,老木门要用力合上。关门的时候,忽然感到不寒而栗。往回推门的时候,借着楼道昏黄的灯光,我看到门里面不是房间,只有一堵红墙。\n\n楼道里有些不存在的侧灯,照过一个飘过的蓝色身影。\n\n我惊醒过来。\n\n只有一堵红墙。\n\n我时常想起她的话语,时常想起那面墙,时常发现那面墙模糊了话语和思想。\n\n#+END_QUOTE\n\x00往事会给我勇气吗?\n\n还是束缚不能动弹?\n\n人生就像矮人要塞,\n\n最终的最终都一样,\n\n黯淡下不同的\x00��篇章'

I guess some emacs machinary is responsible for the weired non-utf8 chars in memory. I modified the broken text, and got back my blog article.

Such a hacky experience, I remember I’ve saw a book in which he restore his blog article from heap for internet explorer, and I did a similar thing (but with free software to free software) several years later, never thought of this.

Life is strange, world is stranger.