The night before he attacked the heart of the nation's capital, Michael Zehaf-Bibeau went into a stairwell at his Ottawa shelter and knelt on a standard-issue white bathroom towel to pray.

For years, Mr. Zehaf-Bibeau, 32, tried and failed to use prayer as a shield against the drug addiction and mental instability stalking him through adulthood, but that is just one shade in a complex portrait composed from dozens of interviews, court records and archives found along Mr. Zehaf-Bibeau's cross-country path.

This was a man who tried to divorce himself from family and moved to British Columbia to get a fresh start, only to land in the same sewer of petty crime and drugs that trapped him in Montreal. He tried to find community with fellow Muslims but drove them away. He would preach to the infidel one day and smoke crack cocaine the next. He even tried to rob a McDonald's wielding a sharp stick, hoping to go to prison and get help with his drug addiction. He served one day in jail.

Story continues below advertisement

The residents of the Ottawa Mission where Mr. Zehaf-Bibeau bowed in final prayer had their own piece of the picture, and there too he failed to fit in, putting off people with his extreme religiosity. "So as I'm walking by this guy I start singing that Christian hymn I'll Fly Away,'" said one resident, who wanted to be known only by his street name, Cakeman. "I'm a jokester. But I could see he didn't think it was funny. He didn't even look at me."

Five hours later, at around 8:30 a.m., someone at the shelter pulled a fire alarm. Mr. Zehaf-Bibeau slipped out. Ninety minutes later, he arrived at the National War Memorial, where he shot Corporal Nathan Cirillo before moving on to Parliament.

His mother, Susan Bibeau, was at a loss Thursday to explain what happened, saying her son was lost. "I am mad at our son, I don't understand and part of me wants to hate him at this time," said Ms. Bibeau, who is a deputy chairperson in the federal immigration department. Ms. Bibeau said she had lunch with her son last week, the first time she'd spoken to him in at least five years. "I have very little insight to offer."

The gunman was born Michael Joseph Paul Abdallah Bulgasem Zehaf Bibeau on Oct. 16, 1982, in Montreal, to Ms. Bibeau and Bulgasem Zehaf, a businessman of Libyan descent. In his school years, friends knew him as Mike Bibeau.

Mr. Zehaf-Bibeau attended a strict, private, French-language high school called Collège Laval, where students wore jackets and ties and could be penalized for swearing or having an untucked shirt. He was a well-liked classmate known for his ready smile and his skill with a Ping-Pong paddle. "He was part of our little 'immigrant' gang," recalled classmate Vito Garofalo, who lost touch with him after high school. "He was a good kid and he was funny. He just fit in."

He was also a fairly large boy who didn't hesitate to look after himself. "He was a strong boy," said Mr. Garofalo. "He had the capacity to pick up anybody if he wanted to. But he wasn't a troublemaker."

In those days, few students knew about his Libyan background, and one former classmate said everyone thought he was Italian. Mr. Zehaf-Bibeau switched to a public school in Laval in his fourth year.

Story continues below advertisement

"He likes to laugh and his smile drives girls crazy. He'll go far in life. He'll surely be a businessman in the near future," the inscription in his graduation yearbook read.

Within five years of graduation, around 2004, Mr. Zehaf-Bibeau was repeatedly in court for petty criminality, often linked to drugs such as marijuana and PCP. He decided to break away from his troubled Montreal life and head West.

"He told me at some point in his life, I guess his 20s, he cut ties basically with his parents," said former B.C. friend Dave Bathurst. "He worked at mining in Squamish. At some point he ended up living in Burnaby. He rented a single room, as far as I understand."

The fresh start did not endure. Mr. Zehaf-Bibeau was arrested in Vancouver on a robbery charge three years ago. He was a devout Muslim – but also a crack addict looking for redemption. A psychiatric report filed in British Columbia provincial court found he wasn't mentally ill and could be released. The document, however, also describes him as a deeply troubled man.

"The accused … wants to be in jail as he believes this is the only way he can overcome his addiction to crack cocaine," the mental status report states. "He has been a devoted Muslim for seven years and he believes he must spend time in jail as a sacrifice to pay for his mistakes in the past and he hopes to be a better man when he is eventually released." The report concluded: "I am unable to find any features or signs of mental illness." He got credit for time served and did one more day in jail.

Mr. Zehaf-Bibeau was also having trouble with his Muslim elders at a Burnaby mosque. Mufti Aasim Rashid, a spokesperson for the B.C. Muslim Association, said Mr. Zehaf-Bibeau was asked in 2011 to stop sleeping at the mosque.

Story continues below advertisement

"This was after he was out of jail and didn't have a place to go," Mr. Rashid said. "He was caught a couple of times trying to linger around when the mosque was about to be locked, and then after that he was told to never try to do that again."

Mr. Zehaf-Bibeau was in and out of the mosque for about four months. While he was there, elders described him as rude and in everyone's business. He picked a fight over the mosque's outreach attempts, complaining too many non-Muslims were visiting. People at the mosque kept their distance from him, Mr. Rashid said.

Then mosque officials learned Mr. Zehaf-Bibeau had somehow gained access to the facility's keys. "They changed all the locks," Mr. Rashid said. "That was the end of that."

A man who first met Mr. Zehaf-Bibeau at a detox centre in Vancouver's Downtown Eastside in December, 2012, said they shared a shelter dorm. Mr. Zehaf-Bibeau planned to hitchhike to Ottawa. His new friend urged him to take a bus instead for his safety.

"He had some problem with his passport and wanted to get that sorted out and he wanted to go back to Libya," said the man, who identified himself only as Steve. The RCMP says Mr. Zehaf-Bibeau told his mother he actually planned to go to Syria, where many extremists have gone to fight with Islamic State.

Steve reconnected with his friend in September at the Salvation Army's Beacon shelter. "He was working: unloading trailers, swamping and doing some kind of moving work. He told me he was getting pretty good wages."

Story continues below advertisement

Steve described Mr. Zehaf-Bibeau as friendly and deeply religious, but said he often used crack cocaine and heroin.

Patricia Cuff, a spokeswoman for the Salvation Army in B.C., confirmed Mr. Zehaf-Bibeau had stayed at the 60-bed Beacon shelter at some point. Police contacted the Salvation Army on Wednesday.

The Salvation Army is reaching out to those at the shelter who may have been affected by the news, she said.

"When an incident like this takes place, we make every means necessary to provide support to both our clients as well as to staff that may be impacted," she said.

At the Beacon shelter on Thursday, several people described him as religious; others said he was nice and seemed normal – "like you and me."

Mr. Zehaf-Bibeau surfaced Oct. 2 at the Ottawa mission, one kilometre from the National War Memorial, telling people he had travelled by bus from Vancouver to sort out passport issues.

Story continues below advertisement

Tom Wilson, 56, says Mr. Zehaf-Bibeau slept below his bunk, No. 294, last Friday or Saturday night, and had a heavy black hockey bag with him. "He only stayed that one night in my room because I kept questioning him about his huge bag."

One volunteer at the shelter, Abdel Kareem Abubakir, bonded instantly with Mr. Zehaf-Bibeau over their shared faith and Arabic language. "He was very pious. I tried to discuss with him these issues. But he seemed very extreme."

Mr. Zehaf-Bibeau carried the Koran with him everywhere but couldn't beat temptation. "When he collapsed into drugs, he became isolated," Mr. Abubakir said. "He was isolating himself. He was always sleeping. For three days he wasn't talking. His intention was to get a passport and get home. He had to stay away from drugs."

His issues didn't seem to originate from mental health problems, said Mr. Abubakir, but from drug use. "He was very sharp-minded, talkative, social person. He has no problem with the mental."

While he earned praise from staff and residents for helping translate for an elderly tenant who spoke only Arabic, Mr. Zehaf-Bibeau's demeanour soured in recent days. Several residents overheard him yelling angrily into a lobby telephone after several rental car outlets declined his business. Instead, he bought a used Toyota Corolla to drive to the War Memorial.

With reports from Ian Bailey and Mark Hume in Vancouver, Robyn Doolittle in Ottawa, Ingrid Peritz and Sean Gordon in Montreal, Colin Freeze and Tu Thanh Ha in Toronto, and the Associated Press